I manually set the req.session.modified flag when I updated the value.
I've spent some time trying to think of an automatic way of determining
if the session was modified.

They key is save the state of the session at the beginning of the
request, and then compare it at the end to see if changes were made.

You should be able to use the copy.deepcopy method to save a "pristine"
version of the session, and then just do a comparison at the end.  If
different, save to the db.

So, in _get_session(self), on an AttributeError, add
self._original_session = copy.deepcopy(self._session_cache)

And then in process_reponse(), instead of if(modified), do
if(request.session._original_session != request.session._session)


Of course, the downside to doing this is doubling the data and
performing a deepcopy on every request (at least every one that uses
the session).  But I figure, ram speed is much faster than db speed, so
it might not be that significant.  perhaps a run-time config
SESSION_TRACK_CHANGES or something.

Reply via email to