I am new to Laminas, I need to know how Cache works

If anybody provides a complete working example, I’ll appreciate.

Hello and welcome to our forums! :smiley:

Before starting, make sure a cache adapter is installed, like the APCu or the Filesystem adapter.

Here is an example which uses two different cache adapters as storage for the cache:

require_once __DIR__ . '/vendor/autoload.php';

$config = (new Laminas\ConfigAggregator\ConfigAggregator([
    Laminas\Cache\ConfigProvider::class,
    Laminas\Cache\Storage\Adapter\BlackHole\ConfigProvider::class,
    Laminas\Cache\Storage\Adapter\Memory\ConfigProvider::class,
]))->getMergedConfig();

$container = new Laminas\ServiceManager\ServiceManager($config['dependencies']);

/** @var Laminas\Cache\Service\StorageAdapterFactoryInterface $storageFactory */
$storageFactory = $container->get(
    Laminas\Cache\Service\StorageAdapterFactoryInterface::class
);

$storage1 = $storageFactory->create(Laminas\Cache\Storage\Adapter\BlackHole::class);
$storage2 = $storageFactory->create(Laminas\Cache\Storage\Adapter\Memory::class);

$storage1->setItem('key', 'value');
if ($storage1->hasItem('key')) {
    echo $storage1->getItem('key'); // "value"
}

$storage2->setItem('key', 'value');
if ($storage2->hasItem('key')) {
    echo $storage2->getItem('key'); // "value"
}

Check also the quick start and the example section: