ZF-2480: Zend_Cache_Frontend_Class does not cache by class

Issue Type: Improvement Created: 2008-01-24T20:03:19.000+0000 Last Updated: 2008-03-21T16:25:25.000+0000 Status: Resolved Fix version(s): - 1.5.0 (17/Mar/08)

Reporter: Darien Hager (hagerd) Assignee: Fabien MARTY (fab) Tags: - Zend_Cache

Related issues: Attachments:


When using Zend_Cache_Frontend_Class to proxy an object, I can create two different objects of different classes with different caching proxy objects.

Unfortunately, as long as the function name is the same, they share results! The cache ID is based only on the function name and arguments.

I found this to be completely counterintuitive and not obvious from the documentation.

Proposal: When constructing the ID key to cache with, additionally use the classname of the object (determined by reflection on init) or string classname for static calls. This way, a cached result is not improperly shared across all objects and all classes that have the same method name and inputs.

Patch to implement/fix is attached.


Posted by Darien Hager (hagerd) on 2008-01-24T20:49:19.000+0000

This patch should address the biggest problem--sharing across classes.

However, it still does not differentiate between caches for two objects of the same class with the same function and parameters.

<pre class="literal">
Index: Zend/Cache/Frontend/Class.php
--- Zend/Cache/Frontend/Class.php   (revision 7621)
+++ Zend/Cache/Frontend/Class.php   (working copy)
@@ -86,6 +86,15 @@
      * @var mixed
     private $_cachedEntity = null;
+    /**
+     * The class name of the cached object or cached abstract class
+     * 
+     * Used to differentiate between different classes with the same method calls.
+     *
+     * @var string
+     */
+    private $_cachedEntityLabel = '';
     // ----------------------
@@ -110,6 +119,12 @@
         $this->_cachedEntity = $this->_specificOptions['cached_entity'];
+        if(is_string($this->_cachedEntity)){
+           $this->_cachedEntityLabel = $this->_cachedEntity;         
+        }else{
+           $ro = new ReflectionObject($this->_cachedEntity);
+           $this->_cachedEntityLabel = $ro->getName();
+        }
         $this->setOption('automatic_serialization', true);
@@ -184,7 +199,7 @@
     private function _makeId($name, $parameters)
-        return md5($name . serialize($parameters));
+        return md5($this->_cachedEntityLabel . ":" . $name . ":" . serialize($parameters));

Posted by Darien Hager (hagerd) on 2008-01-25T11:23:56.000+0000

Metadata tweak

Posted by Fabien MARTY (fab) on 2008-01-26T07:04:10.000+0000

fixed in svn trunk (thanks)

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

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