3

I'm having problems issues using the Django caching middleware. I want to cache an expensive page so that it does not need to be regenerated for each individual visitor.

It seems that SessionMiddleware is setting "Vary: Cookie" in the response header. This instructs the caching middleware to include the cookie when building the cache key, which means that caches are not global across user sessions.

My MIDDLEWARE_CLASSES setting is:

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.http.ConditionalGetMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'users.middleware.ConcurrentUserSessionMiddleware',
    'session_security.middleware.SessionSecurityMiddleware',
)

Moving SessionMiddleware before UpdateCacheMiddleware seems to fix the problem. However, this SO question indicates that that's the incorrect order: Practical rules for Django MiddleWare ordering?

Any ideas what I am doing wrong?

Community
  • 1
  • 1
aco
  • 719
  • 2
  • 9
  • 26

0 Answers0