redisコマンド(3)-両端キュータイプ

7390 ワード

キュータイプ内部は双方向チェーンテーブル(double linked list)で実現されるので、リストの両端に要素を追加する場合
間複雑度は0(1)であり,両端に近い要素を取得するほど速度が速くなる.
 
 
次のコマンドのkey代表リスト
1.キューの左側に要素を追加し、追加後のキューの長さを返します.
 lpush key value1 value2 value...
 
 
localhost:6379> lpush students a b c
(integer) 3
             【c,b,a】

 
2.キューの右側に要素を追加し、追加後のキューの長さを返します.
 
rpush key value1 value2 value....
 
localhost:6379> rpush students o p q
(integer) 6
         【c,b,a,o,p,q】

 3.キューの左側から要素をポップアップします(要素を削除するのと同じです).
 
   lpop key 
 
localhost:6379> lpop students
"c"
localhost:6379>  lpop students
"b"

 4.キューの右側から要素をポップアップします(要素を削除するのと同じです).
rpop key 
 
localhost:6379> rpop students
"q"
localhost:6379> rpop students
"p"

 
5.取得キュー内の要素の数
llen key 
localhost:6379> llen students
(integer) 4

 6.インデックス範囲で取得されたキュー内の要素を取得します(ただし、キューから移動しません)
 lrange key startIndex endIndex
startIndex:開始要素番号
endIndex:カットオフ要素番号(取得した要素に含まれる)
要素連番値:キューの一番左の要素連番は0で、その後の要素連番は順次+1で、一番右の要素は連番最大の要素です
もう1つの要素シーケンス番号の値:最も右側のメタレートシーケンス番号は-1で、前の要素シーケンス番号は-1で、最も左側の要素はシーケンス番号が最も小さい要素です.
 
localhost:6379> del students
localhost:6379> lpush students a b c
(integer) 3
localhost:6379> rpush students o p q
(integer) 6
localhost:6379> lrange students 1 4
1) "b"
2) "a"
3) "o"
4) "p"
localhost:6379> lrange students 1 -2
1) "b"
2) "a"
3) "o"
4) "p"

 7.キューで指定した値を削除します(実際に削除した要素の数を返します).
 lrem key count value
キューからcount個の値がvalueに等しい要素を削除します.
countが正数の場合:count個の要素またはキューに要素値=valueがない要素に達するまで左から右へ削除します.
countが負の場合:count個の要素またはキューに要素値=valueのない要素に達するまで右から左へ削除します.
count=0の場合:キュー内のすべての要素値=valueを削除する要素を表します.
 
localhost:6379> del students
localhost:6379> lpush students c b a c b a c b a
(integer) 9
localhost:6379> lrange students 0 -1
1) "a"
2) "b"
3) "c"
4) "a"
5) "b"
6) "c"
7) "a"
8) "b"
9) "c"
localhost:6379> lrem students -2 c//     2    ==c   
(integer) 2
localhost:6379> lrange students 0 -1
1) "a"
2) "b"
3) "c"
4) "a"
5) "b"
6) "a"
7) "b"
localhost:6379> lrem students 2 a//     2    ==a   
(integer) 2
localhost:6379> lrange students 0 -1
1) "b"
2) "c"
3) "b"
4) "a"
5) "b"

 8.インデックス番号によるキュー内の要素の取得
lindex key index
要素連番値:キューの一番左の要素連番は0で、その後の要素連番は順次+1で、一番右の要素は連番最大の要素です
もう1つの要素シーケンス番号の値:最も右側のメタレートシーケンス番号は-1で、前の要素シーケンス番号は-1で、最も左側の要素はシーケンス番号が最も小さい要素です.
localhost:6379> del students
localhost:6379> lpush students a b c d e f
(integer) 6
localhost:6379> lindex students 0
"f"
localhost:6379> lindex students -1
"a"
localhost:6379> lindex students 2
"d"

9、キュー内の要素値の変更
lset key index value 
インデックスの場所を指定する要素値はvalueで上書きされます
localhost:6379> del students
(integer) 1
localhost:6379> lpush students a b c d e f g
(integer) 7
localhost:6379> lset students -2 x
OK
localhost:6379> lrange students 0 -1
1) "g"
2) "f"
3) "e"
4) "d"
5) "c"
6) "x"
7) "a"
localhost:6379> lset students 0 z
OK
localhost:6379> lrange students 0 -1
1) "z"
2) "f"
3) "e"
4) "d"
5) "c"
6) "x"
7) "a"

 10.指定したインデックス範囲外のすべての要素を削除
ltrim key startIndex endIndex 
インデックス値endIndexのすべての要素を削除
localhost:6379> del students
(integer) 1
localhost:6379> rpush students a b c d e f
(integer) 6
localhost:6379> lrange students 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
localhost:6379> ltrim students 1 -2
OK
localhost:6379> lrange students 0 -1
1) "b"
2) "c"
3) "d"
4) "e"

11.キューに新しい要素を挿入し、挿入後のキュー長を返します.
linsert key before|after oldValue newValue
リストでoldValueを検索し、検索後にその前(before)または後(after)の位置に新しい要素newValueを挿入します.
localhost:6379> del students
(integer) 1
localhost:6379>  rpush students a b c d e f
(integer) 6
localhost:6379> lrange students 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
5) "e"
6) "f"
localhost:6379> linsert students before b x
(integer) 7
localhost:6379> lrange students 0 -1
1) "a"
2) "x"
3) "b"
4) "c"
5) "d"
6) "e"
7) "f"
localhost:6379> linsert students after e z
(integer) 8
localhost:6379> lrange students 0 -1
1) "a"
2) "x"
3) "b"
4) "c"
5) "d"
6) "e"
7) "z"
8) "f"

 12.Aキューの一番右側の要素を削除してBキューの左側に追加し、移動された要素の値を返します.
rpoplpush sourceKey destKey
localhost:6379> rpush A 1 2 3
(integer) 3
localhost:6379> rpush B 7 8 9
(integer) 3
localhost:6379> rpoplpush A  B
"3"
localhost:6379> lrange A 0 -1
1) "1"
2) "2"
localhost:6379> lrange B 0 -1
1) "3"
2) "7"
3) "8"
4) "9"

 
 13.BRPOP 
キューの右側から要素がポップアップされ、要素がない場合はブロックされます. 
BRPOP  key [key …]timeout
パラメータ:key[key]は1つ以上のキューのKEYであり、redisはキューに要素があるかどうかを左から右に検出し、ある場合は直接取り出して返し、すなわちN個のキューに要素がある場合、左側のキュー要素が優先的に取り出し、要素がない場合、あるキューが最初に要素があるまでブロックする.
timeout:タイムアウト時間、単位は秒です.この時間を超えても新しい要素が得られない場合はnilを返します.タイムアウト時間=0の場合、待機時間は制限されません.つまり、新しい要素がリストに追加されていないと、永遠にブロックされます.
 
localhost:6379> del commandList
(integer) 0
localhost:6379> lpush commandList mkdir cd touch
(integer) 3
localhost:6379> brpop commandList 0
1) "commandList"
2) "mkdir"
localhost:6379> brpop commandList 0
1) "commandList"
2) "cd"
localhost:6379> brpop commandList 0
1) "commandList"
2) "touch"
localhost:6379> brpop commandList 0//