4

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)

codecowboy
  • 9,835
  • 18
  • 79
  • 134

1 Answers1

9

Have you tried to set option ttl in adapter options?

'adapter' => array(
    'name' => 'filesystem',
    'options' => array(
        'cache_dir' => __DIR__.'/cache',
        'ttl' => 3600,
    ),
),

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.

$cache = Zend\Cache\StorageFactory::factory(array(
    'adapter' => array(
        'name'    => 'filesystem',
        'options' => array('ttl' => 5),
    ),
));

$cache->setItem('a', 'b');
for ($i = 1; $i <= 7; $i++) {
    sleep(1);
    echo "var_dump on {$i}th second ... ";
    var_dump($cache->getItem('a'));
}

Output is :

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
Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • Thanks. I have tried that but how do I check if it has expired? The docs don't seem to explain what ttl is for? It's badly named IMHO and not sure I would agree with your 'nice' assessment! – codecowboy Sep 03 '13 at 16:35
  • 1
    [TTL](http://en.wikipedia.org/wiki/Time_to_live) is commonly known shortcut for time-to-live, and it is used all over IT. To check if cached value is expired, just run `hasItem()` or `getItem()`. – Glavić Sep 03 '13 at 17:03
  • Thanks - I have updated the code. It does not seem to be working as expected. – codecowboy Sep 03 '13 at 17:06
  • 1
    Glavic, how you set data x in memcached for 5 minutes and data y for 10 minutes? – edigu Sep 03 '13 at 20:34
  • @foozy I think you can set TTL (`$cache->getOptions()->setTtl(60);`) before every `setItem()` call (test it). OR just make 2 instances with different ttl/namespace options > http://stackoverflow.com/a/18511076/67332 – Glavić Sep 03 '13 at 21:09
  • @Glavic thanks! I think the nesting of the ttl option was my problem. Seems to be working now. – codecowboy Sep 04 '13 at 05:46