前言
在遇到了 SESSION
混亂的問題后残腌,突然對自定義會話管理器有了興趣村斟,于是便研究實現(xiàn)了一番。
分析
根據(jù)PHP官網(wǎng)的說明抛猫,實現(xiàn)方式有兩種蟆盹,這里準(zhǔn)備用繼承類的方式做,同時將 SESSION
存儲于 Memcache
中闺金。
實現(xiàn)
首先先要寫一個繼承于 SessionHandlerInterface
的類逾滥。
class MemcachedHanlder implements SessionHandlerInterface
{
}
在 MemcachedHanlder
類實例化的時候,附上需要連接 Memcached
的參數(shù)败匹。
protected $_options;
protected $_memcached;
protected $_prefix;
function __construct(array $options = [])
{
if (!isset($options['host'])) {
$options['host'] = '127.0.0.1';
}
if (!isset($options['port'])) {
$options['port'] = 11211;
}
if (!isset($options['persistent'])) {
$options['persistent'] = false;
}
if (!isset($options['lifetime'])) {
$options['lifetime'] = null;
}
$this->_prefix = isset($options['prefix']) ? $options['prefix'] : '';
$this->_options = $options;
}
繼承 SessionHandlerInterface
的類需要實現(xiàn)幾個方法:open
寨昙、close
、read
掀亩、write
舔哪、destory
和 gc
。
在 open
中連接 Memcached
槽棍。
/**
* Create new session, or re-initialize existing session
*
* @param string $path
* @param string $name
* @return boolean
*/
public function open($path, $name)
{
$options = $this->_options;
if ($options) {
$this->_memcached = new \Memcached($options);
$this->_memcached->addServer($options['host'], $options['port'], $options['persistent']);
return true;
}
return false;
}
/**
* Closes the current session
*
* @return integer
*/
public function close()
{
return true;
}
/**
* Read session data
*
* @param string $id
* @return string
*/
public function read($id)
{
return $this->_memcached->get($this->_prefix . $id, $this->_lifetime);
}
/**
* Read session data
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data)
{
return $this->_memcached->set($this->_prefix . $id, $data, $this->_lifetime);
}
/**
* Destroys a session
*
* @param string $id
* @return boolean
*/
public function destroy($id)
{
if (!$id && $this->_memcached->exists($this->_prefix . $id)) {
return $this->_memcached->delete($this->_prefix . $id);
}
return true;
}
/**
* Cleans up expired sessions
*
* @param string $value
* @return boolean
*/
public function gc($maxlifetime)
{
return true;
}
Handler
類寫完之后捉蚤,實例化它,并調(diào)用 session_set_save_handler
完成注冊炼七。
$options = [];
$handler = new MemcachedHanlder($options);
session_set_save_handler($handler, true);
這時候缆巧,開始暢享 SESSION
吧。
完整代碼
class MemcachedHanlder implements SessionHandlerInterface
{
protected $_options;
protected $_memcached;
protected $_prefix;
function __construct(array $options = [])
{
if (!isset($options['host'])) {
$options['host'] = '127.0.0.1';
}
if (!isset($options['port'])) {
$options['port'] = 11211;
}
if (!isset($options['persistent'])) {
$options['persistent'] = false;
}
if (!isset($options['lifetime'])) {
$options['lifetime'] = null;
}
$this->_prefix = isset($options['prefix']) ? $options['prefix'] : '';
$this->_options = $options;
}
/**
* Create new session, or re-initialize existing session
*
* @param string $path
* @param string $name
* @return boolean
*/
public function open($path, $name)
{
$options = $this->_options;
if ($options) {
$this->_memcached = new \Memcached($options);
$this->_memcached->addServer($options['host'], $options['port'], $options['persistent']);
return true;
}
return false;
}
/**
* Closes the current session
*
* @return integer
*/
public function close()
{
return true;
}
/**
* Read session data
*
* @param string $id
* @return string
*/
public function read($id)
{
return $this->_memcached->get($this->_prefix . $id, $this->_lifetime);
}
/**
* Read session data
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data)
{
return $this->_memcached->set($this->_prefix . $id, $data, $this->_lifetime);
}
/**
* Destroys a session
*
* @param string $id
* @return boolean
*/
public function destroy($id)
{
if (!$id && $this->_memcached->exists($this->_prefix . $id)) {
return $this->_memcached->delete($this->_prefix . $id);
}
return true;
}
/**
* Cleans up expired sessions
*
* @param string $value
* @return boolean
*/
public function gc($maxlifetime)
{
return true;
}
}
$options = [];
$handler = new MemcachedHanlder($options);
session_set_save_handler($handler, true);
總結(jié)
記得之前面試的時候豌拙,有人問過我 SESSION
與 COOKIE
之間的關(guān)系陕悬,SESSION
是怎么實現(xiàn)的,SESSION
除了存文件之外姆蘸,還能夠存在哪里墩莫,如何實現(xiàn)。
當(dāng)初的我都沒有回答上逞敷,想象當(dāng)初真的是太年輕了狂秦,也很天真。越接觸推捐,越發(fā)現(xiàn)自己的渺小裂问,越要勇往直前。
-- EOF --
本文轉(zhuǎn)載自IMJCW
原文鏈接:PHP SESSION 自定義會話管理器