Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

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

Project Description
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.


|Build Status|

* Install via pypi::

pip install django-cached_authentication_middleware

* Configure ``CACHES`` in django's ````::

'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
'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.

.. code-block:: python

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.2 (2017-04-29)
* Added compatibility with Django 1.11. Thanks @ondrejsika!

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

.. |Build Status| image::
Release History

Release History

This version
History Node


History Node


History Node


History Node


History Node


Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django_cached_authentication_middleware-0.2.2-py2-none-any.whl (6.6 kB) Copy SHA256 Checksum SHA256 2.7 Wheel Apr 29, 2017
django-cached_authentication_middleware-0.2.2.tar.gz (4.6 kB) Copy SHA256 Checksum SHA256 Source Apr 29, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting