Zend_Cache 理解问题
Zend_Cache understanding issue
我尝试使用 Zend_Cache
(第一次尝试)来保存有关用户授权的信息。这个想法和大部分源代码来自 Oleg Krivtsovs 教程。
如果我尝试检索缓存,我会收到错误消息。
Call to a member function getItem() on array
这里是FilesystemCache的实现,在我的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' => [
],
],
],
],
],
这里是我的工厂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
}
}
现在,在我的 AccessControl 服务中的初始化函数中,我尝试从缓存中检索:
$this->cache->getItem('rbac_container', $result);
出现上面的错误。
如能提供一些解释,我们将不胜感激。
您向 AccessControl 构造函数注入的是一个数组,而不是缓存实现,因为 $config['caches']['FilesystemCache']
return 是一个 FilesystemCache 选项数组(适配器、插件等)。您应该做的是通过 ContainerInterface 获取缓存实现,如下所示:
$cache = $container->get('FilesystemCache');
然后 ContainerInterface 将依赖于 StorageCacheAbstractServiceFactory 来查找您请求的缓存配置,并 return class 为您找到。
我尝试使用 Zend_Cache
(第一次尝试)来保存有关用户授权的信息。这个想法和大部分源代码来自 Oleg Krivtsovs 教程。
如果我尝试检索缓存,我会收到错误消息。
Call to a member function getItem() on array
这里是FilesystemCache的实现,在我的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' => [
],
],
],
],
],
这里是我的工厂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
}
}
现在,在我的 AccessControl 服务中的初始化函数中,我尝试从缓存中检索:
$this->cache->getItem('rbac_container', $result);
出现上面的错误。
如能提供一些解释,我们将不胜感激。
您向 AccessControl 构造函数注入的是一个数组,而不是缓存实现,因为 $config['caches']['FilesystemCache']
return 是一个 FilesystemCache 选项数组(适配器、插件等)。您应该做的是通过 ContainerInterface 获取缓存实现,如下所示:
$cache = $container->get('FilesystemCache');
然后 ContainerInterface 将依赖于 StorageCacheAbstractServiceFactory 来查找您请求的缓存配置,并 return class 为您找到。