Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update invalidate_cache to accept user id #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cached_auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ def profile_preprocessor(user, request):
user_preprocessor = profile_preprocessor


def invalidate_cache(sender, instance, **kwargs):
if isinstance(instance, User):
key = CACHE_KEY % instance.id
def invalidate_cache(sender, user, **kwargs):
""" user should be User instance or user primary key """
if isinstance(user, User):
key = CACHE_KEY % user.id
else:
key = CACHE_KEY % instance.user_id
key = CACHE_KEY % user
cache.delete(key)


Expand All @@ -78,9 +79,6 @@ class Middleware(object):
def __init__(self):
post_save.connect(invalidate_cache, sender=User)
post_delete.connect(invalidate_cache, sender=User)
if profile_model:
post_save.connect(invalidate_cache, sender=profile_model)
post_delete.connect(invalidate_cache, sender=profile_model)

def process_request(self, request):
assert hasattr(request, 'session'), "The Django authentication middleware requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert 'django.contrib.sessions.middleware.SessionMiddleware'."
Expand Down