ZF-8431: Cache_Backend_TwoLevels: auto_refresh_fast_cache always saves fast backend

Issue Type: Patch Created: 2009-12-01T07:00:42.000+0000 Last Updated: 2009-12-04T23:51:13.000+0000 Status: Resolved Fix version(s): Reporter: Niko Sams (nikosams) Assignee: Satoru Yoshida (satoruyoshida) Tags: - Zend_Cache

Related issues: - ZF-7972



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:

<pre class="highlight">
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'];


Posted by Satoru Yoshida (satoruyoshida) on 2009-12-01T14:46:28.000+0000

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

Posted by Niko Sams (nikosams) on 2009-12-02T00:56:55.000+0000

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.

Posted by Satoru Yoshida (satoruyoshida) on 2009-12-02T06:10:07.000+0000

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.

Posted by Niko Sams (nikosams) on 2009-12-02T23:51:51.000+0000

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

Posted by Satoru Yoshida (satoruyoshida) on 2009-12-03T00:18:13.000+0000

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

Posted by Niko Sams (nikosams) on 2009-12-03T00:32:43.000+0000

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

Posted by Satoru Yoshida (satoruyoshida) on 2009-12-04T23:16:33.000+0000

Hi, Niko.

How about to using false for the auto_refresh_fast_cache option?

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

Posted by Niko Sams (nikosams) on 2009-12-04T23:51:13.000+0000

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.

Have you found an issue?

See the Overview section for more details.


© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.