Redis整理(5)之数据类型list

来源:互联网 发布:win7编程界面 编辑:程序博客网 时间:2024/05/24 07:46
双向链表
应用场景:在成千万条数据如果只是想取得头尾的值,那么可以使用list,复杂度是根据取得值得深度,越靠近两端速度越快
例如,获取最新的插入数据,即最新新闻。
另外拓展:可以作为数据结构栈和队列使用
lpush key value[value...]  向list左边插入一个值
rpush key value[value...]  向list右边插入一个值
lpop key list左边弹出一个值
rpop key list右边弹出一个值
llen key   list的元素个数
lrange key start stop 获得列表片段
lrem key count value  //删除掉count次值为value的元素  注意区分count的正负值
lindex key index // key['index'];
lset key index value // key['index'] = value
ltrim key start end //保留片段 start-end片段
linsert key before|after pivot value//在pivot之前之后插入值

<?php$redis->lpush('lis','one','two','three'); print $redis->llen('lis');//获取列表长度 echo "<hr>"; print $redis->lpop('lis');//two one echo "<hr>"; print $redis->rpop('lis');//two //获取列表片段demo$redis->lpush('lis','one','two','three','four','five');//  five four three two one print $redis->llen('lis');//获取列表长度 echo "<hr>"; echo "正索引,下标从0开始"; var_dump($redis->lrange('lis',0,1));// five four echo "<hr>"; echo "负索引,下标从最右边一个元素开始开始"; var_dump($redis->lrange('lis',-2,-1))//two one//批量删除列表值 $redis->lpush('lis','one','two','three','four','five','one');//  five four three two one $redis->lrem('lis','one',1); var_dump($redis->lrange('lis',0,-1));  //打印列表所有的元素,注意观察列表元素one消失的顺序 echo "<hr>"; $redis->lrem('lis','one',1); var_dump($redis->lrange('lis',0,-1));//下标索引index用法  查询,改值$redis->lpush('lis','one','two','three','four','five','one');//  five four three two one print $redis->lindex('lis',1); echo "<hr>"; $redis->lset('lis',1,'change'); print $redis->lindex('lis',1);//保存部分值$redis->lpush('lis','one','two','three','four','five','one');//  five four three two one $redis->ltrim('lis',1,2);//只保存下标1-2的元素 var_dump($redis->lrange('lis',0,-1));//在某个指定元素前后插入$redis->lpush('lis','one','two','three','four','five','one');//  five four three two one $redis->linsert('lis','BEFORE','two','value'); var_dump($redis->lrange('lis',0,-1));





0 0