Search code examples
phpzend-frameworkmagentomagento-1.5

Magento, memcached, and tags


I have spent the better part of the last week configuring and tweaking the caching for our Magento store and it is working great on my dev install. I have Tinybrick's Lightspeed and Speedbooster extensions along with some customizations. It does full page caching and properly flushes the correct tags when I edit a product so that only the pages affected get flushed. I initially set it up using file caching and only just switched it over to Memcached. On my local dev server, this is still working great. When I put it on the remote dev server running in the same environment as our live site, this doesn't work as expected. For the most part it is good, but when I edit a product, I have to flush the entire cache to see the change. After doing some research I have learned that Memcached doesn't support flushing by matching tag...or at least that's what I understood from the Zend documentation:

Be careful : with this backend, "tags" are not supported for the moment as the "doNotTestCacheValidity=true" argument.

Also, if you look at the source for the file Zend/Cache/Backend/Memcached.php in the clean function:

case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
    $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_MEMCACHED_BACKEND);
    break;

However, on my local dev server, it seems to be working fine, which I really can't explain. So that begs the question, "Why?". Are there differences in versions of the Zend Framework or PHP that may affect this? My local dev server is running the following:

PHP 5.3.3-1ubuntu9.6 with Suhosin-Patch (cli) (built: Oct 14 2011 22:31:25) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

Our dev/live server is running this:

PHP 5.2.17 (cli) (built: Jun 13 2011 14:23:24) Copyright (c) 1997-2010 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies

Could that be the difference? Does anyone have any experience with this? If this is, in fact, a framework limitation, are there any know work arounds? Or am I just doing something wrong?

Thanks!


Solution

  • According to this jira ticket, tags aren't supported for almost all backends in zf. You proposed to use hybrid backend Zend_Cache_Backend_TwoLevels instead. And magento already uses it! Look into Mage_Core_Model_Cache::_getBackendOptions(). So, now we should look into Zend_Cache_Backend_TwoLevels::clean() source code, for example:

            case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
                $ids = $this->_slowBackend->getIdsMatchingTags($tags);
                $res = true;
                foreach ($ids as $id) {
                    $bool = $this->remove($id);
                    $res = $res && $bool;
                }
                return $res;
                break;
    

    As you can see, zf looks for tags in slow backend and if tags are found - it deletes them from both backends. So, either something wrong happened to your low backend or tags are present in fast backend but not present in slow backend (some backends synchronization issue).

    You can read following articles that describe two leve caching in Magento: