Skip to main content

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.

Installation

Build Status

  • 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

Project details


Download files

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

Source Distribution

Built Distribution

File details

Details for the file django-cached_authentication_middleware-0.2.1.tar.gz.

File metadata

File hashes

Hashes for django-cached_authentication_middleware-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7b71ec566b2a2d2666dfe62d2d404c194b1b15ab01103dd766895c30b0a3774c
MD5 5807f5965114a3573a441d87c30511c6
BLAKE2b-256 832cdd829201c3f57b650325b0f7b72c59333eee903a9776fba272c022e99640

See more details on using hashes here.

File details

Details for the file django_cached_authentication_middleware-0.2.1-py2-none-any.whl.

File metadata

File hashes

Hashes for django_cached_authentication_middleware-0.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 6d2546f562d074f1e73246a9cd24302700d81373ab9e1ec8035d2ff5ec81e15d
MD5 731ea44b7f497bc86c1c5aa03946452f
BLAKE2b-256 1e74b00448f2ec5cee5033fcf5f18552ef72232b9574b19743b0e69aa115afee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page