Redis整理(5)之資料型別list

2021-06-27 13:37:00 字數 2207 閱讀 2114

雙向鍊錶

應用場景:在成千萬條資料如果只是想取得頭尾的值,那麼可以使用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 "";

print $redis->lpop('lis');//two one

echo "";

print $redis->rpop('lis');//two

//獲取列表片段demo

$redis->lpush('lis','one','two','three','four','five');// five four three two one

print $redis->llen('lis');//獲取列表長度

echo "";

echo "正索引,下標從0開始";

var_dump($redis->lrange('lis',0,1));// five four

echo "";

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 "";

$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 "";

$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));

Redis 資料型別整理

string redis list 列表 在首尾都可以新增資料 lpush rpush lrange 從左 從右 獲取指定長度 lpush list01 1 2 3 4 5 倒序排列 rpush list02 1 2 3 4 5 正序排列 lrange list01 0 1 獲取list01 中的所...

Redis之資料型別

與mysql資料庫支援的多種資料型別相比,redis資料庫支援的資料型別要少上許多。redis資料庫支援五種資料型別 string 字串 hash 雜湊 list 列表 set 集合 及zset sorted set 有序集合 string 是 redis 最基本的型別,你可以理解成與 memcac...

Redis06 Redis五大資料型別 list

單鍵多值 redis列表是簡單的字串列表,按照插入順序排序,可以新增左邊 右邊 底層實際上是乙個雙向鍊錶,對兩端的操作效能好,但是通過索引下標的操作中間節點效能較差 從左邊 右邊插入乙個或多個值 lpop rpop 從左邊 右邊吐出乙個值 值在鍵在,值無鍵亡 rpoplpush 從列表右邊吐出乙個值...