开发者

what's differece between the django's cache ?the The per-site cache,The per-view cache,Specifying per-view cache and Template fragment caching

i know the django has some cache method such as the The per-site cache,The per-view cache,Specifying per-view cache and Template fragment caching but what's differece between those cache ? the per-site cache means the cache 开发者_开发问答system cache the whole site ? how can i understand the words 'cache the whole site'?


I think the docs do a great job of describing this but I will paste it here and give a little description as well.

Cache the whole site means django will attempt to cache every view you've set up via middleware.

Per site cache docs
http://docs.djangoproject.com/en/dev/topics/cache/#the-per-site-cache

Once the cache is set up, the simplest way to use caching is to cache your entire site.

Basically, it's a set of middleware so it caches all views. Middleware is applied to every request/response.


Per view cache docs
http://docs.djangoproject.com/en/dev/topics/cache/#the-per-view-cache

A more granular way to use the caching framework is by caching the output of individual views. django.views.decorators.cache defines a cache_page decorator that will automatically cache the view's response for you.

This is a per view cache. You can decide to cache a certain view by applying the @cache_page decorator to a specific view (as opposed to ALL views above)


Template fragment cache docs
http://docs.djangoproject.com/en/dev/topics/cache/#template-fragment-caching

The {% cache %} template tag caches the contents of the block for a given amount of time.

This lets you cache chunks of your template (as opposed to an entire view above), so for example you could cache an expensive query in your template while other pieces of the site are still served dynamically.

This would be useful if certain pieces can't be cached.. for example the classic problem of displaying a logged in user at the top wouldn't be possible with a per view cache, as the user would need to be updated and thus the cache invalidated.


If you read further in the docs, you'll reach the cache api as well:

Cache api docs
http://docs.djangoproject.com/en/dev/topics/cache/#the-low-level-cache-api

This is basically how you get control over the cache in python code (as opposed to the template, above).

# imagine you have a function that takes a day to complete.
cache.set('very_expensive_homepage_logic', takes_a_day_to_calculate())

# now if you call get() with your key, it will be returned and you won't have to wait a day to calculate.
cache.get('very_expensive_homepage_logic')
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜