php - ZF2 Redis: how to set expiration time for a key -
i have set redis on server store information zend framework 2. now, can store information, can not give them expiration time naturally renew after while.
i have not found documentations step , seems me rather obscure.
my code:
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 have no effect 'namespace' => 'mycache', ), ), ) ) );
in controller :
.. 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'); } ..
i tried several methods, everytime, result same, no expiration information appears in redis :
127.0.0.1:6379> mycache:test "testtest" 127.0.0.1:6379> ttl mycache:test (integer) -1
thanks help!
take @ redis factory bellow:
<?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); /** * not required, although allow store can serialized php in redis */ $options->setliboptions( [ \redis::opt_serializer => \redis::serializer_php ] ); $redis = new redis($options); return $redis; } }
as can see example, ttl set 60 seconds , working expected.
Comments
Post a Comment