0

I have a Facebook application with 250K DAU. I use Memcached to cache the files and MySql selects because without that my server is going crazy.

I have set up the code like this:

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");

// FOR FILES

$doc = $memcache->get('mem_index_html');

if ($doc == null)
{
    $doc = new Document( HTML.'index.html' );
    $memcache->set('mem_index_html', $doc, 0, 86400);
}

// FOR SQL

$sql=sprintf('count(qtn_id) FROM tb_questions where qtn_lang="EN"));
$total_pages = $memcache->get($sql);

if ($total_pages == null)
{       
    $query_id = DB::query($sql);        
    list( $total_pages ) = DB::nextRow( $query_id );
    DB::free( $query_id );
    $memcache->set($sql, $total_pages, 0, 86400);
}

The problem is that after some time – usually 24 hours, errors are starting to show. No log is created but I receive a timeouts if I try to reload the page. I assume that there is a lots of traffic in that moments, the Memcached is not working correct and everything is slowing down. My temporary solution is to Flush the memcache - $memcache->flush(), but that is not good solution.

Am I doing something wrong? Is there something to optimize or I don’t know. I found searching thru web this http://code.google.com/p/memcached/wiki/TutorialCachingStory about making more that one Memecached servers. Is that the best solution?

Thanks for your answers.

Darko

1 Answers1

1

The fourth parameter in the $memcache->set command represents the expiration timeout. Once this is exceeded, the object stored will no longer be available. Since you set it to 24 hours they would not be available after that period unless you updated them.

If the expiry parameter is set to 0, the item will never be expire, but it can still be deleted if memcached is filled up and need to free some space.

If the value is less than 30 days (30 * 24 * 3600 = 2592000), the value provided is treated as a offset from the current time.

If the value is larger than that it will be interpreted as absolute time.

Chris
  • 352
  • 3
  • 10
  • Hi Chris, thanks for your answer. The thing is that I tried the both ways: with number 86400 and 0. But in both cases, after prox 24 hours something goes wrong and I have to flush the memcached to start working. My question now are: 1)How to free the memcached memory without restart the server? 2)Is Memcached good for files (php/html) or only for MySql? 3)I see lot of Memcached examples using md5. Should I use that? Thanks – darko4spain Dec 14 '10 at 08:56
  • Memcached can be used to store anything (that is not larger than 1 Mb) as it treats it just as data and do not interpret what you are storing, so storing HTML is not a problem. It is strange that 0 did not work for you. Could it be that it ran out of space and started evicting data? Try with a large value for expiry (several days) and also check statistics using the 'stats' command to see if any items have been evicted. – Chris Dec 14 '10 at 18:40