Iv. operations related to the sets type of redis (ordered and unordered sets)

Source: Internet
Author: User
Four or five types (ordered and unordered sets): sets (that is, sets) An Introduction: set indicates a set, and add is randomly added ----- unordered set is a set, it is an unordered set of the string type. Set is implemented through hashtable. The complexity of adding, deleting, and searching is 0 (1 ). We

=====================Four or five types (sorted and unordered sets): sets (that is, sets) ============== 1 Introduction: set indicates a set, which is randomly added. ----- A disordered set is a set, it is an unordered set of the string type. Set is implemented through hash table. The complexity of adding, deleting, and searching is 0 (1 ). We

=====================Four or five types (sorted and unordered sets): sets (that is, sets) ==================
1. Introduction: set indicates a set, which is randomly added -----> unordered set.

Set is a collection, which is an unordered set of the string type.
Set is implemented through hash table. The complexity of adding, deleting, and searching is 0 (1 ).
For a set, we can take the Union, intersection, and difference set.
With this write operation, we can implement the friend recommendation and blog tag FUNCTIONS In sns.

1: sadd
Add an element (unique) to the set named key)
Example: sadd myset1 one ----> Add one element to the set myset1
Sadd myset1 two
View: smembers myset1
2: srem
Delete the elements in a set named key
Example: sadd myset2 one
Srem myset2 one ---> delete one element
3: spop
Randomly return and delete an element in a set named key
Example: spop myset3 ---> randomly pop up the deleted Element
4: sdiff
Returns the difference set between all given keys and the first key.
Myset1 = myset2 = -- return 1
For example, sdiff myset1 myset2 ----> who follows the standard
4: sdiffstore
Returns the difference set between all given keys and the first key, and stores the result in a new key.
For example, sdiffstore myset4 myset2 myset3 ----> pop up the difference set between myset2 and myset3 and save it to myset4
5: sinter
Returns the intersection of all given keys (intersection: the same element)
Two, one three, one ---> one
For example, the intersection between sinter myset2 myset3 --->

6: sinterstore
Returns the intersection of all given keys and the first key, and stores the result in a new key.
Example: sinterstore myset4 myset2 myset3 ----> Save the intersection of myset2 and myset3 to myset4
7: sunion
Returns the union of all given keys.
Example: sunion myset1 myset2 ---> Get Union
8: sunionstore
Returns the union of all given keys and saves them to the new key.
Example: sunionstore myset4 myset1 myset2 ---> fetch and save the Union to myset4
9: smove
Removes an element from the set corresponding to the first key and adds it to the second key.
For example, smove myset2 myset7 three ---> move the elements in myset2 to myset7
10: scard
Returns the number of set elements named key.
For example, scard myset8 ----> returns the number of elements.
11: sismember
Test whether an element is an element in a set named KEY.
For example, sismember myset2 two ---> test whether two is an element in myset2.
12: srandmember (unordered retrieval)
Returns an element of a set named key without deleting the element.
Example: srandmember myset3 ---> randomly remove elements of myset3
13:

II. Introduction: sorted sets ======== "Ordered sets

Sortes set is an upgraded version of set. It adds an ordered Attribute Based on set,
This attribute can be specified when you add and modify elements. After each attribute is specified, zset automatically adjusts the order according to the new values.
It can be understood that there are two columns of MySQL tables, one column stores value and one column stores order. In the operation, the key is interpreted as the name of zset (the name of the sorted set ).
1: zadd --- duplicate Inserts will fail and the sequence will be updated
Add the member element to the zset named key. score is used for sorting. If this element exists, the order is updated.
For example, zadd myzset1 1 one ---> insert one and specify the sequence number
Zrange
Value: zrange myzset1 0-1 -----> all elements of myzset1
Zrange myzset1 0-1 withscores ----> retrieve the element and output it by serial number.
2: zrem deletes an element in an ordered set.
Delete the element member in the zset named key
Example: zrem zset1 two ---> Delete the two element
3: zincrby
If the element member already exists in the zset named key, change the score of the element.
Add an increment (sequence number). Otherwise, add an element to the set. The score value is increment.
For example, zincrby ssett1 2 one ----> minus the Sn
Zincrby ssett1-2 one ----> Add the sequence number
4: zrank
Returns the ranking of the member element in the zset named key (sorted by score in ascending order), that is, the subscript.
Example: zrange zset2 0-1 withscores
Zrank zset2 four ----> returns the index value of an element.
5: zrevrank
Returns the ranking of the member element in the zset named key (sorted by score in ascending order), that is, the subscript.
For example, zrevrank zset2 four ----> is opposite to zrank
6: zrevrange
Values are sorted in descending order (scores are sorted in ascending order)
Example: zrevrange zset2 0-1 withscoress
7: zrangebyscore
Returns the element of the score in the given area in the set.
For example, zrangebyscore zset2 2 4 withscores ---> returns the results in an ordered order with subscripts 2, 3, and 4.
8: zcount
Returns the number of scores in a given area in a set.
For example, zrangebyscore zset2 2 4 -----> 2, 3, and 4 are returned, indicating that three elements exist.
9: zcrad
Returns the number of elements in the set.
Zcrad zset2 ----> Number of all elements in zset2
10: zremrangebyrank
Delete the elements that rank in the specified range in the collection (delete by index)
Example: zremrangebyrank zset2 1 1 ----> Delete (1), 2, 3 (Delete 2 and 3) with the index from 1 to 1)
11: zremrangebyscore
Delete the element of the score range in the Set (delete by sequence number)
Example: zremrangebyscore zset2 2 5 ---> delete a sequence number from 2 to 5

========================================================== END ===================================================== =

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.