php中使用redis扩展学习一
<?php
$rd=new Redis();
$rd->connect('127.0.0.1','6379');
$rd->select(0); //选你要用的库默认为0-15,库名不可自定义
//string 操作
//存储单个字符串
$rd->set('s1','Hello'); //返回值1覆盖s1原有值如s1不存在建立s1
$rd->get('s1'); //返回值
//存储多个字符串
$sarr=array('s2'=>'hello','s3'=>'world');
$rd->mset($sarr); //返回值1 存在覆盖原值 不存在新建
$rd->mget(array('s2','s3')); // array 0)"hello" 1)"world"
//设置过期时间
$rd->expire('s1',10); //10秒后s1将被清理掉
$rd->expire('s1',0); //s1立即被清理掉
//注意:在redis的cli模式小可以使用 expire s1 -1 立即清理s1 但是不可以使用php这么写会返回错误$rd->expire(‘s1’,-1);错误$rd->expireAt(‘s1’,time()+20); //在给定的时间点将清理掉如果给定的时间小于当前时间则立刻清理s1
练习:利用Redis
1,存储任意字符串
2,给字符串增加过期时间,
4,将一部分字符串使用过期方式立即清理掉
<?php
$rd=new Redis();
$rd->connect('localhost',6379);
$rd->set('userid',1);
$arr=array('username'=>'leo',
'useremail'=>'user@yahoo.cn',
'useradd'=>'somewhere');
$rd->expire('userid',10);
$rd->expire('userid',0);
$rd->expireAt('userid',strtotime('2016-10-10'));
?>