Redis内存数据库存储

前言

会话是临时存储数据信息的一种机制。但是如果项目需要使用一些高复用的服务器架构(如下图),就需要实现session数据同步或者统一存储,不然就有可能出现会话状态丢失的问题。所以如何共享Session信息就是一个需要解决的问题。下面本文讲述的是,如何使用Thinkphp搭配Redis存储Session信息(单应用服务器,单redis版本),以达到Session共享的目的。

Redis内存数据库存储_第1张图片
base lbs

环境

  • 操作系统 Linux ubuntu 16.04
  • PHP环境 PHP 7.0.28-0ubuntu0.16.04.1 ( NTS )
  • redis版本 4.0.8

介绍

Redis是一个使用ANSI C编写的开源、支持网络、基于内存、可选持久性的键值对存储数据库。根据月度排行网站DB-Engines.com的数据显示,Redis是最流行的键值对存储数据库

安装Redis

源码包安装

根据redis官网的教程
1, Download, extract and compile Redis with:

wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar xzf redis-4.0.8.tar.gz
cd redis-4.0.8
make

2, The binaries that are now compiled are available in the src directory. Run Redis with:

src/redis-server

3, You can interact with Redis using the built-in client:

src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

一切顺利的话,源码包Redis就安装完毕了。另外我们也可以使用Docker安装Redis。

Docker仓库安装

这里是Redis的仓库 ,提供了 Redis 3.x ~ 4.x 各个版本的镜像。

1, 查看docker-hub上的远程仓库

docker search redis
Redis内存数据库存储_第2张图片
image

2, 执行下面命令,大意是首先docker会本地搜索docker images中理出来的镜像,如果没有则从docker hub(国内镜像)拉取redis的docker镜像,然后启动一个名为some-redis容器,-p是把本地的9736端口和容器中的6379端口映射起来,-d (daemon)并让容器运行在后台,--requirepass客户端链接需要认证。

docker run --name my-redis -p 9736:6379 -d redis --requirepass "11110000"
##查看docker已经启动的容器
docker ps
##安装redis客户端
sudo apt install reids-tools
##尝试从外部主机范围docker中的redis
redis-cli -h 127.0.0.1 -p 9736 -a 11110000
Redis内存数据库存储_第3张图片
test-redis

Docker仓库安装的程序,拓展迁徙相对简单,和其他系统的耦合性很小。接下来我们安装拓展。

安装php-redis 拓展

拓展我们选择phpredis,这个比较通用,更新比较及时。github地址在这里

git clone https://github.com/phpredis/phpredis.git
##切换到phpredis所在的文件夹
phpize
./configure
make && make install
##重启php服务
systemctl restart  php7.0-fpm.service

这时候就可以去phpinfo页面看到redis拓展已经成功出现了。


Redis内存数据库存储_第4张图片
phpinfo-redis.png

这时候,我们就可以在php程序中操作redis数据库了。新建一个文件vim phpredis.php,内容如下。

connect('127.0.0.1',9736);
  $redis->auth("11110000");
  var_dump($redis->info());
##执行上面的测试文件,如果成功了,就出现类似下面的结果。
php phpredis.php
redis-out.png

到这,php-redis拓展已经安装成功,我们也可以在php程序总操作redis数据库了。接下来我们就在项目中使用redis存储session。

Redis存储Session

如果是使用原生的php来实现这个功能,就需要调整配置文件php.ini,配置phpredis使用文档编写代码。

session.save_handler = redis
session.save_path = "tcp://host1:6379?weight=1"

如果是使用php框架的话,一般框架已经集成了对应的模块操作驱动。我们可以到thinkphp官网找到Session的Redis存储驱动类。我们需要在相对应的位置(./ThinkPHP/Library/Think/Session/Driver/
)新建一个Redis.class.php文件,内容如下。

 C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
            'port'       => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
            'timeout'    => C('REDIS_TIMEOUT') ? C('REDIS_TIMEOUT') : false,
            'persistent' => C('REDIS_PERSISTENT') ? C('REDIS_PERSISTENT'):false,
            'auth'      => C('REDIS_AUTH') ? C('REDIS_AUTH') : false,
            );
        }
        $options['host'] = explode(',', $options['host']);
        $options['port'] = explode(',', $options['port']);
        $options['auth'] = explode(',', $options['auth']);
        foreach ($options['host'] as $key=>$value) {
            if (!isset($options['port'][$key])) {
                $options['port'][$key] = $options['port'][0];
            }
            if (!isset($options['auth'][$key])) {
                $options['auth'][$key] = $options['auth'][0];
            }
        }
        $this->options =  $options;
        $expire = C('SESSION_EXPIRE');
        $this->options['expire'] =  
        isset($expire) ? (int)$expire : (int)ini_get('session.gc_maxlifetime');
        $this->options['prefix'] =  
        isset($options['prefix']) ?  $options['prefix']  :  C('SESSION_PREFIX');
        $this->handler  = new \Redis;
    }

    /**
     * 连接Redis服务端
     * @access public
     * @param bool $is_master : 是否连接主服务器
     */
    public function connect($is_master = true) {
        if ($is_master) {
            $i = 0;
        } else {
            $count = count($this->options['host']);
            if ($count == 1) {
                $i = 0;
            } else {
                $i = rand(1, $count - 1);   //多个从服务器随机选择
            }
        }
        $func = $this->options['persistent'] ? 'pconnect' : 'connect';
        try {
            if ($this->options['timeout'] === false) {
                $result = $this
                ->handler
                ->$func($this->options['host'][$i], $this->options['port'][$i]);
                if (!$result)
                    throw new \Think\Exception('Redis Error', 100);
            } else {
                $result = $this
                ->handler
                ->$func($this->options['host'][$i], $this->options['port'][$i],
                $this->options['timeout']);
                if (!$result)
                    throw new \Think\Exception('Redis Error', 101);
            }
            if ($this->options['auth'][$i]) {

                $result = $this->handler->auth($this->options['auth'][$i]);
                if (!$result) {
                    throw new \Think\Exception('Redis Error', 102);
                }
            }
        } catch ( \Exception $e ) {
          exit('Error:'.$e->getMessage().'
Error Code:'.$e->getCode().''); } } /** +---------------------------------------------------------- * 打开Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $savePath * @param mixed $sessName +---------------------------------------------------------- */ public function open($savePath, $sessName) { return true; } /** +---------------------------------------------------------- * 关闭Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- */ public function close() { if ($this->options['persistent'] == 'pconnect') { $this->handler->close(); } return true; } /** +---------------------------------------------------------- * 读取Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessID +---------------------------------------------------------- */ public function read($sessID) { $this->connect(0); $this->get_result = $this->handler->get($this->options['prefix'].$sessID); //延长有效期 $this->handler->expire($this->options['prefix'] .$sessID,C('SESSION_EXPIRE')); return $this->get_result; } /** +---------------------------------------------------------- * 写入Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessID * @param String $sessData +---------------------------------------------------------- */ public function write($sessID, $sessData) { if (!$sessData || $sessData == $this->get_result) { return true; } $this->connect(1); $expire = $this->options['expire']; $sessID = $this->options['prefix'].$sessID; if(is_int($expire) && $expire > 0) { $result = $this->handler->setex($sessID, $expire, $sessData); $re = $result ? 'true' : 'false'; }else{ $result = $this->handler->set($sessID, $sessData); $re = $result ? 'true' : 'false'; } return $result; } /** +---------------------------------------------------------- * 删除Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessID +---------------------------------------------------------- */ public function destroy($sessID) { $this->connect(1); return $this->handler->delete($this->options['prefix'].$sessID); } /** +---------------------------------------------------------- * Session 垃圾回收 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessMaxLifeTime +---------------------------------------------------------- */ public function gc($sessMaxLifeTime) { return true; } /** +---------------------------------------------------------- * 打开Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $savePath * @param mixed $sessName +---------------------------------------------------------- */ public function execute() { session_set_save_handler( array(&$this, "open"), array(&$this, "close"), array(&$this, "read"), array(&$this, "write"), array(&$this, "destroy"), array(&$this, "gc") ); } public function __destruct() { if ($this->options['persistent'] == 'pconnect') { $this->handler->close(); } session_write_close(); } }

接着在配置文件中配置如下配置项,不需要改动php.ini配置项了。

//SESSION 配置
'SESSION_AUTO_START'    => true, //是否开启session
'SESSION_TYPE'          =>  'Redis',    //session 驱动
'SESSION_PREFIX'        =>  '',       //session前缀
'SESSION_EXPIRE'        =>  '7200',        //session有效期(单位:秒) 0表示永久缓存。
//Redis 配置
'REDIS_HOST'        =>'127.0.0.1', //redis服务器ip,多台用逗号隔开;
'REDIS_PORT'        =>'9736',//端口号
'REDIS_TIMEOUT'     =>'30',//超时时间(秒)
'REDIS_PERSISTENT'  =>false,//是否长连接 false=短连接
'REDIS_AUTH'        =>'11110000',//AUTH认证密码

其他层面的代码不需要改动,因为框架已经将Session类的操作方面封装起来了。业务存储Session时候,底层会根据配置项来实例化对应的存储驱动类。从而就实现了session存储在对应的redis数据库中了。

如无意外,至此,我们就完成了这次的小目标了。

结语

本文简单地实现了单机版的Redis存储php程序的Session数据。下一步,计划利用Docker技术搭建Redis基础的高可用分布式集群,就本文的基础上进行项目架构拓展。如有兴趣,敬请关注!

你可能感兴趣的:(Redis内存数据库存储)