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.
我正在为谷歌应用程序引擎创建一个python应用程序,我遇到了一些性能问题,一些昂贵的操作在一个请求中重复。为了帮助解决这个问题,我想创建一种专门针对单个请求的小型缓存。这与会话范围或应用程序范围的缓存相反,对我的特定问题都没有意义。
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.
我认为我可以为此使用python全局或模块级变量,但事实证明,那些以非显而易见的方式在请求之间维护它们的状态。
I also don't think memcache makes sense because it's application wide.
我也不认为memcache有意义,因为它的应用范围很广。
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?
有人有什么想法吗?
4 个解决方案
#1
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.
我通常只是在请求对象上创建一个新属性。但是,我将django与AppEngine一起使用,所以我不确定appengine webapp框架是否有任何不同之处。
def view_handler(request):
if hasattr(request, 'mycache'):
request.mycache['counter'] += 1
else:
request.mycache = {'counter':1,}
return HttpResponse("counter="+str(request.mycache["counter"]))
#2
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.
如果您正在使用App Engine附带的“webapp”框架(或实际上,大多数其他WSGI-baesd框架),则会为每个请求实例化一个新的RequestHandler。因此,您可以在处理程序类上使用类变量来存储每个请求数据。
#3
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.
模块变量可能(或可能不)在请求之间保持(相同的应用程序实例可能会或可能不会在请求之间保持活动状态),但您可以在处理请求的开始时明确清除它们(del,或设置为None) ,或者当你知道自己完成了一个。在最坏的情况下(如果您的代码是特殊组织的),您需要设置一些函数以始终在每个请求开始时执行,或者在每个请求结束时执行。
#4
use local list to store data and do a model.put at end of your request processing. save multiple db trips
使用本地列表存储数据并在请求处理结束时执行model.put。保存多个数据库之旅
#1
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.
我通常只是在请求对象上创建一个新属性。但是,我将django与AppEngine一起使用,所以我不确定appengine webapp框架是否有任何不同之处。
def view_handler(request):
if hasattr(request, 'mycache'):
request.mycache['counter'] += 1
else:
request.mycache = {'counter':1,}
return HttpResponse("counter="+str(request.mycache["counter"]))
#2
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.
如果您正在使用App Engine附带的“webapp”框架(或实际上,大多数其他WSGI-baesd框架),则会为每个请求实例化一个新的RequestHandler。因此,您可以在处理程序类上使用类变量来存储每个请求数据。
#3
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.
模块变量可能(或可能不)在请求之间保持(相同的应用程序实例可能会或可能不会在请求之间保持活动状态),但您可以在处理请求的开始时明确清除它们(del,或设置为None) ,或者当你知道自己完成了一个。在最坏的情况下(如果您的代码是特殊组织的),您需要设置一些函数以始终在每个请求开始时执行,或者在每个请求结束时执行。
#4
use local list to store data and do a model.put at end of your request processing. save multiple db trips
使用本地列表存储数据并在请求处理结束时执行model.put。保存多个数据库之旅