Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google App Engine Memcache

When using memcache in GAE, I notice this weird behavior rarely. I set a value in memcache and when I try to retrieve the same value, I get None instead of the original value. The memcache value cannot have expired within such short span of time. I do ensure that True is returned when I set a value in memcache. What else can cause a memcache value to go missing? Is it a common system behavior?

Code Samples :

For setting the value

cache_set = memcache.set(matrix_name+'-'+str(m)+","+str(n),data[n],namespace=uuid)
while cache_set == False :
    sleep(0.1)
    logging.error(" Cache Set failed. Retrying for %s %s",matrix_name,str[m,n])
    cache_set = memcache.set(matrix_name+'-'+str(m)+","+str(n),data[n],namespace=uuid)

For retrieving the value

memcache.get(matrix_name+'-'+str(m)+","+str(n),namespace=uuid)
like image 721
Sam Avatar asked Feb 25 '11 01:02

Sam


2 Answers

Memcache is by its very nature unreliable. It doesn't guarantee how long it will keep data for, or even that it will keep data at all. You shouldn't be using it with the expectation that it will always return the stored data within some minimum period.

like image 161
Nick Johnson Avatar answered Oct 20 '22 00:10

Nick Johnson


What you need to do is to check whether memcache has the value that you want in its cache (in your case it isn't), otherwise you have to retrieve from datastore or recalculate again. That's a normal behavior of any caches.

The storage of the cache normally faster but smaller compared to the lower level storage which are slower but of larger capacity.

Imagine disk caches, it's way smaller than the actual disks. Only smaller chunk of disk data can be fetched into the cache. It's a matter of hit/miss rate than governs the effectivity of the cache.

In GAE your memcache are dynamically configured there, you don't have control over them, and most likely shared with other applications. So you shouldn't assume stuffs you put may not stay in the cache at all time. It is hit/miss game.

like image 34
Daniel Baktiar Avatar answered Oct 20 '22 00:10

Daniel Baktiar