如何在django中实现每个请求的内存“缓存”值

时间:2013-03-12 15:48:55

标签: python django

我想存储数据库中的值,该值在请求/响应周期中不会发生变化,但会被使用数百次(可能数千次)。

e.g:

#somefile.py

def get_current_foo(request): # this gets called a lot and is currently a bottleneck
  foo = get_foo_from_db(request.blah)
  return foo

目前我使用memcached来存储值,但是这个东西被调用得足够多,即使使用memcached来存储值也是一个瓶颈(我在说话时对它进行分析)。有没有办法在当前请求/响应周期中“缓存”内存中的值?

(从Are python "global" (module) variables thread local?跟进)

1 个答案:

答案 0 :(得分:3)

如果是每个请求数据,请将其存储在请求对象本身上。 : - )

有关这方面的一些技巧,请参阅Per-request cache in Django?

相关问题