Code to store PHP sessions within memcached.

Reinis Rozitis roze at roze.lv
Fri Nov 17 16:06:49 UTC 2006


You dont need to keep that in mind if you use memcache as session store, it 
doesnt have the same restrictions as with file handles cause you practically 
cant lock a memcache data item/key.

You can make a simple test to see that this is true:

longscript.php:
<?
session_id('blabla');
// there goes the previous session_set_save_handler() memcache functions
session_start();
$_SESSION['test'] = 'some data';
sleep(100);
?>

short.php:
<?
session_id('blabla');
// there goes the previous session_set_save_handler() memcache funcctions
session_start();
print_r($_SESSION);
?>

then you open the first and in the same time second script.

You'll see that short.php will finish instantly (not in the default file 
storage case). Although of course short.php will get the 'test' variable 
only after 100 seconds (as the write is done at the end of the script if you 
dont do the session_write_close manually) but thats only important only in 
some cases (in which you wont use ajax requests anyway). So there is no need 
to bother that one ajax request takes more time to complete cause the whole 
session wont be blocked.

rr


----- Original Message ----- 
From: "Mauro N. Infantino" <mauroi at digbang.com>
To: "'Reinis Rozitis'" <roze at roze.lv>
Sent: Friday, November 17, 2006 2:58 PM
Subject: RE: Code to store PHP sessions within memcached.


> Keep in mind that the standard php session module locks the whole session
> until the request finishes (or the user calls session_write_close) [1]. 
> The
> supplied could lead to problems when using frames or ajax (Is not thaaat
> transparent. Even when not using frames it could happen, but it's less
> probable). I could send the code implementing locking using memcache's add
> method, if you want.
>
> Regards,
> Mauro.
>
> [1] Take a look at
> http://thwartedefforts.org/2006/11/11/race-conditions-with-ajax-and-php-sess
> ions/
> Under "Observing the Race Condition"




More information about the memcached mailing list