Setup Zend-Cache

Hello.
Could You explain me, how I have to setup zend-cache in main configuration file service_manager’s section.
In documentation I have tutorial, how to do it by calling service manager’s methods.

I have configuration, but it only throws exceptions, if I want to get service in application.

'service_manager' => [
    'factories' => [
        'storage' => 'Zend\Cache\Service\StorageCacheFactory',
    ],
],
'storage' => [
    'adapter' => [
        'name' => 'apc',
        'options' => [
            'cache_dir' => __DIR__ . '/../../data/cache',
            'ttl' => (60 * 60 * 24) * 1,
        ],
    ],

]

May be I have to use another factory, or use section ‘abstract_factories’.

We can’t see your exceptions…

If I try to get my cache service
$serviceManager->get('storage');

Fatal error : Uncaught Zend\Cache\Exception\InvalidArgumentException: Missing “adapter” in /var/www/vendor/zendframework/zend-cache/src/StorageFactory.php:56 Stack trace: #0 /var/www/vendor/zendframework/zend-cache/src/Service/StorageCacheFactory.php(31): Zend\Cache\StorageFactory::factory(Array) #1 /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(764): Zend\Cache\Service\StorageCacheFactory->__invoke(Object(Zend\ServiceManager\ServiceManager), ‘storage’, NULL) #2 /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate(‘storage’) #3 /var/www/module/Application/Module.php(45): Zend\ServiceManager\ServiceManager->get(‘storage’) #4 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Application\Module->onBootstrap(Object(Zend\Mvc\MvcEvent)) #5 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent)) #6 /var/www/vendor/ze in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 771

In a zend-mvc based application you can register zend-cache as module. Any configuration for the factories is not needed then. Now you can put your configuration under the config-key caches.

If you already have 'Zend\Cache' in config/modules.config.php, you can register under caches config:

// config/autoload/global.php
return [
    // ...
    'caches' => [

        // service cache adapter apcu
        'apcu' => [
            'adapter' => 'apcu',
            'options' => [
                'ttl' => 7200,
            ],
        ],

        // service cache adapter filesystem
        'filesystem' => [
            'adapter' => 'filesystem',
            'options' => [
                'ttl' => 7200,
	        'cache_dir' => './data/cache',
            ],
        ],

    ],
    // ...
];

so, via service manager, you can call:

$serviceManager->get('apcu');
$serviceManager->get('filesystem');

what does apcu mean?

“APC User Cache” - https://php.net/manual/en/book.apcu.php

1 Like

Ok, thank you!:smile: