[Answered ]-How can i find django user from cached backend session

2đź‘Ť

âś…

If your SESSION_ENGINE is set to:

  • “django.contrib.sessions.backends.cached_db”: Combining usage of cache and database, you can lookup that session from database.

  • “django.contrib.sessions.backends.cache” : Cache only; No persistence, you will have to look up that session from cache but fast enough before cached data can be evicted if the cache fills up or if the cache server is restarted. Here you will have to lookup the session when the error occurs, e.g you will need to handle the exception caused by that error, at that point, you can lookup for the user using the session or from request and store the user in the log.

As you said “These are same in many emails so i think the user is one” so it possible when you get the e-mail that the session is still living in the cache ! So look for it in the cache, if for example the session is identified (key) with its “sessionid” in the cache try to get it using “sessionid” as the key.

from django.core.cache import cache
session = cache.get(sessionid)

Then get the user:

from django.contrib.auth.models import User
data = session.get_decoded()
uid = data.get('_auth_user_id', None)
user = User.objects.get(id = uid)

This is just a hint, I’m not sure if it will work coz I don’t have the same settings as yours.

Leave a comment