Memcached PHP Sessions

Victor Gumayunov gva at
Mon Feb 5 06:47:45 UTC 2007

Hi, Steven!

Sorry, last time I didn't have enough time to explain everything in details.

Having 0 as stored element's expire time doesn't allow us to track easy 
when this value expires in its context.
For our case, we store there sessions, and they should expire after some 
time of inactivity.

We need a cache obeying foollowing rules:
1. it always have memory to store all necessary values (indeed we have 
enough for our project)
2. values must remain in cache forever (what our patch does)
3. not used values must be unavailable after certain period of 
inactivity (what 0 expire time cannot do).

Of course, we can do that by embedding session expire time into stored 
structure, but this requires additional
update after each read. Yes, we can optimize it (refresh expire time 
once per minute for example) but we
have a several different clients - php scripts and a couple of perl 
daemons, so I want interaction with memcached
be the same in all languages and as simple as possible.

> If you want keys to not expire based on time, just don't set a 
> specific expiration time on them; there is no need to patch the server 
> to accomplish that. The default expiration time, the one you get if 
> you pass the server a 0 for the expiration value in a "set" command, 
> is "never."
> However, neither your patch nor the above actually solves the 
> fundamental problem with using memcached as a session store. The 
> actual problem is that, for example, you might have 64MB devoted to 
> memcached, but you can get a surge in usage and need to store 65MB 
> worth of session data. Memcached is an LRU cache and will expire the 
> oldest 1MB of data in that case, regardless of its expiration time.
> There are two answers to that: provision enough cache memory to handle 
> any projected usage surge (in which case you can just set your keys to 
> not expire and you're done, no patch required) or use some other 
> storage mechanism as a backing store for your session data and treat 
> memcached as, well, a cache (in which case, again, no patch required.)
> -Steve

More information about the memcached mailing list