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

Common command collation of Redis collection types

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the common command arrangement of Redis collection type". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Redis collection type of common commands collation" bar!

Redis common commands organize collection types

Add delete command

Sadd key member [member.... ]

The sadd command is used to add one or more elements to the collection and is automatically created if the key does not exist. Because there cannot be the same element in a collection, it will be ignored if the element you want to add already exists in the collection. The return value is the number of elements successfully added (ignored elements are not counted)

Srem key member [member.... ]

The srem command removes one or more elements from the collection and returns the number of successful deletions.

Get all the elements in the collection

Smembers key

Returns all elements in the collection

Determine whether the element is in the collection

Sismember key member

Judging whether an element is in a collection is an operation with a time complexity of 0 (1). No matter how many elements there are in the collection, the sismember command can always return the result very quickly. The sismember command returns 1 when the value exists and 0 if the value does not exist or the key does not exist

Inter-set operation

Sdiff key [key. ]

The sdiff command is used to perform subtraction operations on multiple sets. The difference between set An and set B is represented as A-B, which represents the set of all elements that belong to An and do not belong to B, that is,

A-B = {x | x ∈ An and x ∈ / B}

How to use the command:

Sadd seta 1 2 3 4 6 7 8

Sadd setb 2 3 4

Sdiff seta setb

This command allows multiple keys to be passed in at the same time. The order of calculation is to calculate the difference between seta and setb in the calculation result and setc.

Sadd setc 2 3 4

Sdiff seta setb setc

Sinter key [key. ]

This command is used to perform intersection operations on multiple sets. The intersection of set An and set B is represented by A ∩ B, which represents the set of all elements belonging to An and B.

That is, A ∩ B = {x | x ∈ An and x ∈ B}

How to use the command:

Sinter seta setb

This command also supports passing in multiple keys at the same time

Sunion key [key. ]

This command is used to perform union operations on multiple sets. The union of set An and set B is represented as A ∪ B, which represents the set of all elements belonging to An or B.

That is, A ∪ B = {x | x ∈ An or x ∈ B}

How to use the command:

Sunion seta setb

This command also supports passing in multiple keys at the same time

Get the number of elements in the collection

Scard key

Returns the cardinality of the collection. Returns 0 when key does not exist.

Perform set operations and store the results

Sdiffstore destination key [key. ]

The sdiffstore command has the same function as the sdiff command, the only difference is that the former does not directly return the result of the operation, but stores the result in the destination key

Sinterstore destination key [key. ]

The sinterstore command is similar to the sinter command, but it saves the results to the destination collection rather than simply returning the result set.

Sunionstore destination key [key. ]

The sunionstore command is similar to the sunion command, except that it saves the results to the destination collection rather than simply returning the result set.

Randomly get the elements in the set

Srandmember key [count]

This command is used to randomly get an element from the collection

You can also pass the count parameter to get multiple elements randomly at one time, and the specific performance is different according to the positive or negative of count.

When count is positive, srandmember randomly fetches count non-repeating elements from the collection. If the value of count is greater than the number of elements in the collection, srandmember returns all elements in the collection

When count is negative, srandmember will randomly get the elements of | count | from the collection. These elements may be the same.

Note: when passing count parameters, prompt command parameters are incorrect in windows environment

Pop up an element from the collection

Spop key

Because the elements of the collection type are unordered, the spop command pops up an element randomly selected from the collection, returns a random element that has been removed, and returns nil if key does not exist or key is empty.

At this point, I believe that you have a deeper understanding of the "common command collation of Redis collection types". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Internet Technology

Wechat

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

12
Report