redis的操作很多的,以前看到一个比较全的博客,但是现在找不到了。查个东西搜半天,下面整理一下php处理redis的例子,个人觉得常用一些例子。下面的例子都是基于php-redis这个扩展的。
1,connect
描述:实例连接到一个Redis.
参数:host: string,port: int
返回值:BOOL 成功返回:TRUE;失败返回:FALSE
示例:
复制代码 代码如下:
<"codetitle">复制代码 代码如下:
<"11111111111");
var_dump($result); //结果:bool(true)
"codetitle">复制代码 代码如下:
<"11111111111"
"codetitle">复制代码 代码如下:
<"1111111111111");
echo $redis->get('test'); //结果:1111111111111
$redis->delete('test');
var_dump($redis->get('test')); //结果:bool(false)
"codetitle">复制代码 代码如下:
<"1111111111111");
$redis->setnx('test',"22222222");
echo $redis->get('test'); //结果:1111111111111
$redis->delete('test');
$redis->setnx('test',"22222222");
echo $redis->get('test'); //结果:22222222
"codetitle">复制代码 代码如下:
<"1111111111111");
var_dump($redis->exists('test')); //结果:bool(true)
"codetitle">复制代码 代码如下:
<"123");
var_dump($redis->incr("test")); //结果:int(124)
var_dump($redis->incr("test")); //结果:int(125)
"codetitle">复制代码 代码如下:
<"123");
var_dump($redis->decr("test")); //结果:int(122)
var_dump($redis->decr("test")); //结果:int(121)
"codetitle">复制代码 代码如下:
<"1");
$redis->set('test2',"2");
$result = $redis->getMultiple(array('test1','test2'));
print_r($result); //结果:Array ( [0] => 1 [1] => 2 )
"codetitle">复制代码 代码如下:
<"test","111")); //结果:int(1)
var_dump($redis->lpush("test","222")); //结果:int(2)
"codetitle">复制代码 代码如下:
<"test","111")); //结果:int(1)
var_dump($redis->lpush("test","222")); //结果:int(2)
var_dump($redis->rpush("test","333")); //结果:int(3)
var_dump($redis->rpush("test","444")); //结果:int(4)
"codetitle">复制代码 代码如下:
<"test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lpop("test")); //结果:string(3) "222"
"codetitle">复制代码 代码如下:
<"test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lsize("test")); //结果:int(4)
"codetitle">复制代码 代码如下:
<"test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lget("test",3)); //结果:string(3) "444"
"codetitle">复制代码 代码如下:
<"test","111");
$redis->lpush("test","222");
var_dump($redis->lget("test",1)); //结果:string(3) "111"
var_dump($redis->lset("test",1,"333")); //结果:bool(true)
var_dump($redis->lget("test",1)); //结果:string(3) "333"
"codetitle">复制代码 代码如下:
<"test","111");
$redis->lpush("test","222");
print_r($redis->lgetrange("test",0,-1)); //结果:Array ( [0] => 222 [1] => 111 )
"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
<"test1",'111');
print_r($redis->sort('test1')); //结果:Array ( [0] => 111 [1] => 222 [2] => 444 )
"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
<"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
var_dump($redis->spop("test")); //结果:string(3) "333"
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sinter("test","test1")); //结果:array(1) { [0]=> string(3) "111" }
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sinterstore('new',"test","test1")); //结果:int(1)
var_dump($redis->smembers('new')); //结果:array(1) { [0]=> string(3) "111" }
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
print_r($redis->sunion("test","test1")); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 )
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sinterstore('new',"test","test1")); //结果:int(4)
print_r($redis->smembers('new')); //结果:Array ( [0] => 111 [1] => 222 [2] => 333 [3] => 444 )
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
print_r($redis->sdiff("test","test1")); //结果:Array ( [0] => 222 [1] => 333 )
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
$redis->sadd("test1","111");
$redis->sadd("test1","444");
var_dump($redis->sdiffstore('new',"test","test1")); //结果:int(2)
print_r($redis->smembers('new')); //结果:Array ( [0] => 222 [1] => 333 )
"codetitle">复制代码 代码如下:
<"test","111");
$redis->sadd("test","222");
print_r($redis->smembers('test')); //结果:Array ( [0] => 111 [1] => 222 )
?>
php-redis当中,有很多不同名字,但是功能一样的函数,例如:lrem和lremove,这里就不例举了。