cached_db session backend, memcached and SESSION_COOKIE_AGE > 30 days

244 views
Skip to first unread message

Jose C

unread,
Dec 14, 2009, 8:33:37 PM12/14/09
to Django users
Using the 'django.contrib.sessions.backends.cached_db' and setting
SESSION_COOKIE_AGE to any number of seconds greater than 2592000 (i.e.
> 30 days) with memcached appears not to work as intended in that the
session info is never sent back from the cache.

Examples to illustrate:

Memcached output with SESSION_COOKIE_AGE <= 30 days (here 14 days):
<pre>
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 1209600 332
>288 STORED
<288 connection closed.
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 sending key 4c35074ef4038b16a2db4f728324206a
>288 END
<288 connection closed.
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 sending key 4c35074ef4038b16a2db4f728324206a
>288 END
</pre>

Works. After the initial store, the key is requested and sent as
expected.

Now, memcached output with SESSION_COOKIE_AGE > 30 days (here 31
days):
<pre>
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 2678400 334
>288 STORED
<288 connection closed.
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 2678400 334
>288 STORED
<288 connection closed.
</pre>

The key is initially stored, and each time it is requested, it doesn't
send the data but rather gets it from the db and keeps re-setting the
same key each time.

Memcached takes a seconds argument as expiry up to 30 days out.
Otherwise it appears to want a Unix timestamp to set the expiry date.
Looking at cached_db.py it passes the SESSION_COOKIE_AGE which is
always a number of seconds.

It looks like a bug but since both django and memcached are new to me
I wanted to check if anyone else can corroborate this before filing it
with the django folks.

FWIW, I tried patching the cache_db.py to:
"cache.set(self.session_key, self._session, int(time.time()) +
settings.SESSION_COOKIE_AGE)"
but just managed to get Django crashing after sending hundreds of
requests for the same key to memcached on a simple page load....
probably best if I leave the patching to the experts at this stage ;)

Jose C

unread,
Dec 18, 2009, 12:19:20 PM12/18/09
to Django users
In case anyone else hits this issue, there's a patch available:
http://code.djangoproject.com/ticket/12399
Reply all
Reply to author
Forward
0 new messages