Redis自学笔记—PHP

  • connect

实例连接到一个Redis。

$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
var_dump($result); //结果:bool(true)
  • set
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->get('test');
var_dump($result);   
?>
  • get
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->get('test');
var_dump($result);   
?>
  • delete
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"yyd");
echo $redis->get('test');   
$redis->delete('test');
var_dump($redis->get('test'));  //结果:bool(false)
?>
  • setnx
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"1111111111111");
$redis->setnx('test',"22222222");
echo $redis->get('test');  //结果:1111111111111
$redis->delete('test');
$redis->setnx('test',"22222222");
echo $redis->get('test');  //结果:22222222
?>
  • exists
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"1111111111111");
var_dump($redis->exists('test'));  //结果:bool(true)
?>
  • incr
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"123");
var_dump($redis->incr("test"));  //结果:int(124)
var_dump($redis->incr("test"));  //结果:int(125)
?>
  • decr
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"123");
var_dump($redis->decr("test"));  //结果:int(122)
var_dump($redis->decr("test"));  //结果:int(121)
?>
  • getMultiple
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test1',"1");
$redis->set('test2',"2");
$result = $redis->getMultiple(array('test1','test2'));
print_r($result);   //结果:Array ( [0] => 1 [1] => 2 )
?>
  • lpush
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
var_dump($redis->lpush("test","111"));   //结果:int(1)
var_dump($redis->lpush("test","222"));   //结果:int(2)
?>
  • rpush
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
var_dump($redis->lpush("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)
?>
  • lpop
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lpop("test"));  //结果:string(3) "222"
?>
  • rpop
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->rpop("test"));  //结果:string(3) "444"
?>
  • lsize
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lsize("test"));  //结果:int(4)
?>
  • lget
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lget("test",3));  //结果:string(3) "444"
?>
  • lset
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("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"
?>
  • lgetrange
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
print_r($redis->lgetrange("test",0,-1));  //结果:Array ( [0] => 222 [1] => 111 )
?>
  • lremove
<?php
$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush('test','a');
$redis->lpush('test','b');
$redis->lpush('test','c');
$redis->rpush('test','a');
print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b [2] => a [3] => a )
var_dump($redis->lremove('test','a',2));   //结果:int(2)
print_r($redis->lgetrange('test', 0, -1)); //结果:Array ( [0] => c [1] => b )
?>
    其他类型不详举例,可以按照redis-php手册上的操作,形式基本上与在redis上的一样。

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut/p/3612497.html

posted @ 2014-03-19 22:29  我爱物联网  阅读(809)  评论(0编辑  收藏  举报
AmazingCounters.com