why many get_misses

1,744 views
Skip to first unread message

paanX

unread,
Feb 23, 2009, 2:32:18 AM2/23/09
to memcached
I have implemented memcache on a client site, to store a thumbnails in
a product listing.. I have a problem.. I dont know if this is normal.

Once memcached initialized, I will get this stats..
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0

On a first page load, i will get
STAT cmd_get 103
STAT cmd_set 103
STAT get_hits 0
STAT get_misses 103

On 2nd page load, I got this.
STAT cmd_get 309
STAT cmd_set 103
STAT get_hits 206
STAT get_misses 103

The get_misses, will happen at least once, when we do the checking
whether the cache key is empty or not?
So, this is normal?

Thanks

Colin Pitrat

unread,
Feb 23, 2009, 3:35:17 AM2/23/09
to memc...@googlegroups.com
Of course, this is normal ! The first time, the cache is empty, so each query fail, and fill in the cache with corresponding data. On second time, the cache is full, so everything is found in the cache. The only strange thing is that you get exactly two times more query on the second one than on the first one, are you sure there weren't three run ?

Stats are global on the whole time the server run, this mean you will never have 100% cache hit unless you fully fill the cache before any query is done.

2009/2/23 paanX <farhan...@gmail.com>

Farhan Faisal

unread,
Feb 23, 2009, 4:06:04 AM2/23/09
to memc...@googlegroups.com
Thanks for your reply.. 

Ya, that double cmd_get is on the 2nd run.. Isnt it becoz it check the key whether empty or not, and for sure its not empty, then another get. So, 2 cmd_get for each function call

$key_variable  = 'prodpics_'.$id;
if($mmc->get($key_variable) == ''){
//query from database
$mmc->set($key_variable,$db_value, false, 3600);
echo $db_value;
}else{
echo $mmc->get($key_variable);
}


---
Farhan Faisal

Walt Crosby

unread,
Feb 23, 2009, 7:16:12 AM2/23/09
to memc...@googlegroups.com
You don't actually want to check for the nullness of the key first.  In caching, you want to get the data, check for nullness on the get, and then set the cache if the get is empty.  If you do it your way, you have the potential for double access (which you are seeing), or alternatively, the get of the data being null right after the key was non-null. 
 
Walt


From: memc...@googlegroups.com [mailto:memc...@googlegroups.com] On Behalf Of Farhan Faisal
Sent: Monday, February 23, 2009 4:06 AM
To: memc...@googlegroups.com
Subject: Re: why many get_misses

Colin Pitrat

unread,
Feb 23, 2009, 9:29:30 AM2/23/09
to memc...@googlegroups.com
Yes, you should clearly adapt your code:
$key  = 'prodpics_'.$id;
$value=$mmc->get($key);

// Handle cache miss
if($value == '')
{
   $value = get_from_db();
   $mmc->set($key,$value, false, 3600);
}

echo $value;

I also removed the _variable suffix, because we now it's a variable ;-)

2009/2/23 Walt Crosby <wcr...@alum.mit.edu>

Farhan Faisal

unread,
Feb 23, 2009, 12:33:23 PM2/23/09
to memc...@googlegroups.com
thanks for your insight on memcache implementation.. I have change the code according to Colin Pitrat example.. 

Mine was just a basic memcache utilization, put it along while coding for a small website. 

Thanks a lot. 

Marcus Engene

unread,
Feb 23, 2009, 12:49:56 PM2/23/09
to memc...@googlegroups.com
I would recommend that the check of variable $value is done by === instead

if (false === $value)
{
    // it didn't exist
}

=== is true if the types on left side and right side are the same, and if the values are the same. It's a good habit to do.

if (false === strpos("are you here", "are"))
{
   // if (! strpos(...)) would have gotten me here too
}

etc

Best regards,
Marcus
Reply all
Reply to author
Forward
0 new messages