How to implement per-request "cache" in value memory in django

I would like to save a value from a database that will not change during the request / response cycle, but will be used hundreds (potentially thousands) times.

eg:.

#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 

I am currently using memcached to store the value, but this thing is called enough that even using memcached to store the value is a bottleneck (I profile it when we talk). Is there a way to "cache" a value in memory for the current request / response cycle?

(follow Are python and global (?) variables local threads? )

+1
source share
1 answer

If this is request data, save it on the request object itself. :-)

See cache request in Django? for some methods for this.

+3
source

All Articles