thinkphp框架session redis驱动

来源:互联网 发布:js九九乘法表原理 编辑:程序博客网 时间:2024/06/13 22:23
配置文件
/* session */
'SESSION_AUTO_START' => true,
'SESSION_TYPE' => 'Redis',


/* redis */
'REDIS_HOST' => '127.0.0.1',
'REDIS_PORT' => '6379',
'REDIS_PASSWORD' => 'auth',

代码
<?php
/**
* Created by PhpStorm.
* User: hunk
* Date: 2016-03-02
* Time: 17:33
*/

namespace Think\Session\Driver;

class Redis
{
protected $lifeTime = 3600;
protected $sessionName = '';
protected $handle = null;

/**
* 开启连接
* @param $savePath
* @param $sessName
* @return bool
*/
public function open($savePath, $sessName)
{
$this->lifeTime = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') : $this->lifeTime;
if (is_resource($this->handle)) {
return true;
}
$host = C('REDIS_HOST') ?: '127.0.0.1';
$port = C('REDIS_PORT') ?: '6379';
$auth = C('REDIS_PASSWORD') ?: '';
$timeout = C('SESSION_TIMEOUT') ?: 1;
$redis = new \Redis();
$redis->connect($host, $port, $timeout);
$redis->auth($auth);
if (!$redis) {
return false;
}
$this->handle = $redis;
return true;
}

/**
* 关闭连接
* @return bool
*/
public function close()
{
$this->gc(ini_get('session.gc_maxlifetime'));
$this->handle->close();
$this->handle = null;
return true;
}

/**
* 读取session
* @param $sessID
* @return mixed
*/
public function read($sessID)
{
return $this->handle->get($sessID);
}

/**
* 写入session
* @param $sessID
* @param $sessData
* @return mixed
*/
public function write($sessID, $sessData)
{
return $this->handle->setex($sessID, $this->lifeTime, $sessData);
}

/**
* 注销session
* @param $sessID
* @return bool
*/
public function destroy($sessID)
{
return $this->handler->del($sessID)>=1?true:false;
}

/**
* 垃圾回收
* @param $sessMaxLifeTime
* @return bool
*/
public function gc($sessMaxLifeTime)
{
return true;
}


}

0 0