|
Hello,
for our project we need to cluster Magento and we had to use memcached for sessions and magento cache. I tried to overload this Model (responsible for cache backend), but I was not be able to overload or it is currently not possible by Magento to do this). So I decided to patch Magento.
WARNING: This is the short way (patch Magento); not recommended if you want to use future upgrades!!!
Here is my step by step tutorial:
1. Install Memcached Server
2. Install PHP PECL memcache Extension like this:
# pecl install memcache
3. Go to file app/code/core/Mage/Model/App.php and copy this file to “local” folder structure
[EDIT-start]
from:
app/code/core/Mage/Model/App.php
to:
from:
app/code/local/Mage/Model/App.php
[EDIT-stop]
Replace method ”getCache()” with my patched one (shown below).
/** * Retrieve cache object * * @return Zend_Cache_Core */ public function getCache() { if (!$this->_cache) { $backend = strtolower((string)Mage::getConfig()->getNode('global/cache/backend'));
// ################################################################# // Memcached Backend Cache PATCH // // Author : Bjoern Ellebrecht // Date : 2009-09-27 (15:00) // Notice : This mehtod should be able to overload and NOT patched! // // PATCH - START if (extension_loaded('memcache') && $backend == 'memcached') { // Classname (end of Filename) $backend = 'Memcached';
foreach( (array)Mage::getConfig()->getNode('global/cache/servers') as $memcached_server ) { $memcached_servers[] = (array)$memcached_server; }
$backendAttributes = array ( 'servers' => $memcached_servers, 'compression' => false, 'cache_dir' => null, 'hashed_directory_level' => null, 'hashed_directory_umask' => null, 'file_name_prefix' => (string)Mage::getConfig()->getNode('global/cache/prefix') ); } // PATCH - END // ################################################################# else if (extension_loaded('apc') && ini_get('apc.enabled') && $backend=='apc') { $backend = 'Apc'; $backendAttributes = array( 'cache_prefix' => (string)Mage::getConfig()->getNode('global/cache/prefix') ); } else { $backend = 'File'; $backendAttributes = array( 'cache_dir'=>Mage::getBaseDir('cache'), 'hashed_directory_level'=>1, 'hashed_directory_umask'=>0777, 'file_name_prefix'=>'mage', ); } $this->_cache = Zend_Cache::factory('Core', $backend, array( 'caching'=>true, 'lifetime'=>7200, 'automatic_cleaning_factor'=>0, ), $backendAttributes ); } return $this->_cache; }
4. So if you have patched this method, you need to set the memcached server parameter in your app/etc/local.xml.
<config> <global>
<!-- ...here comes your database and session save stuff -->
<cache> <backend>memcached</backend> <servers> <server1> <host>127.0.0.1</host> <port>11211</port> <persistent>0</persistent> </server1> <!-- here you can add as much servers as you like; --> <!-- just increment number (X) in <serverX> and </serverX> --> <!-- <server2> <host>127.0.0.1</host> <port>11211</port> <persistent>0</persistent> </server2> --> </servers> </cache>
</global>
<!-- ...here comes your admin routes stuff -->
</config>
5. Bevore you can use memcache, you have to do:
- stop Apache Server
- clear magento cache path [ var/cache/ ] (if you are in the magento root directory and if you have rights to do this) rm -rf var/cache/*
- start Memcached (see homepage for configuration parameter)
# memcached -d -u {as_linux_user} -m 128 127.0.0.1 -p 11211
- start Apacher Server
So, now you (Magento-System) should be able to store all Magento cache inside Memcached-Server and boost up your system.
Greetings
Björn
P.S.: I have attached 2 files, the patched method and the additional infomation in local.xml.
File Attachments
|