【问题标题】:ZF2 Redis: how to set expiration time for a keyZF2 Redis:如何设置密钥的过期时间
【发布时间】:2016-05-15 15:45:16
【问题描述】:

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

我还没有找到有关此步骤的一些文档,而且在我看来相当晦涩。

我的代码:

页面: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


    【解决方案1】:

    你也可以试试这个:

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

    因此无需在工厂中全局设置。 这对我有用:)

    【讨论】:

      【解决方案2】:

      看看下面我的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 秒,并且按预期工作。

      【讨论】:

        【解决方案3】:

        Predis\Client 有一个 setEx 的“魔法调用”方法命令执行器:

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

        仔细检查您提到的一切是否按预期工作:

        127.0.0.1:6379>转储 your_key 127.0.0.1:6379>ttl your_key

        希望它有所帮助:)!

        【讨论】:

          【解决方案4】:
          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',
                          ),
                      ),
                  )
              )
          );
          

          【讨论】:

            猜你喜欢
            • 2016-09-02
            • 2017-05-05
            • 1970-01-01
            • 2018-01-17
            • 2018-12-03
            • 2015-08-14
            • 1970-01-01
            • 2020-09-26
            • 2020-04-19
            相关资源
            最近更新 更多