I am trying to cache query results on my django app. However, it seems that it is caching the whole app. I tried following logi:
我试图在我的django应用程序上缓存查询结果。但是,似乎它正在缓存整个应用程序。我试过跟随logi:
def cacheView():
result = cache.get('key')
if result is None:
result = Model.objects.get(id=1)
cache.set('key', 'result')
I am calling this method when user logs in. However, if I try to logout after login, it keeps me on same page as if I am still logged in. I tried to follow the Django documentation on cache at http://docs.djangoproject.com/en/1.2/topics/cache/ but to no success.
我在用户登录时调用此方法。但是,如果我在登录后尝试注销,它会让我保持在同一页面,就像我仍然登录一样。我试图在http:// docs上关注缓存上的Django文档。 djangoproject.com/en/1.2/topics/cache/但没有成功。
Another thing I tried is that if I try to use the cache decorator just above the view as:
我尝试的另一件事是,如果我尝试在视图上方使用缓存装饰器:
@cache_control(max_age=1000)
def cacheView():
...
it does it gives an error saying "response header not defined". I am new to django and sure that I missed something. Any idea?
它会给出一个错误,说“响应标头未定义”。我是django的新手,确信我错过了一些东西。任何想法?
2 个解决方案
#1
8
RTFM :) Official Django Docs: Caching and QuerySets
RTFM :)官方Django文档:缓存和查询集
Each QuerySet contains a cache, to minimize database access. (...)
每个QuerySet都包含一个缓存,以最大限度地减少数据库访(......)
and:
和:
In a newly created QuerySet, the cache is empty. The first time a QuerySet is evaluated -- and, hence, a database query happens -- Django saves the query results in the QuerySet's cache and returns the results that have been explicitly requested (e.g., the next element, if the QuerySet is being iterated over). Subsequent evaluations of the QuerySet reuse the cached results.
在新创建的QuerySet中,缓存为空。第一次评估QuerySet - 并因此发生数据库查询 - Django将查询结果保存在QuerySet的缓存中并返回已明确请求的结果(例如,如果正在迭代QuerySet,则返回下一个元素)过度)。 QuerySet的后续评估重用缓存的结果。
Caching is done automagically in case of QuerySets (results of Queries).
在QuerySets(查询结果)的情况下,缓存是自动完成的。
EDIT: As for your code pasted in the question. If the key doesn't already exist in cache you have to create it with add()
method but remember that it will expire by default after 30sec. If you want it to be kept longer you have to add timeout option to add()/set()
method.
编辑:至于您在问题中粘贴的代码。如果密钥尚未存在于缓存中,则必须使用add()方法创建密钥,但请记住它将在30秒后默认过期。如果希望它保持更长时间,则必须为add()/ set()方法添加超时选项。
If you want to cache for whole your site (to ie. decorators as you used them), you need to add proper middleware to your MIDDLEWARE_CLASSES
in settings.py (in this exact order, because middleware order matters, it is loaded one by one as you define them):
如果你想缓存整个你的站点(就像你使用它们那样的装饰器),你需要在settings.py中的MIDDLEWARE_CLASSES中添加适当的中间件(按照这个确切的顺序,因为中间件顺序很重要,它是逐个加载的当你定义它们时):
MIDDLEWARE_CLASSES = (
# ...
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
# ...
)
If you don't have them, then you'll be receiving bad header errors every time you'll use caching-per-site capabilities.
如果您没有它们,那么每次使用每站点缓存功能时,您将收到错误的标头错误。
#2
1
From your example it's not clear why the logout would fail, but it shouldn't be anything to do with caching a model (unless you're caching the User model and using the cached user for authentication instead of request.user?)
从您的示例中不清楚为什么注销会失败,但它不应该与缓存模型有任何关系(除非您缓存User模型并使用缓存用户进行身份验证而不是request.user?)
It's fine to use cache.get and cache.set the way you are (set will create a new key if it doesn't exist).
可以按照您的方式使用cache.get和cache.set(如果不存在则设置将创建新密钥)。
Caching queries can be difficult as you need to take care of invalidating the cache when data changes so as not to serve stale results.
缓存查询可能很困难,因为您需要在数据更改时处理缓存失效,以免提供过时的结果。
Have a look at these query-caching libraries for Django which aim to make things easier:
看看Django的这些查询缓存库,旨在简化操作:
http://jbalogh.me/2010/02/09/cache-machine/
http://jbalogh.me/2010/02/09/cache-machine/
http://packages.python.org/johnny-cache/queryset_cache.html
http://packages.python.org/johnny-cache/queryset_cache.html
#1
8
RTFM :) Official Django Docs: Caching and QuerySets
RTFM :)官方Django文档:缓存和查询集
Each QuerySet contains a cache, to minimize database access. (...)
每个QuerySet都包含一个缓存,以最大限度地减少数据库访(......)
and:
和:
In a newly created QuerySet, the cache is empty. The first time a QuerySet is evaluated -- and, hence, a database query happens -- Django saves the query results in the QuerySet's cache and returns the results that have been explicitly requested (e.g., the next element, if the QuerySet is being iterated over). Subsequent evaluations of the QuerySet reuse the cached results.
在新创建的QuerySet中,缓存为空。第一次评估QuerySet - 并因此发生数据库查询 - Django将查询结果保存在QuerySet的缓存中并返回已明确请求的结果(例如,如果正在迭代QuerySet,则返回下一个元素)过度)。 QuerySet的后续评估重用缓存的结果。
Caching is done automagically in case of QuerySets (results of Queries).
在QuerySets(查询结果)的情况下,缓存是自动完成的。
EDIT: As for your code pasted in the question. If the key doesn't already exist in cache you have to create it with add()
method but remember that it will expire by default after 30sec. If you want it to be kept longer you have to add timeout option to add()/set()
method.
编辑:至于您在问题中粘贴的代码。如果密钥尚未存在于缓存中,则必须使用add()方法创建密钥,但请记住它将在30秒后默认过期。如果希望它保持更长时间,则必须为add()/ set()方法添加超时选项。
If you want to cache for whole your site (to ie. decorators as you used them), you need to add proper middleware to your MIDDLEWARE_CLASSES
in settings.py (in this exact order, because middleware order matters, it is loaded one by one as you define them):
如果你想缓存整个你的站点(就像你使用它们那样的装饰器),你需要在settings.py中的MIDDLEWARE_CLASSES中添加适当的中间件(按照这个确切的顺序,因为中间件顺序很重要,它是逐个加载的当你定义它们时):
MIDDLEWARE_CLASSES = (
# ...
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
# ...
)
If you don't have them, then you'll be receiving bad header errors every time you'll use caching-per-site capabilities.
如果您没有它们,那么每次使用每站点缓存功能时,您将收到错误的标头错误。
#2
1
From your example it's not clear why the logout would fail, but it shouldn't be anything to do with caching a model (unless you're caching the User model and using the cached user for authentication instead of request.user?)
从您的示例中不清楚为什么注销会失败,但它不应该与缓存模型有任何关系(除非您缓存User模型并使用缓存用户进行身份验证而不是request.user?)
It's fine to use cache.get and cache.set the way you are (set will create a new key if it doesn't exist).
可以按照您的方式使用cache.get和cache.set(如果不存在则设置将创建新密钥)。
Caching queries can be difficult as you need to take care of invalidating the cache when data changes so as not to serve stale results.
缓存查询可能很困难,因为您需要在数据更改时处理缓存失效,以免提供过时的结果。
Have a look at these query-caching libraries for Django which aim to make things easier:
看看Django的这些查询缓存库,旨在简化操作:
http://jbalogh.me/2010/02/09/cache-machine/
http://jbalogh.me/2010/02/09/cache-machine/
http://packages.python.org/johnny-cache/queryset_cache.html
http://packages.python.org/johnny-cache/queryset_cache.html