關于redis之lpush、rpush、lset、lrem

下面由redis教程欄目給大家介紹redis之lpush、rpush、lset、lrem,希望對需要的朋友有所幫助!

關于redis之lpush、rpush、lset、lrem

1.lpush

在key對應 list的頭部添加字符串元素

2.rpush

在key對應 list 的尾部添加字符串元素

3.linsert

在key對應 list 的特定位置之前或之后添加字符串元素

redis?127.0.0.1:6379>?rpush?mylist3?"hello" (integer)?1 redis?127.0.0.1:6379>?rpush?mylist3?"world" (integer)?2 redis?127.0.0.1:6379>?linsert?mylist3?before?"world"?"there" (integer)?3 redis?127.0.0.1:6379>?lrange?mylist3?0?-1 1)?"hello" 2)?"there" 3)?"world" redis?127.0.0.1:6379>

在此處我們先插入了一個 hello,然后在 hello 的尾部插入了一個 world,然后又在 world 的

前面插入了 there。

4.lset

設置list中指定下標的元素值(下標從0開始)

redis?127.0.0.1:6379>?rpush?mylist4?"one" (integer)?1 redis?127.0.0.1:6379>?rpush?mylist4?"two" (integer)?2 redis?127.0.0.1:6379>?rpush?mylist4?"three" (integer)?3 redis?127.0.0.1:6379>?lset?mylist4?0?"four" OK redis?127.0.0.1:6379>?lset?mylist4?-2?"five" OK redis?127.0.0.1:6379>?lrange?mylist4?0?-1 1)?"four" 2)?"five" 3)?"three" redis?127.0.0.1:6379>

在此處我們依次插入了 one,two,three,然后將標是 0 的值設置為 four,再將下標是-2的值設

置為 five。

5.lrem

從key對應 list 中刪除 count 個和 value 相同的元素。

count>0 時,按從頭到尾的順序刪除,具體如下:

redis?127.0.0.1:6379>?rpush?mylist5?"hello" (integer)?1 redis?127.0.0.1:6379>?rpush?mylist5?"hello" (integer)?2 redis?127.0.0.1:6379>?rpush?mylist5?"foo" (integer)?3 redis?127.0.0.1:6379>?rpush?mylist5?"hello" (integer)?4 redis?127.0.0.1:6379>?lrem?mylist5?2?"hello" (integer)?2 redis?127.0.0.1:6379>?lrange?mylist5?0?-1 1)?"foo" 2)?"hello" redis?127.0.0.1:6379>

count

redis?127.0.0.1:6379>?rpush?mylist6?"hello" (integer)?1 redis?127.0.0.1:6379>?rpush?mylist6?"hello" (integer)?2 redis?127.0.0.1:6379>?rpush?mylist6?"foo" (integer)?3 redis?127.0.0.1:6379>?rpush?mylist6?"hello" (integer)?4 redis?127.0.0.1:6379>lrem?mylist6?-2?"hello" (integer)?2 redis?127.0.0.1:6379>?lrange?mylist6?0?-1 1)?"hello" 2)?"foo" redis?127.0.0.1:6379>

count=0 時,刪除全部,具體如下:

redis?127.0.0.1:6379>?rpush?mylist7?"hello" (integer)?1 redis?127.0.0.1:6379>?rpush?mylist7?"hello" (integer)?2 redis?127.0.0.1:6379>?rpush?mylist7?"foo" (integer)?3 redis?127.0.0.1:6379>?rpush?mylist7?"hello" (integer)?4 redis?127.0.0.1:6379>?lrem?mylist7?0?"hello" (integer)?3 redis?127.0.0.1:6379>?lrange?mylist7?0?-1 1)?"foo" redis?127.0.0.1:6379>

以上就是關于

? 版權聲明
THE END
喜歡就支持一下吧
點贊14 分享