views:

93

answers:

4

I'm creating a python app for google app engine and I've got a performance problem with some expensive operations that are repetitive within a single request. To help deal with this I'd like to create a sort of mini-cache that's scoped to a single request. This is as opposed to a session-wide or application-wide cache, neither of which would make sense for my particular problem.

I thought I could just use a python global or module-level variable for this, but it turns out that those maintain their state between requests in non-obvious ways.

I also don't think memcache makes sense because it's application wide.

I haven't been able to find a good answer for this in google's docs. Maybe that's because it's either a dumb idea or totally obvious, but it seems like it'd be useful and I'm stumped.

Anybody have any ideas?

+1  A: 

Module variables may (or may not) persist between requests (the same app instance may or may not stay alive between requests), but you can explicitly clear them (del, or set to None, say) at the start of your handling a request, or when you know you're done with one. At worst (if your code is peculiarly organized) you need to set some function to always execute at every request start, or at every request end.

Alex Martelli
A: 

use local list to store data and do a model.put at end of your request processing. save multiple db trips

dhaval
+1  A: 

What I usually do is just create a new attribute on the request object. However, I use django with AppEngine, so I'm not sure if there is anything different about the appengine webapp framework.

def view_handler(request):
    if hasattr(request, 'mycache'):
        request.mycache['counter'] += 1
    else:
        request.mycache = {'counter':1,}

    return HttpResponse("counter="+str(request.mycache["counter"]))
dar
A: 

If you're using the 'webapp' framework included with App Engine (or, actually, most other WSGI-baesd frameworks), a new RequestHandler is instantiated for each request. Thus, you can use class variables on your handler class to store per-request data.

Nick Johnson