浮山狼

导航

PHP store session with couchbase

如何用couchbase存储session

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

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

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

    1. /**** 
    2.  * php storage session with couchbase 
    3.  * by fushanlang@gmail.com 
    4.  */  
    5.   
    6. /*** 
    7.  * SessionHandlerInterface is a internal interface only for PHP_VERSION>=5.4.0 
    8.  * so if interface_exists() false we need to define by yourself. 
    9.  */  
    10. if (!interface_exists('SessionHandlerInterface')) {  
    11.     interface SessionHandlerInterface  
    12.     {  
    13.         public function close();  
    14.   
    15.         public function destroy($session_id);  
    16.   
    17.         public function gc($maxlifetime);  
    18.   
    19.         public function open($save_path$session_id);  
    20.   
    21.         public function  read($session_id);  
    22.   
    23.         public function write($session_id$session_data);  
    24.     }  
    25. }  
    26. /** 
    27.  * A reference implementation of a custom Couchbase session handler. 
    28.  */  
    29. class CouchbaseSessionHandler implements SessionHandlerInterface  
    30. {  
    31.   
    32.     /** 
    33.      * Holds the Couchbase connection. 
    34.      */  
    35.     protected $_connection = null;  
    36.   
    37.     /** 
    38.      * The Couchbase host and port. 
    39.      */  
    40.     protected $_host = null;  
    41.   
    42.     /** 
    43.      * The Couchbase bucket name. 
    44.      */  
    45.     protected $_bucket = null;  
    46.   
    47.     /** 
    48.      * The prefix to be used in Couchbase keynames. 
    49.      */  
    50.     protected $_keyPrefix = 'session:';  
    51.   
    52.     /** 
    53.      * Define a expiration time of 10 minutes. 
    54.      */  
    55.     protected $_expire = 600;  
    56.   
    57.     /** 
    58.      * Set the default configuration params on init. 
    59.      */  
    60.     public function __construct($host = '127.0.0.1:8091'$bucket = 'default')  
    61.     {  
    62.         $this->_host = $host;  
    63.         $this->_bucket = $bucket;  
    64.     }  
    65.   
    66.     /** 
    67.      * Open the connection to Couchbase (called by PHP on `session_start()`) 
    68.      */  
    69.     public function open($savePath$sessionName)  
    70.     {  
    71.         $this->_connection = new Couchbase($this->_host, ''''$this->_bucket);  
    72.         return $this->_connection ? true : false;  
    73.     }  
    74.   
    75.     /** 
    76.      * Close the connection. Called by PHP when the script ends. 
    77.      */  
    78.     public function close()  
    79.     {  
    80.         unset($this->_connection);  
    81.         return true;  
    82.     }  
    83.   
    84.     /** 
    85.      * Read data from the session. 
    86.      */  
    87.     public function read($sessionId)  
    88.     {  
    89.         $key = $this->_keyPrefix . $sessionId;  
    90.         $result = $this->_connection->get($key);  
    91.   
    92.         return $result ? : null;  
    93.     }  
    94.   
    95.     /** 
    96.      * Write data to the session. 
    97.      */  
    98.     public function write($sessionId$sessionData)  
    99.     {  
    100.         $key = $this->_keyPrefix . $sessionId;  
    101.         if (emptyempty($sessionData)) {  
    102.             return false;  
    103.         }  
    104.   
    105.         $result = $this->_connection->set($key$sessionData$this->_expire);  
    106.         return $result ? true : false;  
    107.     }  
    108.   
    109.     /** 
    110.      * Delete data from the session. 
    111.      */  
    112.     public function destroy($sessionId)  
    113.     {  
    114.         $key = $this->_keyPrefix . $sessionId;  
    115.         $result = $this->_connection->delete($key);  
    116.   
    117.         return $result ? true : false;  
    118.     }  
    119.   
    120.     /** 
    121.      * Run the garbage collection. 
    122.      */  
    123.     public function gc($maxLifetime)  
    124.     {  
    125.         return true;  
    126.     }  
    127.   
    128. }  
    129.   
    130. //usage example  
    131. define('COUCHBASE_HOST_PORT','xxxxx:8091');  
    132. define('COUCHBASE_BUCKET','session');  
    133.   
    134. if(class_exists('Couchbase')&&defined('COUCHBASE_HOST_PORT')&&defined('COUCHBASE_BUCKET')){  
    135.     $handler = new CouchbaseSessionHandler(COUCHBASE_HOST_PORT,COUCHBASE_BUCKET);  
    136.     if(version_compare(PHP_VERSION,'5.4.0')>=0){  
    137.         session_set_save_handler($handler,true);  
    138.     }else{  
    139.         session_set_save_handler(  
    140.             array($handler'open'),  
    141.             array($handler'close'),  
    142.             array($handler'read'),  
    143.             array($handler'write'),  
    144.             array($handler'destroy'),  
    145.             array($handler'gc'));  
    146.     }  
    147. }  
    148. session_start();  
  1. 转自 http://www.fushanlang.com/blog/php-store-session-with-couchbase-2302/

posted on 2013-09-26 11:53  浮山狼  阅读(1000)  评论(0)    收藏  举报