Question

I am trying to add memcache to my webapp deployed on GAE, and to do this I am using memcache.Client() to prevent damage from any racing conditions:

from google.appengine.api import memcache

client = memcache.Client()
class BlogFront(BlogHandler):
    def get(self):       
        global client 
        val = client.gets(FRONT_PAGE_KEY)
        posts = list()

        if val is not None:
            posts = list(val)
        else:
            posts = db.GqlQuery("select * from Post order by created desc limit 10")
            client.cas(FRONT_PAGE_KEY, list(posts))

        self.render('front.html', posts = posts)

To test the problem I have a front page for a blog that displays the 10 most recent entries. If there is nothing in the cache, I hit the DB with a request, otherwise I just present the cached results to the user.

The problem is that no matter what I do, I always get val == None, thus meaning that I always hit the database with a useless request.

I have sifted through the documentation:

And it appears that I am doing everything correctly. What am I missing?

(PS: I am a python newb, if this is a retarded error, please bear with me xD )

Was it helpful?

Solution

from google.appengine.api import memcache

class BlogFront(BlogHandler):
    def get(self):       
        client = memcache.Client() 
        client.gets(FRONT_PAGE_KEY)
        client.cas(FRONT_PAGE_KEY, 'my content')

For a reason I cannot yet possible understand, the solution lies in having a gets right before having a cas call ...

I think I will stick with the memcache non-thread-safe version of the code for now ...

OTHER TIPS

I suspect that the client.cas call is failing because there is no object. Perhaps client.cas only works to update and existing object (not to set a new object if there is none currently)? You might try client.add() (which will fail if an object already exists with the specified key, which I think is what you want?) instead of client.cas()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top