ZF-8431: Cache_Backend_TwoLevels: auto_refresh_fast_cache always saves fast backend


The current behaviour of auto_refresh_fast_cache is that it saves on every load. Saving is much slower (at least for memache) than loading, so this should be avoided whenever possible.

I'd prefer cache misses from time to time for the slow backend and loading from slowBackend over always saving to fastBackend.

Possible solution:

Index: /var/www/vpcms/library/zend/1.9.0/Zend/Cache/Backend/TwoLevels.php
--- /var/www/vpcms/library/zend/1.9.0/Zend/Cache/Backend/TwoLevels.php  (revision 29934)
+++ /var/www/vpcms/library/zend/1.9.0/Zend/Cache/Backend/TwoLevels.php  (working copy)
@@ -194,16 +194,18 @@
     public function load($id, $doNotTestCacheValidity = false)
         $res = $this->_fastBackend->load($id, $doNotTestCacheValidity);
+        $loadedFromFast = true;
         if ($res === false) {
             $res = $this->_slowBackend->load($id, $doNotTestCacheValidity);
             if ($res === false) {
                 // there is no cache at all for this id
                 return false;
+            $loadedFromFast = false;
         $array = unserialize($res);
         // maybe, we have to refresh the fast cache ?
-        if ($this->_options['auto_refresh_fast_cache']) {
+        if ($this->_options['auto_refresh_fast_cache'] && !$loadedFromFast) {
             if ($array['priority'] == 10) {
                 // no need to refresh the fast cache with priority = 10
                 return $array['data'];


Hi, Niko. I think You may need refresh only when the fastBackend returns falses though the lowBackend returns true. Is it correct for You?

Yes, that's what my patch does.

This is especially useful when the fastBackend gets cleaned (memcache->flush() was called by another cache, server restart) but the slowBackend not.

I think the load method should set new life time of data in the fastBackend as the data is accessed.

So, I will categorize it won't fix.

So you don't think this extra write is performance wise not optimal?

If the save method would not update $newFastLifetime at line 217, we could change line 206. But sadly the change may cause BC problems.

What about a new option that enables this to keep BC? 'auto_extend_fast_lifetime' or something

Hi, Niko.

How about to using false for the auto_refresh_fast_cache option?

It might lead your problem to be solved, I think.

Not in the usecase where slowBackend never gets cleaned and fastBackend does.

I could disable auto_refresh_fast_cache and implement it myself if ZF-8430 was applied. The current behaviour simply doesn't use fastBackend anymore if the cache entry is only in slowBackend.