redis实战 php实例【1】

<?php
$redis=new Redis();
$redis->connect('127.0.0.1',6379);
$redis->auth('123456');

// string

$redis->set('test','helloworld');
$get = $redis->get('test');
//helloworld
$tt  = $redis->del('test');
//int(1) 返回成功的个数


//list
$redis->delete('list-key'); 
$redis->rPush('list-key','item');//从右端推入
$redis->rPush('list-key','item2');
$redis->rPush('list-key','item');
$redis->rPush('list-key','item3');
$lRange = $redis->lRange('list-key', 0, -1); //取出指定范围

//array(4) { [0]=> string(4) "item" [1]=> string(5) "item2" [2]=> string(4) "item" [3]=> string(5) "item3" }

$lIndex = $redis->lIndex('list-key', 1);//取出指定元素
//string(4) "item2"

$lPop = $redis->lPop('list-key');//从左端弹出 弹出后不存在于列表
//string(4) "item"
$lRange2 = $redis->lRange('list-key', 0, -1); //取出指定范围
//array(2) { [0]=> string(5) "item2" [1]=> string(5) "item3" }

//sadd

$redis->sAdd('set-key','item');
$redis->sAdd('set-key','item2');
$redis->sAdd('set-key','item3');
$redis->sAdd('set-key','item');//集合不允许 有同一个元素 否则会覆盖
$smembers = $redis->smembers('set-key');//获取所有元素
//array(3) { [0]=> string(5) "item2" [1]=> string(5) "item3" [2]=> string(4) "item" }

$sismember = $redis->sismember('set-key','item4');//检查一个元素是否存在集合中
//bool(false)

$srem = $redis->srem('set-key','item2');//移除集合中的元素
//int(1) 返回成功的个数


//hash

$redis->hset('hash-key','sub-key1','value1');//int(1) 返回成功的个数
$redis->hset('hash-key','sub-key2','value1');//int(1) 返回成功的个数
$redis->hset('hash-key','sub-key1','value1');//int(0) 已存在
$hgetall = $redis->hgetall('hash-key');
//array(2) { ["sub-key1"]=> string(6) "value2" ["sub-key2"]=> string(6) "value1" }

$redis->hdel('hash-key','sub-key2');//int(1) 返回成功的个数
$redis->hdel('hash-key','sub-key2');//int(0) 不存在返回0

$hget = $redis->hget('hash-key','sub-key1');
//string(6) "value1"
$hgetall2 = $redis->hgetall('hash-key');
//array(1) { ["sub-key1"]=> string(6) "value1" }



//zset

$redis->zadd('zset-key',728,'member1');//int(1) 返回成功的个数
$redis->zadd('zset-key',982,'member0');//int(1) 返回成功的个数
$redis->zadd('zset-key',982,'member0');//int(0) 已存在
$zrange = $redis->zrange('zset-key',0,1,'withscores');
//array(2) { ["member1"]=> float(728) ["member0"]=> float(982) }

?>

 

posted @ 2018-06-15 11:40  百事可口  阅读(279)  评论(0编辑  收藏  举报