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?
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.