php - How do I set expire time for Zend Cache Storage? -
i store xml in zend filesystem cache , have expire after 30 minutes. how 1 set cache duration / expiry? using zend cache component , not in context of full zf2 application.
$cache = \zend\cache\storagefactory::factory(array( 'adapter' => array( 'name' => 'filesystem', 'ttl' => 60, // kept short during testing 'options' => array('cache_dir' => __dir__.'/cache'), ), 'plugins' => array( // don't throw exceptions on cache errors 'exception_handler' => array( 'throw_exceptions' => false ), ) )); $key = 'spektrix-events'; $events = new simplexmlelement($cache->getitem($key, $success)); if (!$success) { $response = $client->setmethod('get')->send(); $events = new simplexmlelement($response->getcontent()); $cache->setitem('spektrix-events', $events->asxml()); } var_dump($cache->getmetadata($key)); // mtime on file stays same timestamp ls -al in terminal.
how set expiration time , subsequently check if cache has expired? above code not seem expire cache after 60 seconds (the .dat file's timestamp not change)
have tried set option ttl
in adapter options?
'adapter' => array( 'name' => 'filesystem', 'options' => array( 'cache_dir' => __dir__.'/cache', 'ttl' => 3600, ), ),
zf documentation has nice quick start examples, ttl presented.
update:
i have tested next script, , ttl working should. have problem elsewhere.
$cache = zend\cache\storagefactory::factory(array( 'adapter' => array( 'name' => 'filesystem', 'options' => array('ttl' => 5), ), )); $cache->setitem('a', 'b'); ($i = 1; $i <= 7; $i++) { sleep(1); echo "var_dump on {$i}th second ... "; var_dump($cache->getitem('a')); }
output :
var_dump on 1th second ... string(1) "b" var_dump on 2th second ... string(1) "b" var_dump on 3th second ... string(1) "b" var_dump on 4th second ... string(1) "b" var_dump on 5th second ... null var_dump on 6th second ... null var_dump on 7th second ... null
Comments
Post a Comment