Hi all,

In first, I'm not familiar with the memcache object.

I had to develop a memcache object wich must work with our actual sessions
files (and not database session).

I used the init handler session and his callback functions.

But in the architecture of the web site in our production environment, we
have 2 web server with memcache installed (on both) behind a load balancer.
And the cached session must be accessible on both of them.

This is my class :

class MemcacheSession {

  /********************************************
  * CONSTANTS                                  *
  * ******************************************/

  const DEFAULT_HOST        = "127.0.0.1";
  const DEFAULT_PORT        = 11211;
  const DEFAULT_COMPRESSION = MEMCACHE_COMPRESSED;

  /*******************************************
  * PRIVATE VARS                              *
  * ******************************************/

  private $servers;
  private $memcache;

  /*******************************************
  * PUBLIC VARS                              *
  * ******************************************/

  public $lifeTime;

  /*******************************************
  * SINGLETON                                *
  * ******************************************/

  private static $instance = null;

  public static function getInstance() {
    if (empty(self::$instance)) {
      self::$instance = new MemcacheSession();
    }
    return self::$instance;
  }

  /*******************************************
  * CONSTRUCTOR                              *
  * ******************************************/

  private function __construct() {
      $this->memcache = new Memcache;
      $this->servers    = array();
  }

  /*******************************************
  * PUBLIC METHODS                           *
  * ******************************************/

  public function start() {

      $this->initSaveHandler();

    if(empty($this->servers)){
      $this->servers[] = array(self::DEFAULT_HOST=>self::DEFAULT_PORT);
    }
    for ($i = 0, $n = count($this->servers); $i < $n; ++$i){
      $this->memcache->addServer(key($this->servers[$i]),
current($this->servers[$i]));
    }
  }

  public function addServer(array $server){
    $this->servers[] = array($server[0]=>$server[1]);
  }

  public function addMultipleServer(array $server){
    for ($i = 0, $n = count($server); $i < $n; ++$i){
      $this->servers[] = $server[$i];
    }
  }

  /*******************************************
  * PRIVATE METHODS                          *
  * ******************************************/

  private function initSaveHandler(){
     session_set_save_handler(array(&$this,"sessionOpenCb"),
                       array(&$this,"sessionCloseCb"),
                       array(&$this,"sessionReadCb"),
                       array(&$this,"sessionWriteCb"),
                       array(&$this,"sessionDestroyCb"),
                       array(&$this,"sessionGcCb"));
  }

  /*******************************************
  * CALLBACK METHODS                         *
  * ******************************************/

  public function sessionOpenCb($savePath, $sessName) {
    return $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
  }

  public function sessionCloseCb() {
    return $this->sessionGcCb(ini_get('session.gc_maxlifetime'));
  }

  public function sessionReadCb($sessID) {
      return $this->memcache->get($sessID);
  }

  public function sessionWriteCb($sessID,$sessData) {
      return $this->memcache->set($sessID, $sessData,
self::DEFAULT_COMPRESSION, $this->lifeTime) or false;
  }

  public function sessionDestroyCb($sessID) {
      return $this->memcache->delete($sessID);
  }

  public function sessionGcCb($sessMaxLifeTime) {}

}

And his implementation :

$memcacheSess = MemcacheSession::getInstance();
$memcacheSess->addServer(array('192.168.0.1','11211'));
$memcacheSess->addServer(array('192.168.0.2','11211'));
$memcacheSess->start();

session_start();

And after we use the standard procedure of $_SESSION object.

In the memcache log (on both servers) i can see some get/set/stored/deleted
events.

But the session is not accessible on the both server and i can't access to
my information where the session is not write by the $_SESSION object.

Someone can help me on this particular problem ?

Thanks a lot.

Mickaƫl.

Reply via email to