格式化和注释

This commit is contained in:
7IN0SAN9
2016-01-25 15:55:21 +08:00
parent c6a7b9e6cb
commit abd9ab924a
2 changed files with 115 additions and 83 deletions

View File

@@ -1,19 +1,29 @@
<?php <?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\session\driver; namespace think\session\driver;
use think\session\Driver;
use think\Config; use think\Config;
use think\session\Driver;
class Memcache extends Driver class Memcache extends Driver
{ {
protected $handler = null; protected $handler = null;
protected $config = [ protected $config = [
'host' => '127.0.0.1', // 主机 'host' => '127.0.0.1', // 主机
'port' => 1121, // 端口 'port' => 1121, // 端口
'expire' => 3600, // 有效期 'expire' => 3600, // 有效期
'timeout' => 1, // 超时时间 'timeout' => 1, // 超时时间
'persistent' => 0, // 是否长连接 'persistent' => 0, // 是否长连接
'session_name' => '', // memcache key前缀 'session_name' => '', // memcache key前缀
]; ];
/** /**
@@ -22,18 +32,19 @@ class Memcache extends Driver
* @param string $savePath * @param string $savePath
* @param mixed $sessName * @param mixed $sessName
*/ */
public function open($savePath, $sessName) { public function open($savePath, $sessName)
{
// 检测php环境 // 检测php环境
if (!extension_loaded('memcache')) { if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache'); throw new Exception('_NOT_SUPPERT_:memcache');
} }
$this->handler = new \Memcache; $this->handler = new \Memcache;
// 支持集群 // 支持集群
$hosts = explode(',', $this->config['host']); $hosts = explode(',', $this->config['host']);
$ports = explode(',', $this->config['port']); $ports = explode(',', $this->config['port']);
// 建立连接 // 建立连接
foreach ((array) $hosts as $i => $host) { foreach ((array) $hosts as $i => $host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0]; $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
false === $config['timeout'] ? false === $config['timeout'] ?
$this->handler->addServer($host, $port, $this->config['persistent'], 1) : $this->handler->addServer($host, $port, $this->config['persistent'], 1) :
$this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']); $this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']);
@@ -45,10 +56,11 @@ class Memcache extends Driver
* 关闭Session * 关闭Session
* @access public * @access public
*/ */
public function close() { public function close()
{
$this->gc(ini_get('session.gc_maxlifetime')); $this->gc(ini_get('session.gc_maxlifetime'));
$this->handler->close(); $this->handler->close();
$this->handler = null; $this->handler = null;
return true; return true;
} }
@@ -57,8 +69,9 @@ class Memcache extends Driver
* @access public * @access public
* @param string $sessID * @param string $sessID
*/ */
public function read($sessID) { public function read($sessID)
return $this->handler->get($this->config['session_name'].$sessID); {
return $this->handler->get($this->config['session_name'] . $sessID);
} }
/** /**
@@ -67,8 +80,9 @@ class Memcache extends Driver
* @param string $sessID * @param string $sessID
* @param String $sessData * @param String $sessData
*/ */
public function write($sessID, $sessData) { public function write($sessID, $sessData)
return $this->handler->set($this->config['session_name'].$sessID, $sessData, 0, $this->config['expire']); {
return $this->handler->set($this->config['session_name'] . $sessID, $sessData, 0, $this->config['expire']);
} }
/** /**
@@ -76,8 +90,9 @@ class Memcache extends Driver
* @access public * @access public
* @param string $sessID * @param string $sessID
*/ */
public function destroy($sessID) { public function destroy($sessID)
return $this->handler->delete($this->config['session_name'].$sessID); {
return $this->handler->delete($this->config['session_name'] . $sessID);
} }
/** /**
@@ -85,7 +100,8 @@ class Memcache extends Driver
* @access public * @access public
* @param string $sessMaxLifeTime * @param string $sessMaxLifeTime
*/ */
public function gc($sessMaxLifeTime) { public function gc($sessMaxLifeTime)
{
return true; return true;
} }
} }

View File

@@ -1,20 +1,30 @@
<?php <?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\session\driver; namespace think\session\driver;
use think\session\Driver;
use think\Config; use think\Config;
use think\session\Driver;
class Redis extends Driver class Redis extends Driver
{ {
protected $handler = null; protected $handler = null;
protected $config = [ protected $config = [
'host' => '127.0.0.1', // 主机 'host' => '127.0.0.1', // 主机
'port' => 6379, // 端口 'port' => 6379, // 端口
'password' => '', // 密码 'password' => '', // 密码
'expire' => 3600, // 有效期 'expire' => 3600, // 有效期
'timeout' => false, // 超时时间 'timeout' => false, // 超时时间
'persistent' => 0, // 是否长连接 'persistent' => 0, // 是否长连接
'session_name' => '', // memcache key前缀 'session_name' => '', // memcache key前缀
]; ];
/** /**
@@ -23,14 +33,15 @@ class Redis extends Driver
* @param string $savePath * @param string $savePath
* @param mixed $sessName * @param mixed $sessName
*/ */
public function open($savePath, $sessName) { public function open($savePath, $sessName)
{
// 检测php环境 // 检测php环境
if (!extension_loaded('redis')) { if (!extension_loaded('redis')) {
throw new Exception('_NOT_SUPPERT_:redis'); throw new Exception('_NOT_SUPPERT_:redis');
} }
$this->handler = new \Redis; $this->handler = new \Redis;
// 建立连接 // 建立连接
$func = $this->config['persistent'] ? 'pconnect' : 'connect'; $func = $this->config['persistent'] ? 'pconnect' : 'connect';
false === $this->config['timeout'] ? false === $this->config['timeout'] ?
$this->handler->$func($this->config['host'], $this->config['port']) : $this->handler->$func($this->config['host'], $this->config['port']) :
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']); $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
@@ -44,10 +55,11 @@ class Redis extends Driver
* 关闭Session * 关闭Session
* @access public * @access public
*/ */
public function close() { public function close()
{
$this->gc(ini_get('session.gc_maxlifetime')); $this->gc(ini_get('session.gc_maxlifetime'));
$this->handler->close(); $this->handler->close();
$this->handler = null; $this->handler = null;
return true; return true;
} }
@@ -56,8 +68,9 @@ class Redis extends Driver
* @access public * @access public
* @param string $sessID * @param string $sessID
*/ */
public function read($sessID) { public function read($sessID)
return $this->handler->get($this->config['session_name'].$sessID); {
return $this->handler->get($this->config['session_name'] . $sessID);
} }
/** /**
@@ -66,8 +79,9 @@ class Redis extends Driver
* @param string $sessID * @param string $sessID
* @param String $sessData * @param String $sessData
*/ */
public function write($sessID, $sessData) { public function write($sessID, $sessData)
return $this->handler->set($this->config['session_name'].$sessID, $sessData, 0, $this->config['expire']); {
return $this->handler->set($this->config['session_name'] . $sessID, $sessData, 0, $this->config['expire']);
} }
/** /**
@@ -75,8 +89,9 @@ class Redis extends Driver
* @access public * @access public
* @param string $sessID * @param string $sessID
*/ */
public function destroy($sessID) { public function destroy($sessID)
return $this->handler->delete($this->config['session_name'].$sessID); {
return $this->handler->delete($this->config['session_name'] . $sessID);
} }
/** /**
@@ -84,7 +99,8 @@ class Redis extends Driver
* @access public * @access public
* @param string $sessMaxLifeTime * @param string $sessMaxLifeTime
*/ */
public function gc($sessMaxLifeTime) { public function gc($sessMaxLifeTime)
{
return true; return true;
} }
} }