开发者

Django cache_page checking

How can I confirm my Django views are being cached when I use the cache_page decorator like so:

@cache_page(60)
def my_view(request):

Ideally I would like to output cache hit/miss messages in the console so I can confirm my view is being cached for 60 seconds etc.

Many 开发者_运维技巧thanks, g


you could grab a copy of django-debug-toolbar (http://github.com/robhudson/django-debug-toolbar) and watch the queries: if the page is not being pulled from the cache, django-debug-toolbar should display all of the queries required to assemble your page. if the page is pulled from the cache, you won't see the queries.

you could also add logging to the particular cache wrapper that you're using, and then reference the output in django-debug-toolbar's "logging" panel. heres an example of what that would look like: http://gist.github.com/242011

i'd also recommend grabbing a copy of django-memcache-status (http://github.com/bartTC/django-memcache-status) and memcache-top (http://code.google.com/p/memcache-top/), if you're interested in monitoring memcache usage in detail.


You can confirm that your page is cached by looking at the headers in the HTTP response.

$ curl -v http://localhost:8000/cached_view/ >/dev/null
[...]
< Cache-Control: max-age=900
< Expires: Tue, 02 Jul 2019 18:36:34 GMT
[...]

Both Cache-Control and Expires mean that there was a cache involved while generating the response.


Depending on the cache middleware you are using, you could inspect the process_request method of that class and find some line like these (taken from django/middleware/cache.py)

131  response = cache.get(cache_key, None)
132  if response is None:
133      ...

...      logging.debug("Cache miss")
...
...  else:
...      logging.debug("Cache hit")

and log the messages from there. I do admit, it is not a clean way to do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜