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

How to use set types in Redis

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

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to use the set type in Redis, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Set is an unordered collection of type string. It is realized through hash table.

Sadd: add elements to the set named key. You cannot add duplicate elements.

127.0.0.1 purl 6379 > sadd myset hello

(integer) 1

127.0.0.1 purl 6379 > sadd myset world

(integer) 1

127.0.0.1 purl 6379 > sadd myset world

(integer) 0

127.0.0.1 purl 6379 > smembers myset

1) "hello"

2) "world"

Srem: deletes the element in the set named key.

127.0.0.1 purl 6379 > sadd myset2 one

(integer) 1

127.0.0.1 purl 6379 > sadd myset2 two

(integer) 1

127.0.0.1 purl 6379 > srem myset2 one

(integer) 1

127.0.0.1 purl 6379 > smembers myset2

1) "two"

Spop: randomly returns and deletes an element in the set named key.

127.0.0.1 purl 6379 > sadd myset3 one

(integer) 1

127.0.0.1 purl 6379 > sadd myset3 two

(integer) 1

127.0.0.1 purl 6379 > spop myset3

"two"

127.0.0.1 purl 6379 > smembers myset3

1) "one"

Sdiff: returns the difference between all the given key and the first key

127.0.0.1 purl 6379 > smembers myset2

1) "three"

2) "two"

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > sdiff myset2 myset3

1) "three"

Sdiffstore: returns the difference between all the given key and the first key and saves the result as another key.

127.0.0.1 purl 6379 > sdiffstore myset4 myset2 myset3

(integer) 1

127.0.0.1 purl 6379 > smembers myset4

1) "three"

Sinter: returns the intersection of all given key.

127.0.0.1 purl 6379 > smembers myset2

1) "three"

2) "two"

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > sinter myset2 myset3

1) "two"

Sinterstore: returns the intersection of all given key and saves the result as another key.

127.0.0.1 purl 6379 > smembers myset2

1) "three"

2) "two"

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > sinterstore myset6 myset2 myset3

(integer) 1

127.0.0.1 purl 6379 > smembers myset6

1) "two"

Sunion: returns the union of all given key.

127.0.0.1 purl 6379 > smembers myset2

1) "three"

2) "two"

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > sunion myset2 myset3

1) "two"

2) "three"

3) "one"

Sunionstore: returns the union of all given key and saves the result as another key.

127.0.0.1 purl 6379 > smembers myset2

1) "three"

2) "two"

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > sunionstore myset6 myset2 myset3

(integer) 3

127.0.0.1 purl 6379 > smembers myset6

1) "two"

2) "three"

3) "one"

Smove: remove the member from the set corresponding to the first key and add it to the second corresponding set.

127.0.0.1 purl 6379 > smembers myset2

1) "three"

2) "two"

127.0.0.1 purl 6379 > smove myset2 myset7 three

(integer) 1

127.0.0.1 purl 6379 > smembers myset2

1) "two"

127.0.0.1 purl 6379 > smembers myset7

1) "three"

Scard: returns the number of elements of the set named key.

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > scard myset3

(integer) 2

Sismember: test whether member is an element of a set named key

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > sismember myset3 two

(integer) 1

127.0.0.1 purl 6379 > sismember myset3 three

(integer) 0

Srandmember: randomly returns an element of set with the name key, but does not delete the element.

127.0.0.1 purl 6379 > smembers myset3

1) "two"

2) "one"

127.0.0.1 purl 6379 > srandmember myset3

"one"

127.0.0.1 purl 6379 > srandmember myset3

"two"

The above is how to use the set type in Redis. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Database

Wechat

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

12
Report