diff --git a/library/think/session/driver/Redis.php b/library/think/session/driver/Redis.php new file mode 100644 index 00000000..10fab63b --- /dev/null +++ b/library/think/session/driver/Redis.php @@ -0,0 +1,90 @@ + '127.0.0.1', // 主机 + 'port' => 6379, // 端口 + 'password' => '', // 密码 + 'expire' => 3600, // 有效期 + 'timeout' => false, // 超时时间 + 'persistent' => 0, // 是否长连接 + 'session_name' => '', // memcache key前缀 + ]; + + /** + * 打开Session + * @access public + * @param string $savePath + * @param mixed $sessName + */ + public function open($savePath, $sessName) { + // 检测php环境 + if (!extension_loaded('redis')) { + throw new Exception('_NOT_SUPPERT_:redis'); + } + $this->handler = new \Redis; + // 建立连接 + $func = $this->config['persistent'] ? 'pconnect' : 'connect'; + false === $this->config['timeout'] ? + $this->handler->$func($this->config['host'], $this->config['port']) : + $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']); + if ('' != $this->config['password']) { + $this->handler->auth($this->config['password']); + } + return true; + } + + /** + * 关闭Session + * @access public + */ + public function close() { + $this->gc(ini_get('session.gc_maxlifetime')); + $this->handler->close(); + $this->handler = null; + return true; + } + + /** + * 读取Session + * @access public + * @param string $sessID + */ + public function read($sessID) { + return $this->handler->get($this->config['session_name'].$sessID); + } + + /** + * 写入Session + * @access public + * @param string $sessID + * @param String $sessData + */ + public function write($sessID, $sessData) { + return $this->handler->set($this->config['session_name'].$sessID, $sessData, 0, $this->config['expire']); + } + + /** + * 删除Session + * @access public + * @param string $sessID + */ + public function destroy($sessID) { + return $this->handler->delete($this->config['session_name'].$sessID); + } + + /** + * Session 垃圾回收 + * @access public + * @param string $sessMaxLifeTime + */ + public function gc($sessMaxLifeTime) { + return true; + } +} \ No newline at end of file