0

有人可以帮我用 zend 框架设置 redis 缓存。我已成功设置文件系统缓存,如下所示。 全局.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;
                },
            )

在控制器中,我调用缓存如下。

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

如果非常感谢您的帮助

4

1 回答 1

3

在配置文件中有:

...
    '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');
}
于 2013-07-05T08:56:20.747 回答