This is the 8th day of my participation in the August More Text Challenge

An overview of the

Following on from the previous article, this article continues to document the other two data types in redis data structures: Redis Base – Data types and common commands – on portals

SET type and common commands

The set type

Collection type, which is the data type in Redis that supports list operations

The encoding structure of the set type

  • Intset: The saved elements are all certificates, the total number is less than 512
  • Hashtable: Saved yuan shu is not an integer, the total number is greater than 512

Coding structure

Intset structure

Hashtable coding

Conversion between codes

It is intset and converted to HashTable if the stored data is not an integer value or the number of elements is greater than 512

Common commands

The command Functional description grammar The sample
sadd Add one or more member elements to a collection. Elements already in the collection are ignored. If key is not present, create a set of members in the member element, return an error if key is not set type sadd key member [member …] sadd list 1
srem Remove one or more member elements from the set. Non-existing member elements are ignored. Returns an error when key is not a collection type srem key member [member …] srem list 1
smembers Returns all members of the collection smembers key smembers list
sismember Check whether member becomes a member of the collection. If the member element is a member of the collection, return 1; Otherwise return 0 sismembers key member sismembers list 1
scard Returns the number of elements in the collection scard key scard list
smove Move the member element from the source collection to the destination collection; If the member collection does not exist or does not contain the specified member element, smove does nothing and simply returns. Otherwise, the member element is removed from the source collection and added to the Destination collection smove source destination member smove list list 1
spop Removes and returns a random element from the set, or nil if the set does not exist or is empty spop key spop list
srandmember Returns a random element of the collection. This operation is similar to spop, except that SPOp removes random elements from the collection and returns them, whereas SrandMember simply returns random elements without doing anything to the collection srandmember key srandmember list
sinter Returns all members of a set that is the intersection of all given sets; A set that does not exist is considered empty, and when there is an empty set in a given set, the result is also empty sinter key [key …] sinter list list2
sinterscore This command is the same as sinter, but instead of simply returning the result, it saves it to the Destination set and overwrites it if the destination set already exists; A destionation set can be a key set itself sinterscore destination key [key …] sinterscore list2 list
sunion Returns all members of a set that is the union of all given sets; A set that does not exist is considered empty sunion key [key …] sunion list list2
sunionstore This command is equivalent to sunion, but it saves the result to the Destination collection rather than simply returning the result set; Overwrite the destination if it already exists, which can be the key itself sunionstore destination key [key …] sunionstore list2 list
sdiff Returns the difference set in the specified set. Nonexistent keys are considered empty sdiff key [key …] sdiff list list2
sdiffstore This command is equivalent to sdiff, but it saves the results to the Destination collection rather than simply returning the result set; If the Destination collection already exists, it is overwritten; The destination can be the key itself sdiffstore destination key [key …] sdiffstore list2 list

ZSET type and common commands

Zset type

Ordered collection, the most typical application is to do the ranking function

Common commands

The command Functional description grammar The sample
zadd Add one or more member elements to the ordered set. When a member is already a member of the ordered set, update the score value of the member and reinsert the member element to ensure that the member is in the correct position. The score value can be an integer or a double-precision floating-point value. If the key does not exist, an empty ordered set is created and zadd is performed, and an error is returned if the key does exist but is not an ordered set of types zadd key score member [[score member …]] zadd list 1 create_time
zrem Removes one or more members of the ordered set key, and non-existing members are ignored. An error is returned when the key exists but is not of an ordered collection type zrem key member [[score member …]] zrem list create_time
zcard Returns the number of elements in the ordered set; Returns the number of ordered set elements if key exists, or 0 if key does not zcard key zcard key
zcount Returns the number of members of the ordered set key whose score is between min and Max zcount key min max zcount list 0 4
zscore Returns score of member in you, or nil if the member element is not a member of key, or if key does not exist zscore key member zscore list create_time
zincrby Add increment to score value of member of ordered set key zscore key member zscore list create_time
zrange Returns the members of an ordered set key, whose positions are sorted in ascending order by score zrange key start stop [WITHSCORES] zrange list 0 2
zrevrange Returns the members of the ordered set key within the specified range, whose positions are sorted by incrementing (inverted) score zrevrange key min max [WITHSCORES] [limit offset count] zrevrange list 0 2
zrangebyscore Returns all members of the ordered set key whose score is between min and Max. The ordered set members increase according to score value; Members with the same score are sorted in dictionary order zrangebyscore key start stop [WITHSCORES] zrangebyscore list 0 2
zrevrangebyscore Returns all members of the ordered set key whose score is between min and Max. The ordered set members increase according to score value; Those with the same score members are arranged according to the dictionary’s fanxu zrevrangebyscore key max min [WITHSCORES] zrevrangebyscore list 2 0
zrank Returns the ranking of members in the ordered set key. The members of the ordered set are ranked according to the order of score, with the ranking at the bottom of 0, that is, the member with the lowest score is ranked 0. Returns the ranking of member if it is an ordered collection key member, nil otherwise zrank key member zrank list create_time
zrevrank Returns the ranking of members in the ordered set key. The members of the ordered set are arranged in reverse order according to the score value, ranking at the bottom of 0, that is, the member with the highest score value is ranked 0. Returns the ranking of member if it is an ordered collection key member, nil otherwise zrevrank key member zrevrank list create_time
zremrangebyrank Remove ordered setkeyTo specify all members of the rank range. zremrangebyrank key start stop zremrangebyrank list 0 1
zremrangebyscore Remove ordered setkey, all members whose score is between min and Max zremrangebyscore key min max zremrangebyscore list 0 1