Zend_Cache seemed very elegant to me at first, until I tried to make a customization.

I have some apps that will be using memcached for objects. Thing is, some of my server farms will have multiple sites and networks, all using the same pool of memcached servers. Of course all the networks will cache similar named objects, like 'acl' for instance. So, to avoid namespace trampling, or rather to "create" namespaces, I thought it'd be very useful to have cache id prefixes. Now, instead of setting keys in memecached just called 'obj_name' they'd be 'uniquenetwork_obj_name'. This allows me to have an acl object for Network1 (network1_acl) and an acl object for network2 (network2_acl).

I could just go ahead and setup a constant and pass it in along with the ids into every load(), save(), remove(), etc calls, but that seems like a lot of unnecessary crap. Instead, it makes a lot more sense to me to create a new backend option for my "cache id prefix", then have each one of those methods concat the prefix on to the front of ids automatically just before the real memcache calls are made.

So, I went about this, and its a real mess. First of all, all options are hardcoded into the classes and get explicitly checked at object instantiation. OK, well, I extended Zend_Cache_Backend_Memcached and did this:

   public function __construct($options = array())
   {
       // add cache id prefix as an allowed option, default nothing
       $this->_options['cache_id_prefix'] = null;
       // Call parent constructor
       parent::__construct($options);
   }

Then each one of those methods now look basically like this:

   public function load($id, $doNotTestCacheValidity = false)
   {
       $id = $this->_options['cache_id_prefix'] . $id;
       parent::load($id, $doNotTestCacheValidity);
   }

Overall, I didn't think that was too big of a deal, but then I realized that the backends are hardcoded and checked in Zend_Cache, just like the options, so I'd have to do the same thing for that. What's worse though, is only the name of the backend is passed in, and then class names are created and called with a hardcoded 'Zend_Cache_Backend' . $backend. Now I can't even call my class something different. Now I'd have to create my class file specifically in the /Zend/Cache/Backend directory, and call it something like CustomMemcached.

Anyway, overall Zend_Cache is a very nice component with lots of options, and I appreciate all of them, its just that its very closed. Maybe that was the intention, and if so, maybe you would accept a patch file from me to officially add this feature to the memcache backend.

Thanks in advance for reading,
Tony

Reply via email to