簡體   English   中英

如何使用Zend Framework 2設置Redis Cache?

[英]How to setup Redis Cache with Zend framework 2?

有人可以幫我用zend framework設置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;
            },
        ),
    ),
);

我的Module.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;
                },
            )

在控制器中,我呼喚緩存如下。

$this->cache->setItem('samplecache', $data);

高度贊賞您的幫助

在配置文件中:

...
    'my-redis-cache' => array (
            'adapter' => array (
                    'name' => 'redis',
                    'options' => array (
                            'server' => [
                                    'host' => '127.0.0.1',
                                    'port' => 6379,
                            ]
                    )
            ),
    )
...

然后在控制器中的某個位置:

use Zend\Cache\StorageFactory;

...

$redis = StorageFactory::factory ($this->getServiceLocator ()
        ->get ('config') ['my-redis-cache']);

if ($redis->hasItem ('key'))
{
        $value = $redis->getItem ('key');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM