-
13
Sep
The Seagull framework which we love to use for our projects offers two built-in methods for session management: files and database.
In our podcast project we started out using the file based sessions years ago. A while back we switched to so-called extended sessions which are saved in the database. Not so long ago we switched back to files again as the requests just for the sessions to the database became a serious bottleneck in our installation.
We did a relaunch of our podcast service beginning of September with sessions still stored to in files. This might have worked well if we had not switched to serving the website’s resources including the sessions through a high available NFS4 server. The server with the active NFS4 export hit its maximum capacity randomly making the service unusable.
I could have tried to switch back to the database handler as we have new, much more powerful machines. But I did not even bother as I assumed I’d eventually run into the same problems as before. Instead I researched sessions saved in memory. I knew that PHP offers shared-memory sessions. I tried that a while ago with no luck. During my research I came across memcached sessions. As I am already using memcache to store objects in the application I thought this would be ideal. And as it turned out today when applying the following changes to our live system it is!
To make Seagull work with memcached sessions only two minor changes to the code base had to be made. In the SGL core library Session.php change the constructor as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | if ($conf['session']['handler'] == 'database') { $ok = session_set_save_handler( array(& $this, 'dbOpen'), array(& $this, 'dbClose'), array(& $this, 'dbRead'), array(& $this, 'dbWrite'), array(& $this, 'dbDestroy'), array(& $this, 'dbGc') ); } elseif ($conf['session']['handler'] == 'memcache') { session_save_path($conf['session']['save_path']); } else { session_save_path(SGL_TMP_DIR); } |
The second change is in the _init function:
1 2 3 4 5 6 7 8 9 10 11 12 13 | if ($conf['session']['handler'] == 'file') { // manually remove old session file, see http://ilia.ws/archives/47-session_regenerate_id-Improvement.html $ok = @unlink(SGL_TMP_DIR . '/sess_'.$oldSessionId); } elseif ($conf['session']['handler'] == 'database') { $value = $this->dbRead($oldSessionId); $this->dbDestroy($oldSessionId); $this->dbRead(session_id()); // creates new session record $this->dbWrite(session_id(), $value); // store old session value in new session record } elseif ($conf['session']['handler'] == 'memcache') { // do nothing - just do not complain or fail } else { die('Internal Error: unknown session handler'); } |
So just add the lines with memcache and below. That’s it!
To make Seagull use the memcache session handler adjust your config accordingly, e.g. my local one looks like the following:
1 2 | $conf['session']['handler'] = 'memcache'; $conf['session']['save_path'] = 'tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15'; |
Live we use several memcache servers which you can address with a comma separated list of servers, e.g.
1 2 | $conf['session']['handler'] = 'memcache'; $conf['session']['save_path'] = 'tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15,tcp://127.0.0.1:11212?persistent=1&weight=2&timeout=1&retry_interval=50'; |
Make sure your memcache server(s) listen(s) on the correct IP address and port. Otherwise you will get a blank screen and/or a nasty error message.
Now you should be ready to go. Experience a never before known speed of your PHP applcation!
- Veröffentlicht von Fabio Bacigalupo in: Howto Software Tricks
- Wenn Sie diese Seite mögen, abonnieren Sie doch unseren RSS-Feed!


Ein Kommentar to “Seagull Framework with memcached sessions”
Hi,
Came to Thanks again…
I will need to revisit and use the Instructions provided here..
@rungss on Twitter
Hinterlasse eine Antwort