[ci框架3.0关于session的redis数据库]CI框架3.0关于session的redis,数据库的使用方法

更新时间:2020-03-11    来源:redis    手机版     字体:

【www.bbyears.com--redis】

3.0变动是比较大的,session支持了redis,memcache,files,database。文件的就不说了,这个是最简单的。这里说一下使用数据库、redis来存储session的方法。

首选,因为其手册没有更新,手册讲的是2.0版本的使用方法。3.0已经不是这样使用。这里使用数据库的方式,最大的变化是数据库变了。
 

 代码如下 CREATE TABLE IF NOT EXISTS `ci_sessions` (
        `id` varchar(40) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        PRIMARY KEY (id),
        KEY `ci_sessions_timestamp` (`timestamp`)
);

请使用这个建数据库,应该根据其配置说明应该就会使用数据库的存储方式了。

当然如果你重构了他的数据库操作类,尤其是增加了自动主从读写分离等,那么你需要也重构这个session_database.php了,这个有问题可以交流,也很简单。ci的好处就可以很方便重构。

2、使用redis

这个我是看他代码才知道怎么用的。

看下这个文件。Session_redis_driver.php

 

 代码如下
public function __construct(&$params)
 {
  parent::__construct($params);
 
  if (empty($this->_config["save_path"]))
  {
   log_message("error", "Session: No Redis save path configured.");
  }
  elseif (preg_match("#(?:tcp://)?([^:?]+)(?:\:(\d+))?(\?.+)?#", $this->_config["save_path"], $matches))
  {
   isset($matches[3]) OR $matches[3] = ""; // Just to avoid undefined index notices below
   $this->_config["save_path"] = array(
    "host" => $matches[1],
    "port" => empty($matches[2]) ? NULL : $matches[2],
    "password" => preg_match("#auth=([^\s&]+)#", $matches[3], $match) ? $match[1] : NULL,
    "database" => preg_match("#database=(\d+)#", $matches[3], $match) ? (int) $match[1] : NULL,
    "timeout" => preg_match("#timeout=(\d+\.\d+)#", $matches[3], $match) ? (float) $match[1] : NULL
   );
 
   preg_match("#prefix=([^\s&]+)#", $matches[3], $match) && $this->_key_prefix = $match[1];
  }
  else
  {
   log_message("error", "Session: Invalid Redis save path format: ".$this->_config["save_path"]);
  }
 
  if ($this->_config["match_ip"] === TRUE)
  {
   $this->_key_prefix .= $_SERVER["REMOTE_ADDR"].":";
  }
 }

根据这个构造函数,我们发现,redis的连接信息全在save_path这个配置项。所以我们可以这样配置

 

 代码如下 $config["sess_save_path"] = "127.0.0.1:6379?auth=admin&prefix=ci_sess:&database=2&timeout=60";

这个地方,你根据代码就可以看出来,ip,端口。然后是后面的password就是匹配的auth=之后,后面就很简单了。prefix是session存储的前缀,database是库号,redis是分库的。timeout连接超时时间

本文来源:http://www.bbyears.com/shujuku/86436.html