Magento Forum

Page 1 of 5
Magento memcached backend? 
 
SimpleHelixcom
Enthusiast
 
Avatar
Total Posts:  906
Joined:  2007-08-31
Huntsville, AL
 

I realize that to utilize the backend with apc you have to do the following:
<cache>
<backend>apc</backend>
<prefix>alphanumeric</prefix>
</cache>

But for memcached, there has to be more setting to with than just replacing “apc” with “memcached” such as inputting the memcached address.

p.s.
Can the varien team give us some insight into this new backend integrating “feature”?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bjoern Ellebrecht
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2008-05-08
Hamburg (Norderstedt)
 

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.  cool smile

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 (Xin <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.  grin

Greetings
Björn

P.S.: I have attached 2 files, the patched method and the additional infomation in local.xml.

File Attachments
 
Magento Community Magento Community
Magento Community
Magento Community
 
Dmitriy
Magento Team
 
Total Posts:  39
Joined:  2007-08-31
 

Hi

We planning implement Memcache support in core.
Described way is ok, but one small note - please, don’t modify core files. You can create folder app/code/local/Mage/Core/Model and copy file app/code/core/Mage/Core/Model/App.php to this folder. Then you can apply patch from Bjoern Ellebrecht. (in this way you will protected from future core upgrade, but please after upgrade check App.php file modifications)

Class Mage_Core_Model_App can’t be rewrited throw xml because this model is part of initialization process. Instance of this class is being created before we load all configuration data.

 
Magento Community Magento Community
Magento Community
Magento Community
 
peterw83
Member
 
Total Posts:  44
Joined:  2007-11-12
 

Dmitriy,

Any chance the memcache support in core will be availabe in 1.1.7 or will it be at a later time?

Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Bjoern Ellebrecht
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2008-05-08
Hamburg (Norderstedt)
 

Hi Dmitriy,

thank you for this information (to copy file with folder-structure to local). I have copy all patched files to local. Works fine.  grin

In this context, I have another question:

Works this practice also with following code? (i.e.: I have copied a core model and changed it)

<?php
...
    
Mage::getModel('.../...');
...
?>

Greetings
Björn

 
Magento Community Magento Community
Magento Community
Magento Community
 
Vilpenstylen
Member
 
Total Posts:  54
Joined:  2008-05-29
 

Maybe this could be packaged as a module to Magento Connect for easy installation?

 
Magento Community Magento Community
Magento Community
Magento Community
 
aarne
Sr. Member
 
Total Posts:  130
Joined:  2007-09-06
 

Is there a way to test if memcached is in use. I’ve installed the server and php extension, added this patch. But still I can’t be sure memcache is in use, the cachefiles come up to the var/cache folder and phpinfo() says memcache has 0 active connections. So that part tells me it’s not using memcached.

Edit: I’ve tested that memcached server works, but has no items. I’ve tried both to batch local folder or actual App.php with no change. Any ideas on getting memcached to work? And yes, also the php extension is working.

 
Magento Community Magento Community
Magento Community
Magento Community
 
retif
Sr. Member
 
Total Posts:  171
Joined:  2008-08-16
Odessa, Ukraine
 

what is the point to use apc || memcached without tag support?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Michae1
Enthusiast
 
Total Posts:  826
Joined:  2007-08-31
 

Apc and Memcached backends in Magento do have tag support.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Michae1
Enthusiast
 
Total Posts:  826
Joined:  2007-08-31
 

There’s a built-in support for memcached in Magento 1.2.0
You can check the app/etc/local.xml.additional file to see how can it be configured.

 
Magento Community Magento Community
Magento Community
Magento Community
 
_julius_
Jr. Member
 
Total Posts:  2
Joined:  2007-10-25
 
Michael - 13 January 2009 11:57 AM

There’s a built-in support for memcached in Magento 1.2.0
You can check the app/etc/local.xml.additional file to see how can it be configured.

Hello,

I also read in release notes that magento supports now memcache as a caching backend and supports session, but it seems that local.xml as not been updated to help us setting up the whole thing.

What seetings do we have to use?

Regards

EDIT: ok, found the files in a newly downloaded archive.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Michae1
Enthusiast
 
Total Posts:  826
Joined:  2007-08-31
 
_julius_ - 14 January 2009 12:58 AM

What seetings do we have to use?

Check app/etc/local.xml.additional

 
Magento Community Magento Community
Magento Community
Magento Community
 
i960
Guru
 
Avatar
Total Posts:  633
Joined:  2007-10-01
Bakersfield, CA
 

Couple questions.

1.) I am already using tmpfs for the /var/cache directory.  Given that, is there any benefit to using memcached?  Can I use both or should I pick 1 or the other?
2.) With memcached enabled, will Magento use it to cache database queries?
3.) From looking at local.xml.additional, it appears that session data can be saved in memcached as well.  I’m guessing that means if the server is restarted, all session data will be lost.  What are the ramifications of this?  Will it log out customers and cause their shopping carts to be cleared?  I certainly wouldn’t restart a live server when customers are on the site unless absolutely necessary, but I would like to know what affects this would have.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Michae1
Enthusiast
 
Total Posts:  826
Joined:  2007-08-31
 
i960 -

1.) I am already using tmpfs for the /var/cache directory.  Given that, is there any benefit to using memcached?  Can I use both or should I pick 1 or the other?

You should test which one is faster on your server.

i960 -

2.) With memcached enabled, will Magento use it to cache database queries?

Magento will cache the same things that are cashed in var/cache directory.

i960 -

3.) From looking at local.xml.additional, it appears that session data can be saved in memcached as well.  I’m guessing that means if the server is restarted, all session data will be lost.  What are the ramifications of this?  Will it log out customers and cause their shopping carts to be cleared?

Yes, customers will be logged out. Guest users shopping carts will be cleared. Registered (logged in) users carts are kept under their accounts and not in sessions, so their carts will be available again once they log in.

 
Magento Community Magento Community
Magento Community
Magento Community
 
i960
Guru
 
Avatar
Total Posts:  633
Joined:  2007-10-01
Bakersfield, CA
 

Thanks Michael.  I have memcached installed and working, and I have benchmarked both back to back about a dozen times.  Memcached is slightly faster for me, so I will stick with that.  I am also using the Fooman Speedster extension which has it’s own cache in the var directory.  For that I am still using tmpfs.  The site is very fast and I am happy. grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
Carumba
Jr. Member
 
Total Posts:  18
Joined:  2009-02-27
California
 

i960,

Do you mind posting a sample config? It’s not clear to me what you have to put in for the compression, cache_dir etc. Thanks.

<memcached><!-- memcached cache backend related config -->
<servers><!-- any number of server nodes can be included -->
<server>
<host><![CDATA[]]></host>
<port><![CDATA[]]></port>
<persistent><![CDATA[]]></persistent>
</server>
</servers>
<compression><![CDATA[0]]></compression>
<cache_dir><![CDATA[]]></cache_dir>
<hashed_directory_level><![CDATA[]]></hashed_directory_level>
<hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
<file_name_prefix><![CDATA[]]></file_name_prefix>
</memcached>

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 5