Skip to content

Drop-in replacement for django.contrib.auth.middleware.AuthenticationMiddleware, with cached request.user

License

Notifications You must be signed in to change notification settings

SectorLabs/django-cached-auth-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

<SectorLabs>

Drop-in replacement for django.contrib.auth.middleware.AuthenticationMiddleware with cached request.user

While this middleware will work with any type of cache, it only makes sense to use it with caches that are faster than FileSystem or DB (e.g. in-memory caches such as Redis or Memcached).

When using this middleware, the code will not raise any exceptions if it fails to get/set/delete from the cache, but it will just fallback to the DB.

This plugin will use the SESSION_CACHE_ALIAS cache to cache the users.

Usage

Replace django.contrib.auth.middleware.AuthenticationMiddleware with django_cached_auth_middleware.middleware.CachedAuthenticationMiddleware in MIDDLEWARE.

-    "django.contrib.auth.middleware.AuthenticationMiddleware",
+    "django_cached_auth_middleware.middleware.CachedAuthenticationMiddleware",

Set DJANGO_CACHED_AUTH_TIMEOUT_SECONDS to the amount of seconds you want the sessions to be stored. If this env var is not set, the sessions will be stored forever.

Set DJANGO_CACHED_AUTH_ENABLED to False, if you want to fallback to the django.contrib.auth.middleware.AuthenticationMiddleware functionality.

Inspired from django-cached_authentication_middleware

About

Drop-in replacement for django.contrib.auth.middleware.AuthenticationMiddleware, with cached request.user

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages