I'm currently running a Django application with SESSION_ENGINE configured as user_sessions.backends.db
(I'm using this external library). Django's own built-in sessions are great, however all the data is hidden away into base64 encoded data. This library makes session objects accessible like other ORM objects.
I need to remove my dependency on this library; changing the SESSION ENGINE to cache to boost performance.
My challenge is making this change without destroying the current session data, and that's what this question is about. Given its nature, the library naturally doesn't include a backend other than db
, so I'll have to extend it.
How should I approach this problem? E.g., in my head: i) I ought to look at the contents of the db table(s) this library saves session data to, ii) write functionality that saves this data to cache memory at each write, iii) shift over to reading from cache (with db as a fall-back).
Does that sound about right? Would love to know any dont's at this point in time, since this is a rather complex task. Thanks in advance.
The best way to do this, in my opinion, is to migrate all your current session data to the native Django Session framework by just re-serializing all the session data, keeping the same session ID.
First, I'd figure out exactly how the Session framework serializes the data. For instance, on Django 1.10.4, here's what you should be looking for: django/contrib/sessions/backends/base.py#L96.
Then, I would write a converter that reads the current session data from the database and converts it to the native one.
I'm not sure how the sessions IDs work, but it looks like they use the same kind of id, so it should be possible to just to this and then change the SESSION_ENGINE
and MIDDLEWARE_CLASSES
back to the Django default's and it should work.