Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are lpush, rpush, lset and lrem in redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what lpush, rpush, lset and lrem are in redis. I hope you will gain a lot after reading this article. Let's discuss it together.

1.lpush

Add a string element to the header of the corresponding list of key

2.rpush

Add a string element to the tail of the corresponding list of key

3.linsert

Add string elements before or after the specific position of the key corresponding to the list

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

Here we insert a hello, then a world at the end of the hello, and then a world in the world

There was inserted in the front.

4.lset

Sets the element value of the specified subscript in the list (subscript starts at 0)

Redis 127.0.0.1 1redis 127.0.0.1 (integer) 1redis 127.0.0.1 1redis 6379 > rpush mylist4 "two" (integer) 2redis 127.0.0.1 3redis 127.0.0.1 (integer) 3redis 127.0.0.16379 > lset mylist4 0 "four" OKredis 127.0.0.11redis 6379 > lset mylist4-2 "five" OKredis 127.0.0.1) four "2)" five "3)" three "redis 127.0.1

Here we insert one,two,three in turn, then set the value of 0 to four, and then set the value of subscript-2

Set it to five.

5.lrem

Delete count elements that are the same as value from the corresponding list of key.

When count > 0, delete it from beginning to end, as shown below:

Redis 127.0.0.1 1redis 127.0.0.1 (integer) 1redis 127.0.0.1 1redis 6379 > rpush mylist5 "hello" (integer) 2redis 127.0.0.1 3redis 127.0.0.1 3redis 127.0.0.1 (integer) 3redis 127.0.0.16379 > rpush mylist5 "hello" (integer) 4redis 127.0.0.1 4redis 127.0.1 4redis 6379 > lrem mylist5 2 "hello" (integer) 2redis 127.0.1 2redis 127.0.1) "foo" 2) "hello" redis 127.0.1

Count rpush mylist6 "hello" (integer) 1redis 127.0.0.1 rpush mylist6 "hello" (integer) 2redis 127.0.0.1 integer > rpush mylist6 "foo" (integer) 3redis 127.0.0.1 3redis 127.0.0.1 > rpush mylist6 "hello" (integer) 4redis 127.0.0.1 > lrem mylist6-2 "hello" (integer) 2redis 127.0.1 > lrange mylist6 0-11) "hello" 2) "foo" redis 127.0.1

When count=0, delete all of them, as follows:

Redis 127.0.0.1 1redis 127.0.0.1 (integer) 1redis 127.0.0.1 1redis 6379 > rpush mylist7 "hello" (integer) 2redis 127.0.0.1 3redis 127.0.0.1 (integer) 3redis 127.0.0.16379 > rpush mylist7 "hello" (integer) 4redis 127.0.0.1 4redis 127.0.0.1 4redis 6379 > lrem mylist7 0 "hello" (integer) 3redis 127.0.1 3redis 6379 > lrange mylist7 0-11) "foo" redis 127.0.1 I believe you have a certain understanding of lpush, rpush, lset and lrem in redis. If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report