1 <?php 2 $redis=new Redis(); 3 $redis->connect('127.0.0.1',6379); 4 $redis->auth('123456'); 5 6 // string 7 8 $redis->set('test','helloworld'); 9 $get = $redis->get('test'); 10 //helloworld 11 $tt = $redis->del('test'); 12 //int(1) 返回成功的个数 13 14 15 //list 16 $redis->delete('list-key'); 17 $redis->rPush('list-key','item');//从右端推入 18 $redis->rPush('list-key','item2'); 19 $redis->rPush('list-key','item'); 20 $redis->rPush('list-key','item3'); 21 $lRange = $redis->lRange('list-key', 0, -1); //取出指定范围 22 23 //array(4) { [0]=> string(4) "item" [1]=> string(5) "item2" [2]=> string(4) "item" [3]=> string(5) "item3" } 24 25 $lIndex = $redis->lIndex('list-key', 1);//取出指定元素 26 //string(4) "item2" 27 28 $lPop = $redis->lPop('list-key');//从左端弹出 弹出后不存在于列表 29 //string(4) "item" 30 $lRange2 = $redis->lRange('list-key', 0, -1); //取出指定范围 31 //array(2) { [0]=> string(5) "item2" [1]=> string(5) "item3" } 32 33 //sadd 34 35 $redis->sAdd('set-key','item'); 36 $redis->sAdd('set-key','item2'); 37 $redis->sAdd('set-key','item3'); 38 $redis->sAdd('set-key','item');//集合不允许 有同一个元素 否则会覆盖 39 $smembers = $redis->smembers('set-key');//获取所有元素 40 //array(3) { [0]=> string(5) "item2" [1]=> string(5) "item3" [2]=> string(4) "item" } 41 42 $sismember = $redis->sismember('set-key','item4');//检查一个元素是否存在集合中 43 //bool(false) 44 45 $srem = $redis->srem('set-key','item2');//移除集合中的元素 46 //int(1) 返回成功的个数 47 48 49 //hash 50 51 $redis->hset('hash-key','sub-key1','value1');//int(1) 返回成功的个数 52 $redis->hset('hash-key','sub-key2','value1');//int(1) 返回成功的个数 53 $redis->hset('hash-key','sub-key1','value1');//int(0) 已存在 54 $hgetall = $redis->hgetall('hash-key'); 55 //array(2) { ["sub-key1"]=> string(6) "value2" ["sub-key2"]=> string(6) "value1" } 56 57 $redis->hdel('hash-key','sub-key2');//int(1) 返回成功的个数 58 $redis->hdel('hash-key','sub-key2');//int(0) 不存在返回0 59 60 $hget = $redis->hget('hash-key','sub-key1'); 61 //string(6) "value1" 62 $hgetall2 = $redis->hgetall('hash-key'); 63 //array(1) { ["sub-key1"]=> string(6) "value1" } 64 65 66 67 //zset 68 69 $redis->zadd('zset-key',728,'member1');//int(1) 返回成功的个数 70 $redis->zadd('zset-key',982,'member0');//int(1) 返回成功的个数 71 $redis->zadd('zset-key',982,'member0');//int(0) 已存在 72 $zrange = $redis->zrange('zset-key',0,1,'withscores'); 73 //array(2) { ["member1"]=> float(728) ["member0"]=> float(982) } 74 75 ?>
转载自:redis实战 php实例【1】 - 百事可口 - 博客园 (cnblogs.com)