Wednesday, November 30, 2011

Re: Cache timeout

On Sat, Nov 26, 2011 at 4:07 PM, Addy Yeow <> 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,
> CACHES = {
>     'default': {
>         'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
>         'LOCATION': os.path.join(WEB_ROOT, "django_cache"),
>         'TIMEOUT': 5184000,
>     }
> }

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

* 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


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



You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment