|
|
<?php |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace think\session\driver; |
|
|
|
|
|
use SessionHandler; |
|
|
use think\Exception; |
|
|
|
|
|
class Redis extends SessionHandler |
|
|
{ |
|
|
|
|
|
protected $handler = null; |
|
|
protected $config = [ |
|
|
'host' => '127.0.0.1', |
|
|
'port' => 6379, |
|
|
'password' => '', |
|
|
'select' => 0, |
|
|
'expire' => 3600, |
|
|
'timeout' => 0, |
|
|
'persistent' => true, |
|
|
'session_name' => '', |
|
|
]; |
|
|
|
|
|
public function __construct($config = []) |
|
|
{ |
|
|
$this->config = array_merge($this->config, $config); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function open($savePath, $sessName) |
|
|
{ |
|
|
|
|
|
if (!extension_loaded('redis')) { |
|
|
throw new Exception('not support:redis'); |
|
|
} |
|
|
$this->handler = new \Redis; |
|
|
|
|
|
|
|
|
$func = $this->config['persistent'] ? 'pconnect' : 'connect'; |
|
|
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']); |
|
|
|
|
|
if ('' != $this->config['password']) { |
|
|
$this->handler->auth($this->config['password']); |
|
|
} |
|
|
|
|
|
if (0 != $this->config['select']) { |
|
|
$this->handler->select($this->config['select']); |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function close() |
|
|
{ |
|
|
$this->gc(ini_get('session.gc_maxlifetime')); |
|
|
$this->handler->close(); |
|
|
$this->handler = null; |
|
|
return true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function read($sessID) |
|
|
{ |
|
|
return (string) $this->handler->get($this->config['session_name'] . $sessID); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function write($sessID, $sessData) |
|
|
{ |
|
|
if ($this->config['expire'] > 0) { |
|
|
return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData); |
|
|
} else { |
|
|
return $this->handler->set($this->config['session_name'] . $sessID, $sessData); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function destroy($sessID) |
|
|
{ |
|
|
return $this->handler->delete($this->config['session_name'] . $sessID) > 0; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function gc($sessMaxLifeTime) |
|
|
{ |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|