ZF2 Redis:如何设置密钥的到期时间

问题描述 投票:1回答:4

我必须在服务器上设置一个Redis来存储来自Zend Framework 2的信息。现在,我可以存储信息,但我不能给他们一个到期时间,因为他们会在一段时间后自然更新。

我没有找到关于这一步的一些文件,在我看来相当模糊。

我的代码:

page:config / autoload / cache.global.php

return array(
    'caches' => array(
        'redis' => array (
            'adapter' => array (
                'name' => 'redis',
                'lifetime' => 60, //doesn't work
                'options' => array (
                    'server' => array (
                        'host' => 'x.x.x.x',
                        'port' => x
                    ),
                    'ttl' => 10, // seems to have no effect
                    'namespace' => 'mycache',
                ),
            ),
        )
    )
);

在控制器中:

..
use Zend\Cache\StorageFactory;
..
        $redis = StorageFactory::factory ($this->getServiceLocator ()
             ->get ('config')['caches']['redis']);

        if ($redis->hasItem ('test')) {
                var_dump($redis->getItem ('test'));
                $redis->removeItem('test');
        } else {
            $redis->addItem('test', 'testtest');
        }
..

我尝试了几种方法,但每次都是相同的,Redis中没有出现过期信息:

127.0.0.1:6379> get mycache:test
"testtest"
127.0.0.1:6379> ttl mycache:test
(integer) -1

谢谢你的帮助!

php caching zend-framework redis zend-framework2
4个回答
0
投票

看看我的redis工厂:

 <?php
 namespace Application\Service\Factory;

 use Zend\ServiceManager\FactoryInterface;
 use Zend\ServiceManager\ServiceLocatorInterface;
 use Zend\Cache\Storage\Adapter\RedisOptions;
 use Zend\Cache\Storage\Adapter\Redis;

 class RedisCacheFactory implements FactoryInterface
 {
     public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('Config');
        $config = $config['redis'];

        $options = new RedisOptions();
        $options->setServer(
            [
                'host' => $config["host"],
                'port' => $config["port"],
                'timeout' => $config["timeout"]
            ]
        );
        $options->setTtl(60);

        /**
         * This is not required, although it will allow to store anything that can be serialized by PHP in Redis
         */
        $options->setLibOptions(
            [
                \Redis::OPT_SERIALIZER => \Redis::SERIALIZER_PHP
            ]
         );

        $redis = new Redis($options);

        return $redis;
    }
}

从示例中可以看出,TTL设置为60秒,并且按预期工作。


0
投票

Predis \ Client为setEx提供了一个“魔术调用”方法命令执行程序:

$redis->setEx($key, $expireTTL, $value);
  • 如果自定义到期时间的传递值不存在,则设置密钥。
  • 这将更新现有密钥,重置到期时间。

如上所述仔细检查,看看一切是否按预期工作:

127.0.0.1:6379>dump your_key 127.0.0.1:6379>ttl your_key

希望能帮助到你 :) !


0
投票

你也可以试试这个:

$redis = $this->getServiceLocator()->get('Cache\RedisFactory');
$redis->getOptions()->setTtl(10);
$redis->setItem('test', 'Custom Value');

所以没有必要在工厂全局设置它。这项工作对我来说:)


-1
投票
return array(
    'caches' => array(
        'redis' => array (
            'adapter' => array (
                'name' => 'redis',
                'options' => array (
                    'server' => array (
                        'host' => 'x.x.x.x',
                        'port' => x
                    ),
                    'Ttl' => 10, // Starting with capital letter
                    'namespace' => 'mycache',
                ),
            ),
        )
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.