I would like to store some XML in a Zend filesystem cache and have it expire after 30 minutes. How does one set the cache duration / expiry? I am using Zend cache as a component and not in the context of a 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)); // the mtime on the file stays the same as does timestamp from ls -al in a terminal.
How do I set an expiration time and then subsequently check if the cache has expired? The above code does not seem to expire the cache after 60 seconds (the .dat file's timestamp does not change)
Have you tried to set option
ttl
in adapter options?ZF documentation has even nice quick start examples, where TTL is presented.
Update:
I have tested next script, and TTL is working like it should. You have problem elsewhere.
Output is :