Rumah > Artikel > pembangunan bahagian belakang > redis php 实例一
/**
* Redis 学习笔记
*/
//echo 8%(-2); //print 0
/**
链接一个redis实例
return 链接成功返回TRUE 否则返回 false
*/
$redis = new Redis();
$result = $redis->connect('127.0.0.1',6379);
if($result){
echo "redis conn is success";
}else{
echo "redis conn is fail";
}
echo '
'; <br> var_dump($result1).'<br>'; <br> /** <br> lpush <br> 将一个或多个值 value 插入到列表 key 的表头 <br> <br> <br> 描述:由列表头部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回FALSE。 <br> 参数:key,value <br> 返回值:成功返回数组长度,失败false <br> */ <br> $redis->delete('test'); <br> var_dump($redis->lpush("test","111")).'<br>'; //结果:int(1) <br> var_dump($redis->lpush("test","222")).'<br>'; //结果:int(2) <br> /** <br> rpush <br> <br> 描述:由列表尾部添加字符串值。如果不存在该键则创建该列表。如果该键存在,而且不是一个列表,返回FALSE。 <br> 参数:key,value <br> 返回值:成功返回数组长度,失败false <br> */ <br> $redis->delete('test'); <br> var_dump($redis->lpush("test","111")).'<br>'; //结果:int(1) <br> var_dump($redis->lpush("test","222")).'<br>'; //结果:int(2) <br> var_dump($redis->rpush("test","333")).'<br>'; //结果:int(3) <br> var_dump($redis->rpush("test","444")).'<br>'; //结果:int(4) <br> <br> /** <br> lpop <br> <br> 描述:返回和移除列表的第一个元素 <br> 参数:key <br> 返回值:成功返回第一个元素的值 ,失败返回false <br> */ <br> $redis->delete('test'); <br> $redis->lpush("test","111"); <br> $redis->lpush("test","222"); <br> $redis->rpush("test","333"); <br> $redis->rpush("test","444"); <br> var_dump($redis->lpop("test")).'<br>'; //结果:string(3) "222" <br> <br> /** <br> rpop <br> <br> 描述:返回和移除列表的最后一个元素 <br> 参数:key <br> 返回值:成功返回最后一个元素的值 ,失败返回false <br> */ <br> <br> $redis->delete('test'); <br> $redis->lpush("test","111"); <br> $redis->lpush("test","222"); <br> $redis->rpush("test","333"); <br> $redis->rpush("test","444"); <br> var_dump($redis->rpop("test")).'<br>'; //结果:string(3) "444" <br> <br> /** <br> lsize,llen <br> <br> 描述:返回的列表的长度。如果列表不存在或为空,该命令返回0。如果该键不是列表,该命令返回FALSE。 <br> 参数:Key <br> 返回值:成功返回数组长度,失败false <br> */ <br> $redis->delete('test'); <br> $redis->lpush("test","111"); <br> $redis->lpush("test","222"); <br> $redis->rpush("test","333"); <br> $redis->rpush("test","444"); <br> var_dump($redis->lsize("test")).'<br>'; //结果:int(4) <br> <br> /** <br> lget <br> <br> 描述:返回指定键存储在列表中指定的元素。 0第一个元素,1第二个… -1最后一个元素,-2的倒数第二…错误的索引或键不指向列表则返回FALSE。 <br> 参数:key index <br> 返回值:成功返回指定元素的值,失败false <br> */ <br> <br> $redis->delete('test'); <br> $redis->lpush("test","111"); <br> $redis->lpush("test","222"); <br> $redis->rpush("test","333"); <br> $redis->rpush("test","444"); <br> var_dump($redis->lget("test",3)).'<br>'; //结果:string(3) "444" <br> <br> /** <br> lset <br> <br> 描述:为列表指定的索引赋新的值,若不存在该索引返回false. <br> 参数:key index value <br> 返回值:成功返回true,失败false <br> */ <br> <br> $redis->delete('test'); <br> $redis->lpush("test","111"); <br> $redis->lpush("test","222"); <br> var_dump($redis->lget("test",1)).'<br>'; //结果:string(3) "111" <br> var_dump($redis->lset("test",1,"333")).'<br>'; //结果:bool(true) <br> var_dump($redis->lget("test",1)).'<br>'; //结果:string(3) "333" <br> <br> <br> /** <br> lgetrange <br> <br> 描述: <br> 返回在该区域中的指定键列表中开始到结束存储的指定元素,lGetRange(key, start, end)。0第一个元素,1第二个元素… -1最后一个元素,-2的倒数第二… <br> 参数:key start end <br> 返回值:成功返回查找的值,失败false <br> */ <br> <br> $redis->delete('test'); <br> $redis->lpush("test","111"); <br> $redis->lpush("test","222"); <br> print_r($redis->lgetrange("test",0,-1)); //结果:Array ( [0] => 222 [1] => 111 ) <br> <br> <br> /** <br> lremove <br> <br> 描述:从列表中从头部开始移除count个匹配的值。如果count为零,所有匹配的元素都被删除。如果count是负数,内容从尾部开始删除。 <br> 参数:key count value <br> 返回值:成功返回删除的个数,失败false <br> */ <br> <br> $redis->delete('test'); <br> $redis->lpush('test','a'); <br> $redis->lpush('test','b'); <br> $redis->lpush('test','c'); <br> $redis->rpush('test','a'); <br> print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b [2] => a [3] => a ) <br> var_dump($redis->lremove('test','a',2)); //结果:int(2) <br> print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b ) <br> <br> <br> /** <br> sadd <br> <br> 描述:为一个Key添加一个值。如果这个值已经在这个Key中,则返回FALSE。 <br> 参数:key value <br> 返回值:成功返回true,失败false <br> <br> */ <br> $redis->delete('test'); <br> var_dump($redis->sadd('test','111')); //结果:bool(true) <br> var_dump($redis->sadd('test','333')); //结果:bool(true) <br> print_r($redis->sort('test')); //结果:Array ( [0] => 111 [1] => 333 ) <br> <br> /** <br> <br> sremove <br> <br> 描述:删除Key中指定的value值 <br> 参数:key member <br> 返回值:true or false <br> */ <br> $redis->delete('test'); <br> $redis->sadd('test','111'); <br> $redis->sadd('test','333'); <br> $redis->sremove('test','111'); <br> print_r($redis->sort('test')); //结果:Array ( [0] => 333 ) <br> <br> <br> /** <br> <br> smove <br> <br> 描述:将Key1中的value移动到Key2中 <br> 参数:srcKey dstKey member <br> 返回值:true or false <br> */ <br> $redis->delete('test'); <br> $redis->delete('test1'); <br> $redis->sadd('test','111'); <br> $redis->sadd('test','333'); <br> $redis->sadd('test1','222'); <br> $redis->sadd('test1','444'); <br> $redis->smove('test',"test1",'111'); <br> print_r($redis->sort('test1')); //结果:Array ( [0] => 111 [1] => 222 [2] => 444 ) ; <br> <br> /** <br> <br> scontains <br> <br> 描述:检查集合中是否存在指定的值。 <br> 参数:key value <br> 返回值:true or false <br> */ <br> $redis->delete('test'); <br> $redis->sadd('test','111'); <br> $redis->sadd('test','112'); <br> $redis->sadd('test','113'); <br> var_dump($redis->scontains('test', '111')); //结果:bool(true) <br> <br> /** <br> <br> ssize <br> <br> 描述:返回集合中存储值的数量 <br> 参数:key <br> 返回值:成功返回数组个数,失败0 <br> */ <br> $redis->delete('test'); <br> $redis->sadd('test','111'); <br> $redis->sadd('test','112'); <br> echo $redis->ssize('test'); //结果:2 <br> <br> <br> <br> /** <br> <br> spop <br> <br> 描述:随机移除并返回key中的一个值 <br> 参数:key <br> 返回值:成功返回删除的值,失败false <br> */ <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> var_dump($redis->spop("test")); //结果:string(3) "333" <br> <br> /** <br> <br> sinter <br> <br> 描述:返回一个所有指定键的交集。如果只指定一个键,那么这个命令生成这个集合的成员。如果不存在某个键,则返回FALSE。 <br> 参数:key1, key2, keyN <br> 返回值:成功返回数组交集,失败false <br> */ <br> $redis->delete('test'); <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> $redis->sadd("test1","111"); <br> $redis->sadd("test1","444"); <br> var_dump($redis->sinter("test","test1")); //结果:array(1) { [0]=> string(3) "111" } <br> <br> <br> /** <br> sinterstore <br> <br> 描述:执行sInter命令并把结果储存到新建的变量中。 <br> 参数: <br> Key: dstkey, the key to store the diff into. <br> Keys: key1, key2… keyN. key1..keyN are intersected as in sInter. <br> 返回值:成功返回,交集的个数,失败false <br> */ <br> <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> $redis->sadd("test1","111"); <br> $redis->sadd("test1","444"); <br> var_dump($redis->sinterstore('new',"test","test1")); //结果:int(1) <br> var_dump($redis->smembers('new')); //结果:array(1) { [0]=> string(3) "111" } <br> <br> <br> /** <br> sunion <br> <br> 描述: <br> 返回一个所有指定键的并集 <br> 参数: <br> Keys: key1, key2, … , keyN <br> 返回值:成功返回合并后的集,失败false <br> */ <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> $redis->sadd("test1","111"); <br> $redis->sadd("test1","444"); <br> print_r($redis->sunion("test","test1")); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) <br> <br> <br> <br> <br> /** <br> sunionstore <br> <br> 描述:执行sunion命令并把结果储存到新建的变量中。 <br> 参数: <br> Key: dstkey, the key to store the diff into. <br> Keys: key1, key2… keyN. key1..keyN are intersected as in sInter. <br> 返回值:成功返回,交集的个数,失败false <br> */ <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> $redis->sadd("test1","111"); <br> $redis->sadd("test1","444"); <br> var_dump($redis->sinterstore('new',"test","test1")); //结果:int(4) <br> print_r($redis->smembers('new')); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 ) <br> <br> <br> <br> <br> /** <br> 描述:返回第一个集合中存在并在其他所有集合中不存在的结果 <br> 参数:Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis. <br> 返回值:成功返回数组,失败false <br> */ <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> $redis->sadd("test1","111"); <br> $redis->sadd("test1","444"); <br> print_r($redis->sdiff("test","test1")); //结果:Array ( [0] => 222 [1] => 333 ) <br> <br> <br> <br> <br> /** <br> sdiffstore <br> <br> 描述:执行sdiff命令并把结果储存到新建的变量中。 <br> 参数: <br> Key: dstkey, the key to store the diff into. <br> Keys: key1, key2, … , keyN: Any number of keys corresponding to sets in redis <br> 返回值:成功返回数字,失败false <br> */ <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> $redis->sadd("test","333"); <br> $redis->sadd("test1","111"); <br> $redis->sadd("test1","444"); <br> var_dump($redis->sdiffstore('new',"test","test1")); //结果:int(2) <br> print_r($redis->smembers('new')); //结果:Array ( [0] => 222 [1] => 333 ); <br> <br> <br> /** <br> smembers, sgetmembers <br> <br> 描述: <br> 返回集合的内容 <br> 参数:Key: key <br> 返回值:An array of elements, the contents of the set. <br> */ <br> <br> $redis->delete('test'); <br> $redis->sadd("test","111"); <br> $redis->sadd("test","222"); <br> print_r($redis->smembers('test')); //结果:Array ( [0] => 111 [1] => 222 ) <br> <br> ?>