PHP store session with couchbase

如何用couchbase存储session

有两种常见方式:
1.采用memcache模式连接couchbase 只需两句修改:

ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://couchbase_host:9999');

注意里面的9999是couchbase 里面创建的bucket的对外memcache端口,这种访问方式运行以memcache兼容的模式访问couchbase。
2.采用couchbase扩展添加一个sessionhandler如下:

/****
 * php storage session with couchbase
 * by fushanlang@gmail.com
 */

/***
 * SessionHandlerInterface is a internal interface only for PHP_VERSION>=5.4.0
 * so if interface_exists() false we need to define by yourself.
 */
if (!interface_exists('SessionHandlerInterface')) {
    interface SessionHandlerInterface
    {
        public function close();

        public function destroy($session_id);

        public function gc($maxlifetime);

        public function open($save_path, $session_id);

        public function  read($session_id);

        public function write($session_id, $session_data);
    }
}
/**
 * A reference implementation of a custom Couchbase session handler.
 */
class CouchbaseSessionHandler implements SessionHandlerInterface
{

    /**
     * Holds the Couchbase connection.
     */
    protected $_connection = null;

    /**
     * The Couchbase host and port.
     */
    protected $_host = null;

    /**
     * The Couchbase bucket name.
     */
    protected $_bucket = null;

    /**
     * The prefix to be used in Couchbase keynames.
     */
    protected $_keyPrefix = 'session:';

    /**
     * Define a expiration time of 10 minutes.
     */
    protected $_expire = 600;

    /**
     * Set the default configuration params on init.
     */
    public function __construct($host = '127.0.0.1:8091', $bucket = 'default')
    {
        $this->_host = $host;
        $this->_bucket = $bucket;
    }

    /**
     * Open the connection to Couchbase (called by PHP on `session_start()`)
     */
    public function open($savePath, $sessionName)
    {
        $this->_connection = new Couchbase($this->_host, '', '', $this->_bucket);
        return $this->_connection ? true : false;
    }

    /**
     * Close the connection. Called by PHP when the script ends.
     */
    public function close()
    {
        unset($this->_connection);
        return true;
    }

    /**
     * Read data from the session.
     */
    public function read($sessionId)
    {
        $key = $this->_keyPrefix . $sessionId;
        $result = $this->_connection->get($key);

        return $result ? : null;
    }

    /**
     * Write data to the session.
     */
    public function write($sessionId, $sessionData)
    {
        $key = $this->_keyPrefix . $sessionId;
        if (empty($sessionData)) {
            return false;
        }

        $result = $this->_connection->set($key, $sessionData, $this->_expire);
        return $result ? true : false;
    }

    /**
     * Delete data from the session.
     */
    public function destroy($sessionId)
    {
        $key = $this->_keyPrefix . $sessionId;
        $result = $this->_connection->delete($key);

        return $result ? true : false;
    }

    /**
     * Run the garbage collection.
     */
    public function gc($maxLifetime)
    {
        return true;
    }

}

//usage example
define('COUCHBASE_HOST_PORT','xxxxx:8091');
define('COUCHBASE_BUCKET','session');

if(class_exists('Couchbase')&&defined('COUCHBASE_HOST_PORT')&&defined('COUCHBASE_BUCKET')){
    $handler = new CouchbaseSessionHandler(COUCHBASE_HOST_PORT,COUCHBASE_BUCKET);
    if(version_compare(PHP_VERSION,'5.4.0')>=0){
        session_set_save_handler($handler,true);
    }else{
        session_set_save_handler(
            array($handler, 'open'),
            array($handler, 'close'),
            array($handler, 'read'),
            array($handler, 'write'),
            array($handler, 'destroy'),
            array($handler, 'gc'));
    }
}
session_start();

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Protected by WP Anti Spam