﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
12399	memcached not working as expected when setting keys with a timeout > 30 days	houdinihound	nobody	"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 does not to work as intended in that the session info is never sent back from the cache.

Memcached takes an 'expire_in_seconds' argument which can be up to 2592000 (30 days).  For numbers greater than that it requires a Unix timestamp to set the expiry date.

Currently cached_db.py passes in settings.SESSION_COOKIE_AGE which is always a number of seconds. Consequently this works up to 30 days (2592000 seconds) but if SESSION_COOKIE_AGE is greater than that it does not.

To illustrate:
Memcached output with SESSION_COOKIE_AGE = 2592000 (i.e. 30 days): 
{{{
<280 server listening (udp)
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 2592000 334
>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
}}}

Works.  After the initial store, the key is requested and sent as expected. 

Now with SESSION_COOKIE_AGE = 2592001 (i.e. 30 days + 1 second): 
{{{
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 2592001 337
>288 STORED
<288 connection closed.
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 2592001 337
>288 STORED
<288 connection closed.
<288 new client connection
<288 get 4c35074ef4038b16a2db4f728324206a
>288 END
<288 set 4c35074ef4038b16a2db4f728324206a 1 2592001 337
>288 STORED
<288 connection closed.
}}}
The key is initially stored, but 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. 

I've patched `load` and `save` in cached_db.py to:[[BR]]
1) Check if memcached is being used[[BR]]
2) If so, check if settings.SESSION_COOKIE_AGE > 2592000[[BR]]
3) If so, add settings.SESSION_COOKIE_AGE to the unix timestamp (time.time()) and pass that to memcached.

which now works as expected when SESSION_COOKIE_AGE > 2592000:
{{{
<292 new client connection
<292 get 4c35074ef4038b16a2db4f728324206a
>292 END
<292 set 4c35074ef4038b16a2db4f728324206a 1 1263689307 337
>292 STORED
<292 connection closed.
<292 new client connection
<292 get 4c35074ef4038b16a2db4f728324206a
>292 sending key 4c35074ef4038b16a2db4f728324206a
>292 END
<292 connection closed.
<292 new client connection
<292 get 4c35074ef4038b16a2db4f728324206a
>292 sending key 4c35074ef4038b16a2db4f728324206a
>292 END
<292 connection closed.
}}}

"		closed	Core (Cache system)	dev		fixed	cached_db session memcached		Ready for checkin	1	0	0	0	0	0
