ZF-2480: Zend_Cache_Frontend_Class does not cache by class

Description

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.

Comments

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.

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));
     }
 
 }

Metadata tweak

fixed in svn trunk (thanks)