Reputation: 1275
Recently we switched to memcached sessions from django's default DB sessions.
We've been using the contrib session model to remove session based on a session_key: https://github.com/django/django/blob/master/django/contrib/sessions/models.py
Session.objects.get(session_key=key).delete()
Once we switched to memcached sessions the above query raises a DoesNotExist exception.
Is Django's Session model usable with memcached sessions? If so, what is the solution?
Upvotes: 0
Views: 1182
Reputation: 55207
Django provides a django.contrib.sessions.backends.cache
session backend which is supposed to achieve what you need so long as the cache is configured properly.
As suggested by yourself, the Session
model is used by the db
backend. To use the cache
backend, you could do the following (so you keep using the API and make the code future proof).
from django.contrib.sessions.backends.cache import SessionStore
session = SessionStore(session_key)
session.delete()
That's how it's done in the Sessions middleware.
This method should work whatever the backend, which is likely a prefered behavior should you decide to move to another backend in the future.
Upvotes: 3
Reputation: 1275
mc = memcache.Client(settings.CACHE_BACKEND[:-1].split('//')[1].split(';'))
mc.delete(str(key))
Upvotes: 1