0

I have a zend cache which stores an array from the db. The cache can be read & updated fine. But the actual cache file seems to disappear after a day or so. I thought that adding automatic_cleaning_factor = 0 would solve this, but that doesn't seem to be the case.

$frontendOptions = array(
    'caching' => true,
    'cache_id_prefix' => 'mysite_blah',
    'lifetime' => 14400, # 4 hours
    'automatic_serialization' => true,
    'automatic_cleaning_factor' => 0,
);
$backendOptions = array(
    'cache_dir' => "{$_SERVER['DOCUMENT_ROOT']}/../../cache/zend_cache/"
);
$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

if(!$result = $cache->load('test_blah'))
{
    // run SQL
    ...    
    $cache->save($my_array_from_db, 'test_blah');
}
else
{
     $result = $cache->load('test_blah');
}

The page which uses this cache isn't very popular, not sure if that has anything to do with it..... Any ideas?

Glen Solsberry
  • 11,960
  • 15
  • 69
  • 94
Adam
  • 1,098
  • 1
  • 8
  • 17

1 Answers1

0

I cannot explain why your file is vanishing, I think something is missing from your sample that is probably removing the cache file. I can however have a few notes that might help you out...

I think you're misinterpreting the automatic_cleaning_factor parameter. This does not disable your cache from expiring. This only disables the cache frontend from automatically cleaning any cache that may have expired when you call the save() method.

When you load() your cache it still gets tested for validity, and if invalid, it gets ignored and your database will be queried for fresh data. You can pass true as a second parameter on the load() to ignore the validity test.

However you should probably just set your lifetime to a very long time instead of overriding cache validation, because it's there for a reason.

Also, this code can be simplified:

if(!$result = $cache->load('test_blah')) {
    // run SQL
    ...    
    $cache->save($my_array_from_db, 'test_blah');
} else {
     $result = $cache->load('test_blah');
}

To:

if(!$result = $cache->load('test_blah')) {
    // run SQL
    ...   
    $result = $my_array_from_db;
    $cache->save($result, 'test_blah');
}

The else is not needed because $result from your cache is assigned in the if() statement.

Darryl E. Clarke
  • 7,537
  • 3
  • 26
  • 34
  • Thanks for the explanations. I'll put these into action and report back if it fixes my problem. – Adam Apr 19 '11 at 10:45
  • Thought: if the load() validation fails, does it remove the cache file at that point? Or it the cache file only removed/updated in save() – Adam Apr 19 '11 at 10:46
  • Since removing the unnecessary else block, this problem has disappeared. – Adam Apr 21 '11 at 14:38