我在nginx服务器上使用uWSGI进程提供的Django应用程序。 此应用程序使用tastypie进行API管理,使用memcached缓存一些模板块。
我的问题是API请求不断返回旧结果。
我按日期过滤我的查询
queryset = Event.objects.filter(status='P').exclude(date_end__lt=date.today()).order_by('-featured', 'date_end')但返回的对象每天都是一样的。
我还将date.today添加到上下文中以进行调试,并正确输出当前日期。
当我重新启动uWSGI进程时,QuerySet被正确评估。
所以我从这个问题中排除了DB和memcached。 对我来说,似乎某种QuerySet缓存是由tastypie或uWSGI进程完成的。
我已经阅读了tastypie缓存文档并尝试了NoCache类但没有成功。
我还阅读了关于QuerySet缓存的Django文档但是不是每次请求后都应抛弃的QuerySet对象吗?
UPDATE
我检查了响应标头,客户端缓存在60秒后到期,最大年龄为一小时。
HTTP/1.1 200 OK Server: nginx/1.2.6 Date: Mon, 18 Feb 2013 10:47:03 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Last-Modified: Mon, 18 Feb 2013 10:44:56 GMT Expires: Mon, 18 Feb 2013 10:54:56 GMT Cache-Control: max-age=600UPDATE
我按照建议改变了我的查询
queryset = Event.objects.filter(status='P').exclude(date_end__lt=date.today).order_by('-featured', 'date_end')但结果仍然相同。
这是一个JSON输出示例
{ "date_begin": "11/17/2012", "date_end": "11/17/2012", "description": "Presentazione del libro di Daniela Giusto", "featured": false, "location": "Libreria antiquaria Romeo Prampolini", "resource_uri": "/api/v1/event/213/", "time": "18:00:00", "title": "Un insolito Jules Verne. Tradurre umorismo e fantasia", "today": "2012-11-18", }date_begin和date_end以不同的方式格式化以实现javascript兼容性。
I've a Django application served by uWSGI processes on an nginx server. This application uses tastypie for API management and memcached to cache some template blocks.
My problem is that API requests keep returning old results.
I'm filtering my query by date
queryset = Event.objects.filter(status='P').exclude(date_end__lt=date.today()).order_by('-featured', 'date_end')but the returned objects are the same every day.
I also added date.today to the context for debugging purpose and it correctly outputs the current date.
When I restart uWSGI processes the QuerySet is evaluated correctly.
So I exclude DB and memcached from this problem. To me it seems like some sort of QuerySet caching is being done by tastypie or uWSGI processes.
I've read tastypie caching documentation and tried the NoCache class with no success.
I've also read Django doc about QuerySet caching but aren't QuerySet objects supposed to be thrown away after each request?
UPDATE
I checked response headers and client cache expires after 60 seconds with a max-age of an hour.
HTTP/1.1 200 OK Server: nginx/1.2.6 Date: Mon, 18 Feb 2013 10:47:03 GMT Content-Type: application/json; charset=utf-8 Connection: keep-alive Last-Modified: Mon, 18 Feb 2013 10:44:56 GMT Expires: Mon, 18 Feb 2013 10:54:56 GMT Cache-Control: max-age=600UPDATE
I changed my query as suggested
queryset = Event.objects.filter(status='P').exclude(date_end__lt=date.today).order_by('-featured', 'date_end')but the result is still the same.
Here is a JSON output example
{ "date_begin": "11/17/2012", "date_end": "11/17/2012", "description": "Presentazione del libro di Daniela Giusto", "featured": false, "location": "Libreria antiquaria Romeo Prampolini", "resource_uri": "/api/v1/event/213/", "time": "18:00:00", "title": "Un insolito Jules Verne. Tradurre umorismo e fantasia", "today": "2012-11-18", }date_begin and date_end are formatted in a different way for javascript compatibility.
最满意答案
您的QuerySet对象在Tastypie中不是请求范围的; 它会持续存在于请求中。 因此,您的date.today仅评估一次(即使您将date.today函数作为参数而不是其返回值传递),而不是像您期望的那样按请求传递。 从Tastypie文档中查看此配方以获得修复。
Your QuerySet object is not request-scoped in Tastypie; it persists across requests. Thus your date.today is only evaluated once (even if you pass the date.today function as an argument instead of its return value), and not per request like you expect. Check out this recipe from the Tastypie docs for a fix.
更多推荐
发布评论