zend-frameworkzend-framework3zend-cache

Zend_Cache understanding issue


I try to use Zend_Cache (first try) to save information about user grants. The idea and most of the source code comes from Oleg Krivtsovs tutorial.

I get an error, if I try to retrieve my cache.

Call to a member function getItem() on array

Here the implementation of FilesystemCache, in my global.php

'caches' => [
    'FilesystemCache' => [
        'adapter' => [
            'name'    => Filesystem::class,
            'options' => [
                // Store cached data in this directory.
                'cache_dir' => './data/cache',
                // Store cached data for 1 hour.
                'ttl' => 60*60*1
            ],
        ],
        'plugins' => [
            [
                'name' => 'serializer',
                'options' => [
                ],
            ],
        ],
    ],
],

Here my factory class:

<?php
namespace User\Service;

use User\Controller\Plugin\AuthPlugin;
use User\Model\GrantsTable;
use User\Model\UserTable;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\Factory\FactoryInterface;
use Interop\Container\ContainerInterface;

class AccessControlFactory implements FactoryInterface {
 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        $config = $container->get('config');
        $userTable = $container->get(UserTable::class);
        $grantsTable = $container->get(GrantsTable::class);
        $cache = $config['caches']['FilesystemCache'];
        $userplugin = $container->get(AuthPlugin::class);
    //    $authentication = $container->get( \Zend\Authentication\AuthenticationService::class);
        
        return new AccessControl($userTable, $grantsTable, $cache, $userplugin);//, $authentication
    }
}

Now in the init function within my AccessControl Service, I try to retrieve from the cache:

$this->cache->getItem('rbac_container', $result);

There I get the above error.

Any help with a bit of explanation would be appreciated.


Solution

  • What you're injecting to the AccessControl constructor is an array, not a cache implementation, because $config['caches']['FilesystemCache'] returns an array of FilesystemCache options (adapter, plugins, etc.). What you're supposed to do is fetch the cache implementation via the ContainerInterface, like this: $cache = $container->get('FilesystemCache');

    Then the ContainerInterface will depend on StorageCacheAbstractServiceFactory to find your requested cache configs and return the class for you.