Redis series-set types and Zset types

Source: Internet
Author: User
Tags new set
set Type

Set is a collection, which is an unordered collection of string types.

Set is implemented through a hash table, and the complexity of adding, deleting, and finding is 0 (1).

We can take the set, the intersection, the difference set.

Sadd add elements to set with name key

Sadd myset "Hello"

Smembers View Collection

Smembers MySet

Srem Delete Element

Srem MySet "One" returns 1 delete successfully returned 0 delete failed



Spop randomly returns and deletes an element of the set with the name key

Spop MySet


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

Sdiff Myset1 Myset2 (in front of the standard)



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

Sdiffstore myset3 myset1 Myset2 # Save Myset1 Myset2 's difference set to Myset3 (base on Myset1)



Sinter returns the intersection of the given key

Sinter Myset4 Myset5



Sinterstore stores the intersection to a different collection Sinterstore Myset6 Myset1 Myset2 (similar to Sdiffstore)


Sunion and set

Sunion Myset1 Myset2

Sunionstore the collection to the new set

Sunionstore myset7 Myset1 Myset2 #返回插入的元素个数



Smove removes member from the set corresponding to the first key and adds it to the second corresponding set

Smove Myset2 Myset7 Three #成功返回1 failed to return 0



SCard view the number of collections SCard Myset2


Sismember test whether member is a set element with the name key

Sismember Myset2 Two is returning 1 is not returning 0


Srandmember Random return (because set unordered)

Srandmember Myset7


sorted Sets Type

Sorted set is an upgraded version of set that adds an ordered attribute to the set, which can be specified when adding a modified element.

After each designation, Zset automatically adjusts the new value by adjusting the order, which can be understood as a list of two-column MySQL, a list of stored values, and a sequence of stored orders.

Where key is understood as Zset's name.

Zadd add Element

Zadd Myzset 1 "one"

Zadd Myzset 2 "two"

Zadd Myzset 3 "three"

Zrange Myzset 0-1 Withscores

Here the 0 and-1 represent the index withscores output sequence number



Zrem Delete elements in Zset with name key

Zrem Myzset Two


Zincrby to increment (decrease) The order logarithmic ordinal number with a specified value.

If the element member already exists in the Zset named Key, the Sroce of the element is incremented increment,

Otherwise, the element is added to the collection, and its score value is Incrnment

Zincrby Myzset 2 One


Like four, this element doesn't exist.



Zrank returns the rank of the member element in the Zset named key (sorted by score from small to large) that is subscript

Zrank Myzset Two



Zrevrank reverse according to score from big to small rank Zrevrank Myzset two


Zrevrange Reverse Descending sort

Zrevrange Myzset 0-1 Withscores


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.