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

2024-05-02 17:15:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望从数据库中存储一个值,该值在请求/响应周期内不会更改,但会被使用数百次(可能数千次)。

例如:

#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?


Tags: andpy数据库getfooisrequestdef