skip to navigation
skip to content

Not Logged In

django-cached_authentication_middleware 0.2.1

A drop in replacement for django's built in AuthenticationMiddleware that utilizes caching.

django-cached_authentication_middleware is a drop in replacement for django.contrib.auth’s built in AuthenticationMiddleware. It tries to populate request.user by fetching user data from cache before falling back to the database.


  • Install via pypi:

    pip install django-cached_authentication_middleware
  • Configure CACHES in django’s

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
            'LOCATION': '',
            'TIMEOUT': 36000,
  • Replace django.contrib.auth.middleware.AuthenticationMiddleware with cached_auth.Middleware in


And you’re done!

Cached Auth Preprocessor

Sometimes you want to preprocess to User instance before storing it into cache. cached_auth allows you to define settings.CACHED_AUTH_PREPROCESSOR, a callable that takes two arguments, user & request and returns a User instance.

A classic example of this would be to attach Profile data to User object so calling request.user.profile does not incur a database hit. Here’s how we can implement it.

def attach_profile(user, request):
    # Handle exception for user with no profile and AnonymousUser
    except (Profile.DoesNotExist, AttributeError):
    return user

# In

Running Tests

To run the test suite:

python tests/

To run the test suite with Django custom user (this will run only on Django 1.5):

python tests/


Version 0.2.1

  • Better Django 1.8 compatibility.

Version 0.2.0

  • Added support for Django 1.5’s customer user model

Version 0.1.1

  • Fixed an error where middleware tries to call “get_profile” on AnonymousUser

Version 0.1

  • Initial release
File Type Py Version Uploaded on Size
django-cached_authentication_middleware-0.2.1.tar.gz (md5) Source 2015-06-03 4KB
django_cached_authentication_middleware-0.2.1-py2-none-any.whl (md5) Python Wheel 2.7 2015-06-03 6KB
  • Downloads (All Versions):
  • 29 downloads in the last day
  • 331 downloads in the last week
  • 1212 downloads in the last month