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?
Django provides a
django.contrib.sessions.backends.cachesession backend which is supposed to achieve what you need so long as the cache is configured properly.As suggested by yourself, the
Sessionmodel is used by thedbbackend. To use thecachebackend, you could do the following (so you keep using the API and make the code future proof).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.