Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached - Why data is not stored?

I'm actually trying to understand why memcached is not storing data for a give Id.

I'm using Magento EE 1.11.2.0 that is based on Zend Framework 1.11.1. The interface used to deal with memcached is Zend_Cache_Backend_Memcached

Now there is not such big custom code here, this is my custom container that save the cache:

public function saveCache($blockContent)
{

    $lifeTime = 86400 * 30 * 6;
    $tags = array(
        My_Module_Model_PageCache_Container_Category_Blabla::CACHE_TAG
    );
    $cacheId = $this->_getCacheId();

    if ($cacheId !== false) {
        $this->_saveCache($blockContent, $cacheId, $tags, $lifeTime);
    }
    return $this;
}

I'm just forcing magento to use my custom cache tag and a fixed lifetime ( memcache doens't support custom tag so I guess this is not the problem ) also my lifetime is not used in memcached because I can see that the default one is used.

At the beginning I thought the issue was caused by a to long cache ID but now after reducing it ( <31 char ) this didn't help me:

I can see that the load() method Zend_Cache_Backend_Memcached return always false for my cache ids. While the save() method return true like if it was cached.

This is my local.xml configuration:

<cache>
        <slow_backend_store_data>1</slow_backend_store_data>
        <auto_refresh_fast_cache>0</auto_refresh_fast_cache>
        <backend>memcached</backend>
        <slow_backend>database</slow_backend>
        <memcached>
            <servers>
                <!--<server>-->
                <!--<host><![CDATA[127.0.0.1]]></host>-->
                <!--<port><![CDATA[11213]]></port>-->
                <!--<persistent><![CDATA[1]]></persistent>-->
                <!--</server>-->

                <server>
                    <host><![CDATA[unix:///tmp/memcached.sock]]></host>
                    <port><![CDATA[0]]></port>
                    <persistent><![CDATA[0]]></persistent>
                    <weight><![CDATA[2]]></weight>
                    <timeout><![CDATA[5]]></timeout>
                </server>
            </servers>
            <compression><![CDATA[0]]></compression>
            <hashed_directory_level><![CDATA[]]></hashed_directory_level>
            <hashed_directory_umask><![CDATA[]]></hashed_directory_umask>
            <file_name_prefix><![CDATA[]]></file_name_prefix>
        </memcached>
    </cache>
    <full_page_cache>
        <backend>memcached</backend>
        <slow_backend>database</slow_backend>
        <slow_backend_store_data><![CDATA[1]]></slow_backend_store_data>
        <memcached>
            <servers>
                <server>
                    <host><![CDATA[unix:///tmp/memcached.sock]]></host>
                    <port><![CDATA[0]]></port>
                    <persistent><![CDATA[0]]></persistent>
                    <weight><![CDATA[2]]></weight>
                    <timeout><![CDATA[10]]></timeout>
                    <retry_interval><![CDATA[10]]></retry_interval>
                    <status><![CDATA[1]]></status>
                    <stats_update_factor><![CDATA[1]]></stats_update_factor>
                </server>
            </servers>
        </memcached>
    </full_page_cache>

I also tried to check the entry of memcached to see if my id was stored or not with this command:

echo 'stats cachedump 35 35' | sudo nc -U /tmp/memcached.sock

  • Any idea about the reason of this ?
  • How can I debug it ? ( I cannot go under Zend_Cache_Backend_Memcached with xdebug)
like image 989
WonderLand Avatar asked Jul 21 '13 03:07

WonderLand


People also ask

How is data stored in Memcached?

How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.

Where does memcache store data?

Memcached stores the data in the memory and when the data is needed the application checks for the data in the memcache via its daemon. Memcached has the ability to store SQL queries, that way the next time the query is ran, it can return the result from memory.

How long does Memcached keep data?

The expiration time in Memcached is in seconds. For instance, the default value is 10800 seconds. But, it can have a maximum value of 2592000 seconds that is, 30 days.

What do you store in memcache?

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.


2 Answers

You can restart memcached with -vv and look at the interactions.

BTW, the default port for memcache is 11211. FYI

like image 122
Daniel Avatar answered Sep 26 '22 08:09

Daniel


here the issue is $lifetime and a relative bug in Zend_Cache_Backend_Memcached.

Memcached has a max lifetime of 30 days (2592000) so the limit I was using it was too big and that is why the data was not stored.

Sadly:

  1. Zend_Cache_Backend_Memcached::save() doesn't check if the limit is correct < 2592000

  2. the set() method on the memcached object return true even if it doesn't store the data $result = @$this->_memcache->set($id, array($data, time(), $lifetime), $flag, $lifetime);

like image 39
WonderLand Avatar answered Sep 24 '22 08:09

WonderLand