Wednesday, November 30, 2011

Re: Cache timeout

On Sat, Nov 26, 2011 at 4:07 PM, Addy Yeow <ayeowch@gmail.com> wrote:
> If I use cache_page() decorator for my view function, will the TIMEOUT be
> able to take precedence over CACHE_MIDDLEWARE_SECONDS?
>
> In my settings.py,
>
> CACHES = {
>     'default': {
>         'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
>         'LOCATION': os.path.join(WEB_ROOT, "django_cache"),
>         'TIMEOUT': 5184000,
>     }
> }
> CACHE_MIDDLEWARE_SECONDS = 86400
>

TIMEOUT (from CACHES) is never used with the cache_page decorator.
TIMEOUT is only used when no timeout is specified when
adding/setting/updating an item in the cache, and cache_page always
does specify a timeout.

The cache_page decorator is a special invocation of the
CacheMiddleware, which is a mixin of both UpdateCacheMiddleware and
FetchFromCacheMiddleware, the former of which is documented as
follows:

* The number of seconds each page is stored for is set by the "max-age" section
of the response's "Cache-Control" header, falling back to the
CACHE_MIDDLEWARE_SECONDS setting if the section was not found.

The cache_page decorator adjusts this slightly, so that if a timeout
is specified within the tag, this is used instead of using
settings.CACHE_MIDDLEWARE_SECONDS as a default, but it will always use
'max-age' from the 'Cache-Control' header in preference to either of
these.

eg:

@cache_page
def foo():
# This will be cached for CACHE_MIDDLEWARE_SECONDS if no max-age

@cache_page(15 * 60)
def bar():
# This will be cached for 15 minutes if no max-age

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment