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.

Installation

  • Install via pypi:

    pip install django-cached_authentication_middleware
    
  • Configure CACHES in django's settings.py:

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

    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        #'django.contrib.auth.middleware.AuthenticationMiddleware'
        'cached_auth.Middleware',
    )
    

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):
    try:
        user.get_profile()
    # Handle exception for user with no profile and AnonymousUser
    except (Profile.DoesNotExist, AttributeError):
        pass
    return user


# In settings.py:
CACHED_AUTH_PREPROCESSOR = 'path.to.module.attach_profile'

Running Tests

To run the test suite:

python tests/runtests.py

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

python tests/runtests_custom_user.py

Changelog

Version 0.2.1

  • Better Django 1.8 compatibility.

Version 0.2.0

  • Added support for Django 1.5's customer user model
  • Added CACHED_AUTH_PREPROCESSOR setting

Version 0.1.1

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

Version 0.1

  • Initial release
MIT

Author

Selwin Ong

Pip

django-cached_authentication_middleware==0.2.1

Classifiers

  • Environment :: Web Environment
  • Framework :: Django
  • Intended Audience :: Developers
  • License :: OSI Approved :: MIT License
  • Operating System :: OS Independent
  • Programming Language :: Python
  • Programming Language :: Python :: 2
  • Programming Language :: Python :: 2.6
  • Programming Language :: Python :: 2.7
  • Programming Language :: Python :: 3
  • Programming Language :: Python :: 3.3
  • Development Status :: 4 - Beta
  • Programming Language :: Python :: 3.4
  • Topic :: Software Development :: Libraries :: Python Modules
  • Programming Language :: Python :: Implementation :: CPython
  • Programming Language :: Python :: Implementation :: PyPy
  • Topic :: Internet :: WWW/HTTP
File Type Python Version Uploaded On Downloads
django_cached_authentication_middleware-0.2.1-py2-none-any.whl Wheel 2.7 June 3, 2015 845
django-cached_authentication_middleware-0.2.1.tar.gz Source June 3, 2015 1,344
Version Release Date
0.2.1 June 3, 2015
0.2.0 April 19, 2013
0.1.1 Sept. 7, 2012
0.1.0 Sept. 6, 2012
Date Package Version Action
Aug. 24, 2015, 10:59 p.m. django-cached_authentication_middleware 0.2.0 Release Created
Aug. 24, 2015, 10:59 p.m. django-cached_authentication_middleware 0.2.1 Release Created
Aug. 24, 2015, 10:59 p.m. django-cached_authentication_middleware 0.1.1 Release Created
Aug. 24, 2015, 10:59 p.m. django-cached_authentication_middleware 0.1.0 Release Created
Aug. 24, 2015, 10:59 p.m. django-cached_authentication_middleware Package Created