From 5589c8c9619f8a477407aaa6b74653e8828fe2a0 Mon Sep 17 00:00:00 2001 From: winerQin <753814253@qq.com> Date: Tue, 24 Mar 2015 15:03:47 +0800 Subject: [PATCH] Rename src to src/LaravelSessionHandler.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Laravel5.x不支持 SessionHandler类。所以,我调整之后把适合的版本提交上来。麻烦请合并一下。 --- src/LaravelSessionHandler.php | 108 ++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/LaravelSessionHandler.php diff --git a/src/LaravelSessionHandler.php b/src/LaravelSessionHandler.php new file mode 100644 index 0000000..13f9278 --- /dev/null +++ b/src/LaravelSessionHandler.php @@ -0,0 +1,108 @@ +_ttl = $ttl ?: ini_get('session.gc_maxlifetime'); + $this->_prefix = $prefix; + $components = parse_url($save_path); + + if ($components === false || + !isset($components['scheme'], $components['host'], $components['port']) + || strtolower($components['scheme']) !== 'tcp') { + throw new Exception('Invalid session.save_path: ' . $save_path); + } + + $this->_client = new Client($components['host'], $components['port']); + if (isset($components['query'])) { + parse_str($components['query'], $query); + if (isset($query['auth'])) { + $this->_client->auth($query['auth']); + } + } + } + /** + * 关闭当前session。 + * @return boolean + */ + public function close() { + $this->_client->close(); + return true; + } + /** + * + * @param string $session_id + * @return boolean + */ + public function destroy($session_id) { + $this->_client->del($this->_prefix . $session_id); + return true; + } + /** + * ssdb不需要gc清理过期的session。ssdb会自己清掉。 + * @param int $maxlifetime + * @return boolean + */ + public function gc($maxlifetime) { + return true; + } + /** + * @param string $save_path + * @param string $name + * @return boolean + */ + public function open($save_path, $name) { + return true; + } + /** + * 读取session。 + * @param string $session_id + * @return string + */ + public function read($session_id) { + if (isset($this->_cache[$session_id])) { + return $this->_cache[$session_id]; + } + $session_data = $this->_client->get($this->_prefix . $session_id)->data; + return $this->_cache[$session_id] = ($session_data === null ? '' : $session_data); + } + /** + * 写session。 + * @param string $session_id + * @param string $session_data + * @return boolean + */ + public function write($session_id, $session_data) { + if (isset($this->_cache[$session_id]) && $this->_cache[$session_id] === $session_data) { + $this->_client->expire($this->_prefix . $session_id, $this->_ttl); + } else { + $this->_cache[$session_id] = $session_data; + $this->_client->setx($this->_prefix . $session_id, $session_data, $this->_ttl); + } + return true; + } +}