有人可以帮我用 zend 框架设置 redis 缓存。我已成功设置文件系统缓存如下。 global.pgp
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=tvguide;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
'Zend\Cache\Storage\Filesystem' => function($sm) {
$cache = Zend\Cache\StorageFactory::factory(array(
'adapter' => 'filesystem',
'plugins' => array(
'exception_handler' => array('throw_exceptions' => false),
'serializer'
)
));
$cache->setOptions(array(
'cache_dir' => './data/cache'
));
return $cache;
},
),
),
);
我的模块.php
'factories' => array(
'json_hub\Model\Entity\CustomerQueriesTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$cacheAdapter = $sm->get('Zend\Cache\Storage\Filesystem');
$table = new Model\Entity\CustomerQueriesTable($dbAdapter);
$table->setCache($cacheAdapter);
return $table;
},
)
在 Controller 中,我调用缓存如下。
$this->cache->setItem('samplecache', $data);
非常感谢您的帮助
最佳答案
在配置文件中有:
...
'my-redis-cache' => array (
'adapter' => array (
'name' => 'redis',
'options' => array (
'server' => [
'host' => '127.0.0.1',
'port' => 6379,
]
)
),
)
...
然后在 Controller 的某处:
use Zend\Cache\StorageFactory;
...
$redis = StorageFactory::factory ($this->getServiceLocator ()
->get ('config') ['my-redis-cache']);
if ($redis->hasItem ('key'))
{
$value = $redis->getItem ('key');
}
关于php - 如何使用 Zend Framework 2 设置 Redis 缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16858838/