tp3使用redis存储session

2020-06-28

注:使用时php需要安装redis扩展

创建文件 ThinkPHP/Library/Think/Session/Driver/Redis.class.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace Think\Session\Driver;
 
use think\Exception;
 
class Redis implements \SessionHandlerInterface
{
    /** @var \Redis */
    protected $handler = null;
    protected $config = [
        // redis主机
        'host' => '127.0.0.1',
        // redis端口
        'port' => 6379,
        // 密码
        'password' => '123456',
        // 操作库
        'select' => 0,
        // 有效期(秒)
        'expire' => 3600,
        // 超时时间(秒)
        'timeout' => 0,
        // 是否长连接
        'persistent' => true,
        // sessionkey前缀
        'session_name' => 'session_',
    ];
 
    public function __construct($config = [])
    {
        $this->config['host'] = C("SESSION_REDIS_HOST") ? C("SESSION_REDIS_HOST") : $this->config['host'];
        $this->config['port'] = C("SESSION_REDIS_PORT") ? C("SESSION_REDIS_PORT") : $this->config['port'];
        $this->config['password'] = C("SESSION_REDIS_AUTH") ? C("SESSION_REDIS_AUTH") : $this->config['password'];
        $this->config['select'] = C("SESSION_REDIS_SELECT") ? C("SESSION_REDIS_SELECT") : $this->config['select'];
        $this->config['expire'] = C("SESSION_REDIS_EXPIRE") ? C("SESSION_REDIS_EXPIRE") : $this->config['expire'];
        $this->config['session_name'] = C('SESSION_PREFIX') ? C('SESSION_PREFIX') : $this->config['session_name'];
        $this->config['timeout'] = C('SESSION_CACHE_TIMEOUT') ? C('SESSION_CACHE_TIMEOUT') : $this->config['timeout'];
 
    }
 
    /**
     * 打开Session
     * @access public
     * @param string $savePath
     * @param mixed $sessName
     * @return bool
     * @throws Exception
     */
    public function open($savePath, $sessName)
    {
        // 检测php环境
        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;
    }
 
    /**
     * 关闭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
     * @return string
     */
    public function read($sessID)
    {
        return (string)$this->handler->get($this->config['session_name'] . $sessID);
    }
 
    /**
     * 写入Session
     * @access public
     * @param string $sessID
     * @param String $sessData
     * @return bool
     */
    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);
        }
    }
 
    /**
     * 删除Session
     * @access public
     * @param string $sessID
     * @return bool
     */
    public function destroy($sessID)
    {
        return $this->handler->delete($this->config['session_name'] . $sessID) > 0;
    }
 
    /**
     * Session 垃圾回收
     * @access public
     * @param string $sessMaxLifeTime
     * @return bool
     */
    public function gc($sessMaxLifeTime)
    {
        return true;
    }
}

修改配置文件:

'SESSION_AUTO_START'    =>  true,    // 是否自动开启Session
'SESSION_TYPE'          =>  'Redis', // session hander类型 默认无需设置 除非扩展了session hander驱动
'SESSION_PREFIX'        =>  'sess_', // session 前缀
'VAR_SESSION_ID'        =>  'session_id', //sessionID的提交变量
 
'SESSION_PERSISTENT'    =>  1,//是否长连接(对于php来说0和1都一样)
'SESSION_CACHE_TIMEOUT' =>  3,//连接超时时间(秒)
'SESSION_REDIS_EXPIRE'  =>  3600,        //session有效期(单位:秒) 0表示永久缓存
'SESSION_REDIS_HOST'    =>  '127.0.0.1', //redis服务器ip
'SESSION_REDIS_PORT'    =>  '6379',       //端口
'SESSION_REDIS_AUTH'    =>  '123456', //认证密码
'SESSION_REDIS_SELECT'  =>  0,  //操作库

文章来源于:https://www.cnblogs.com/lixiuran/p/12174906.html

{/if}