When the memcached fails on a server, Magento croaks and tells you the cache is bust
|
|
Notice: MemcachePool::get() [memcachepool.get]: Server unix:///var/run/memcached.sock (tcp 0, udp 0) failed with: No such file or directory (2) in /home/magfnftp/magento.byte.nl/app/code/core/Zend/Cache/Backend/Memcached.php on line 141 |
This means that if PHP can’t retrieve a cache key from the cache (since it does not run or is broken), PHP throws an error. Magento handles this by simply aplying the general error display. Not pretty!
Fortunately there is a sane fallback for Magento memcached backend, (Magento 1.3, 1.4 and 1.5).
For Magento 1.3 and 1.4, it is easily fixed with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
--- app/code/core/Mage/Core/Model/App.php.orig 2010-02-22 13:34:18.000000000 +0100 +++ app/code/core/Mage/Core/Model/App.php 2010-02-22 13:21:32.000000000 +0100 @@ -1016,6 +1016,20 @@ false, true ); + + /* byte.nl - Test the cache, if it doesn't work, use default filecache */ + if ($this->_cache) { + try { + $this->_cache->load('bytetest'); + } catch (Exception $e) { + /* + * The cache is not valid! Use the file cache. Do this by updating + * the config node for 'cachebackend', then calling getCache again. + */ + Mage::getConfig()->setNode('global/cache/backend', 'file'); + return $this->getCache(); + } + } } return $this->_cache; |
This patch applies to Magento 1.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
--- a/app/code/core/Mage/Core/Model/App.php +++ b/app/code/core/Mage/Core/Model/App.php @@ -382,6 +382,20 @@ class Mage_Core_Model_App $options = array(); } $this->_cache = Mage::getModel('core/cache', $options); + + if ($this->_cache) { + try { + $this->_cache->load('bytetest'); + } catch (Exception $e) { + /* + * The cache is not valid! Use the file cache. Do this by updating + * the config node for 'cachebackend', then calling getCache again. + */ + Mage::getConfig()->setNode('global/cache/backend', 'file'); + return $this->getCache(); + } + } + return $this; }; |
As you can see in the code, this makes sure the configured cache works.
- Test the cache by retrieving a random key.
- If PHP throws an error, catch it with a try and catch block.
- Reset Magento to use the relatively safe file cache backend.
Done!
No more ugly cache-is-down errors