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

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

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

PHP

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`)

);

1

2

3

4

5

6

7

8

CREATETABLEIFNOTEXISTS`ci_sessions`(

`id`varchar(40)NOTNULL,

`ip_address`varchar(45)NOTNULL,

`timestamp`int(10)unsignedDEFAULT0NOTNULL,

`data`blobNOTNULL,

PRIMARYKEY(id),

KEY`ci_sessions_timestamp`(`timestamp`)

);

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

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

2、使用redis

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

看下这个文件。Session_redis_driver.php

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'].':';

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

publicfunction__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这个配置项。所以我们可以这样配置

PHP

$config['sess_save_path'] = '127.0.0.1:6379?auth=admin&prefix=ci_sess:&database=2&timeout=60';

1

$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连接超时时间

程序本天成,妙手偶得之!我们只是代码的搬运工!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值