php - 如何使用 Zend Framework 2 设置 Redis 缓存?

标签 php zend-framework caching redis

有人可以帮我用 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/

相关文章:

php - 在正则表达式 (php) 方面需要一些帮助

php - yii 中的 findAll()

zend-framework - Zend 休息 API : putAction on POST method

php - 在 zend 框架中使用 LIMIT 进行更新

php - Yii2 多重缓存存储

c# - 在 ASP.NET 中缓存异步方法的结果

php - 在cviebrock/laravel-elasticsearch中设置elasticsearch映射

php - 就地编辑更新文本

zend-framework - 使用 zend 在另一个 Controller 中使用 Controller

c - 在缓存中存储数组