ZF2 Redis:如何设置密钥的过期时间
ZF2 Redis: how to set expiration time for a key
我必须在服务器上设置一个 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
感谢您的帮助!
下面看看我的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 秒并且按预期工作。
Predis\Client 有一个 "magic call" 方法命令执行器用于 setEx:
$redis->setEx($key, $expireTTL, $value);
- 如果不存在具有自定义过期时间的传递值,这将设置密钥。
- 这将更新现有密钥,重置到期时间。
仔细检查您提到的一切是否按预期工作:
127.0.0.1:6379>转储 your_key
127.0.0.1:6379>ttl your_key
希望对您有所帮助:) !
你也可以试试这个:
$redis = $this->getServiceLocator()->get('Cache\RedisFactory');
$redis->getOptions()->setTtl(10);
$redis->setItem('test', 'Custom Value');
因此无需在工厂中进行全局设置。
这对我有用 :)
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',
),
),
)
)
);
我必须在服务器上设置一个 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
感谢您的帮助!
下面看看我的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 秒并且按预期工作。
Predis\Client 有一个 "magic call" 方法命令执行器用于 setEx:
$redis->setEx($key, $expireTTL, $value);
- 如果不存在具有自定义过期时间的传递值,这将设置密钥。
- 这将更新现有密钥,重置到期时间。
仔细检查您提到的一切是否按预期工作:
127.0.0.1:6379>转储 your_key 127.0.0.1:6379>ttl your_key
希望对您有所帮助:) !
你也可以试试这个:
$redis = $this->getServiceLocator()->get('Cache\RedisFactory');
$redis->getOptions()->setTtl(10);
$redis->setItem('test', 'Custom Value');
因此无需在工厂中进行全局设置。 这对我有用 :)
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',
),
),
)
)
);