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 <wcro...@alum.mit.edu>

>  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:* memcached@googlegroups.com [mailto:memcac...@googlegroups.com] *On
> Behalf Of *Farhan Faisal
> *Sent:* Monday, February 23, 2009 4:06 AM
> *To:* memcached@googlegroups.com
> *Subject:* Re: why many get_misses
>
> 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
>
>  On Mon, Feb 23, 2009 at 4:35 PM, Colin Pitrat <colin.pit...@gmail.com>wrote:
>
>> 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 <farhanfai...@gmail.com>
>>
>>
>>> 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
>>
>>
>>
>

Reply via email to