Code to store PHP sessions within memcached.

Reinis Rozitis roze at roze.lv
Fri Nov 17 08:06:23 UTC 2006


Hmm why dont you just use the nice feature of php to have your own session 
handling functions override the default which could be transparent to the 
rest of the code?
We use something like this:

<?
function mc_client() {
 $smc =& $GLOBALS['smc'];
 if (!is_object($smc)) {
  $smc = new Memcache;
  $smc->connect('127.0.0.1',11211);
   // and the rest of the servers you need with memcache_add_server()
 }
 return $smc;
}

function mc_ses_open($path, $name) { return true; }
function mc_ses_close() { return true; }

function mc_ses_read($id) {
 $client = mc_client();
 return $client->get($id);
}

function mc_ses_write($id,$data) {
 $client = mc_client();
 $client->set($id,$data,0,6000);
}

function mc_ses_destroy($id) {
 $client = mc_client();
 $client->delete($id);
 return true;
}
function mc_ses_gc($maxlt) { return true; }
session_set_save_handler('mc_ses_open','mc_ses_close','mc_ses_read','mc_ses_write','mc_ses_destroy','mc_ses_gc');
?>


And then simply session_start() and $_SESSION['blabla'] = 'value' and thats 
all.. (you dont need to stick with cookies cause that is handled by default 
from php side)

Tested on 30k concurent users/sessions online with 7 switching (roundrobin) 
www nodes and it works just fine..
The conclusion we got its best to keep some seperate Memcached instance just 
for the sessions that way you dont mix it with other type of data caches and 
because of LRU push the needed items out.

rr



----- Original Message ----- 
From: Kevin Burton
To: Memcached list
Sent: Friday, November 17, 2006 7:30 AM
Subject: Code to store PHP sessions within memcached.


Someone wanted the PHP code were were using to store our sessions within 
memcached.

Well here you go.

replace tailrank_ to something less proprietary.... and you'll need your own 
version of tailrank_memcache_get_instance to get a copy of your memcached 
object.

you to call session_start() at the beginning of your script and 
session_commit() at the end.

you'll also need to change the domain from tailrank.com to your domain 
(obviously)

If you make any changes or fix any bugs please let me know.

This is hereby licensed under the GPL, BSD, and public domain so have fun! 
:)

<?php

/*
 * Send mail but also include the From header.
 */
function tailrank_session_start() {

    //For machines that already have this session loaded this will merely be
    //redundant....

    global $_SESSION;
    $my_memcache = tailrank_memcache_get_instance();

    $cookie = $_COOKIE[ "SESSIONID" ];

    if ( $cookie != null ) {

        $key = "session3:" . $cookie;
        $session_data = $my_memcache->get( $key );

        if ( $session_data != null ) {
            $_SESSION = unserialize( $session_data );
        }

    } else {

        //call the default php session start code to init the session on 
this
        //client.

        srand((double)microtime()*1000000);
        $cookie = rand( 0, 100000000000 );

        $_COOKIE[ "SESSIONID" ] = $cookie;

        //create a new local aray for the session.
        $_SESSION=array();

        $expire = time() + 365 * 24 * 60 * 60 * 24;
        setcookie( 'SESSIONID', $cookie, $expire, "/", ".tailrank.com" );

    }

}

function tailrank_session_commit() {

    //serialze $_SESSION

    global $_SESSION;
    $my_memcache = tailrank_memcache_get_instance();

    if ( $_SESSION != null ) {

        $session_data = serialize( $_SESSION );
        $cookie = $_COOKIE[ "SESSIONID" ];
        $key = "session3:" . $cookie;

        $my_memcache->set( $key, $session_data, false, 600 );

    }

}

?>

-- 
Founder/CEO Tailrank.com
Location: San Francisco, CA
AIM/YIM: sfburtonator
Skype: burtonator
Blog: feedblog.org
Cell: 415-637-8078 




More information about the memcached mailing list