Page 1 of 1

Zend Cache File flawed?

Posted: Sat Dec 02, 2006 2:51 pm
by skruby
I really don't get Zend Cache's implementation.

When they do the clean up, they do this:
Compare the current time with the filemtime + the TTL. If current time is >, then cache record is invalid and thus, removed.

Prob. is: They expect all cache records in the same folder to have the same TTL.

For eg.
1) I have a common cache folder for ALL cache records
2) I cache 3 records with 3 diff TTL
Record A - TTL (5 mins)
Record B - TTL (50 mins)
Record C - TTL (1 hour)

Assuming now, 30 mins has passed.
if i try to retrieve Record A, i'll have to tell Zend Cache the TTL for that record, which is 5 mins. So while retrieving Record A, Zend will compare filemtime + TTL vs current time for every record in the cache folder when doing the cleanup. BUT, they assume that Record A, B and C all have TTL of 5 mins. Of course, what happens next? Record A, B and C ALL get removed.

Which is not what is supposed to happen. What should happen is: Record A is removed but the other 2 remains.

Zend Cache's approach is similar to Pear Cache's approach, which is flawed too in my opinion.

A better approach would be to adjust the filemtime to filemtime + TTL when saving the cache record. Then, when doing the cleanup, compare current time vs the filemtime.

It is basically the same approach as the native memcached and apc functions. You DON'T set the TTL when instantiating the Cache object. You set the TTL when saving the record!

So you can do this, set different ttl for diff records:
$cache = new Zend_cache;
$cache->set('key', $data, $ttl1);
$cache->set(key2', $data2, $ttl2);

BUT NOOO.... Zend/PEAR don't allow you to do that. To do that, you have to create 2 objects
$cache1 = new Zend_cache (with $ttl1)
$cache1->set('key', $data);

$cache2 = new Zend_cache (with $ttl2)
$cache2->set('key', $data);

And of course, we have that clean up problem. And to circumvent this crap, we have to group records into diff folders based on TTL.

Seriously, Zend/Pear's approach is SO so SOOOOOO wrong.

i believe THIS IS the right approach. http://www.phpguru.org/static/Caching.html

Although i feel that it would be better to not make the class static, so you can have more than one cache object.

Sorry for sounding dismissive, but i really couldn't fathom why Zend and PEAR would adopt such a flawed approach.