如何实现每个请求内存中的“高速缓存”在django的价值 [英] How to implement per-request in-memory "cache" of value in django

查看:186
本文介绍了如何实现每个请求内存中的“高速缓存”在django的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在数据库中存储一个在请求/响应周期内不会改变的值,但是可以使用数百个(可能数千次)的数据。



例如:

 #somefile.py 

def get_current_foo(request):#这个被调用了很多,现在是瓶颈
foo = get_foo_from_db(request.blah)
return foo
/ pre>

目前我使用memcached来存储值,但是这个东西被称为足够的,即使使用memcached来存储值也是一个瓶颈(我将其分析为我们说)。有没有办法缓存当前请求/响应周期内存中的值?



(从是python全局(module)变量thread local?

解决方案

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



请参阅按请求缓存Django?,以获取一些技巧。


I'd like to store a value from the database that isn't going to change during a request/response cycle, but gets used hundreds (potentially thousands) of times.

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

Currently I use memcached to store the value, but this thing gets called enough that even using memcached to store the value is a bottleneck (I'm profiling it as we speak). Is there a way to "cache" the value in-memory for the current request/response cycle?

( follow up from Are python "global" (module) variables thread local? )

解决方案

If it is per-request data, store it on the request object itself. :-)

See Per-request cache in Django? for some techniques to do so.

这篇关于如何实现每个请求内存中的“高速缓存”在django的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆