redis php+redis常用数据类型应用总结

2017/07/17
redis常用数据类型总结,方便项目查看和实现
总结的别人的,自己验证下
前人栽树 后人乘凉

redis字符串 String


//利用列表list实现简单队列
$redis = new \Redis();
$redis->connect('localhost',6379);

//进队列
$redis->rpush('queue_name',json_encode(['user_id'=>5]));
$redis->rpush('queue_name',json_encode(['user_id'=>6]));
$redis->rpush('queue_name',json_encode(['user_id'=>7]));

echo "数据进队列完成\n";

//可查看队列
$res = $redis->lrange('queue_name',0,1000);
print_r($res);

//出队列
$redis->lpop('queue_name');

//查看队列
$res = $redis->lrange('queue_name',0,1000);
print_r($res);

然后写个死循环 读就行了。同步到mysql也行
分页
lpush
lrange
($page-1)*10)  ($page-1)*10)+14

根据索引删除列表
$value = time();
$redis->lSet("listtest", 1, $value);
$redis->lRem("listtest", $value,1);
$i = $redis->LRANGE('listtest',0,-1);
print_r($i);die();

redis链表 List


lPush()/rPush()
lPush()的语法格式是:$redis->lPush(key, value),作用是将value添加到链表key的左边(头部)。  
rPush()的语法格式是:$redis->rPush(key, value),作用是将value添加到链表key的右边(尾部)。  

lPop()/rPop()
lPop()的语法格式是:$redis->lPop(key),作用是将链表key的左边(头部)元素删除。  
rPop()的语法格式是:$redis->rPop(key),作用是将链表key的右边(尾部)元素删除。  


$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
// 进队列
$userId = mt_rand(000000, 999999);
$redis->rpush('QUEUE_NAME',json_encode(['user_id' => $userId]));

$userId = mt_rand(000000, 999999);
$redis->rpush('QUEUE_NAME',json_encode(['user_id' => $userId]));

$userId = mt_rand(000000, 999999);
$redis->rpush('QUEUE_NAME',json_encode(['user_id' => $userId]));

echo "数据进队列成功 \n";
// 查看队列
$res = $redis->lrange('QUEUE_NAME', 0, 1000);

echo "当前队列数据为: \n";
print_r($res);

echo "----------------------------- \n";
// 出队列
$redis->lpop('QUEUE_NAME');

echo "数据出队列成功 \n";
// 查看队列
$res = $redis->lrange('QUEUE_NAME', 0, 1000);

echo "当前队列数据为: \n";
print_r($res);


redis哈希 Hash

$redis = new redis(); 
$redis->connect('192.168.1.108', 6379); 
$redis->delete('test'); 
$redis->hset('test', 'key1', 'hello'); 

echo $redis->hget('test', 'key1');     //结果:hello 

$redis->hSetNx('test', 'key1', 'world'); 
echo $redis->hget('test', 'key1');   //结果:hello 

$redis->delete('test'); 
$redis->hSetNx('test', 'key1', 'world'); 
echo "<br>"; 
echo $redis->hget('test', 'key1');   //结果:world 

echo $redis->hlen('test');   //结果:1 
var_dump($redis->hdel('test','key1'));  //结果:bool(true) 

$redis->delete('test'); 
$redis->hSet('test', 'a', 'x'); 
$redis->hSet('test', 'b', 'y'); 
$redis->hSet('test', 'c', 'z'); 
print_r($redis->hkeys('test'));  //结果:Array ( [0] => a [1] => b [2] => c )  

print_r($redis->hvals('test'));  //结果:Array ( [0] => x [1] => y [2] => z )  
 
print_r($redis->hgetall('test'));  //结果:Array ( [a] => x [b] => y [c] => z )  
 
var_dump($redis->hExists('test', 'a'));  //结果:bool(true)

$redis->delete('test'); 
echo $redis->hIncrBy('test', 'a', 3);    //结果:3 
echo $redis->hIncrBy('test', 'a', 1);    //结果:4  

redis集合 Zadd

$redis = new redis(); 
$redis->connect('192.168.1.108', 6379); 
$redis->delete('test'); 
$redis->zadd('test', 1, 'val1'); 
$redis->zadd('test', 0, 'val2'); 
$redis->zadd('test', 3, 'val3');
print_r($redis->zrange('test', 0, -1)); //结果:Array ( [0] => val2 [1] => val1 [2] => val3 ) 

$redis->zdelete('test', 'val2'); 
print_r($redis->zrange('test', 0, -1)); //结果:Array ( [0] => val1 [1] => val3 )  

$redis->zadd('test',4, 'val0'); 
print_r($redis->zrevrange('test', 0, -1));  //结果:Array ( [0] => val0 [1] => val3 [2] => val1 ) 
print_r($redis->zrevrange('test', 0, -1,true));  //结果:Array ( [val0] => 4 [val3] => 3 [val1] => 1 )  

$redis->zadd('key', 0, 'val0'); 
$redis->zadd('key', 2, 'val2'); 
$redis->zadd('key', 10, 'val10'); 

print_r($redis->zrangebyscore('key', 0, 3, array('limit' => array(1, 1),'withscores' => TRUE))); //结果:Array ( [val2] => 2 ) 

print_r($redis->zrangebyscore('key', 0, 3, array('limit' => array(1, 1)))); //结果:Array ( [0] => val2 )  

echo $redis->zcount('key', 0, 3); //结果:2 

$redis->zremrangebyscore('key', 0, 3); 
print_r($redis->zrange('key', 0, -1));  //结果:Array ( [0] => val10 )   ($page-1)*10)  ($page-1)*10)+14
 
echo $redis->zsize('key');   //结果:1 
 
$redis->zadd('key', 2.5, 'aaaa'); 
echo $redis->zscore('key', 'aaaa');   //结果:2.5 
 
echo $redis->zrank('key', 'aaaa');   //结果:0 
echo $redis->zrevrank('key', 'aaaa');    //结果:1 
 
$redis->delete('key'); 
 
echo $redis->zincrby('key', 2, 'aaaa');  //结果:2  score累加
echo $redis->zincrby('key', 1, 'aaaa');  //结果:3 
 
$redis->delete('key'); 
$redis->delete('test'); 
 
$redis->zadd('key', 0, 'val0'); 
$redis->zadd('key', 1, 'val1'); 
$redis->zadd('key', 4, 'val2'); 
$redis->zadd('test', 2, 'val2'); 
$redis->zadd('test', 3, 'val3'); 
$redis->zunion('k01', array('key', 'test')); 
print_r($redis->zrange('k01',0, -1)); //结果:Array ( [0] => val0 [1] => val1 [2] => val3 [3] => val2 ) 
 
$redis->zunion('k03', array('key', 'test'), array(5, 1)); 
print_r($redis->zrange('k03',0, -1)); //结果:Array ( [0] => val0 [1] => val3 [2] => val1 [3] => val2 )  
 
$redis->zinter('k02', array('key', 'test')); 
print_r($redis->zrange('k02',0, -1)); //结果:Array ( [0] => val2 ) 

zIncrBy
zRevRange

个人随笔

安静的等待,你若盛开,蝴蝶自来

承接高质量 网站开发 app开发 如有需要请点击About联系

@承鹏辉