Redis actual combat 04. Brief introduction to common commands of redis data structure

Time:2021-3-27

character stringP39

Redis string is a sequence of bytes, which can store the following three types of values: byte string, integer and floating-point number.

Redis converts integers to floating-point numbers when needed. The range of integer is the same as that of long, and the range and precision of floating-point number are the same as that of double in IEEE 754.

Add and subtract commands in redisP39
command format describe
INCR INCR key Add 1 to the numeric value stored by the key
DECR DECR key Subtract 1 from the numeric value stored by the key
INCRBY INCRBY key increment Adds an integer increment to the numeric value stored by the key
DECRBY DECRBY key decrement Subtract the integer decrement from the numeric value stored by the key
INCRBYFLOAT INCRBYFLOAT key increment Adds the numeric value stored by the key to the floating-point increment increment

The relevant demo code is as follows(mainandhandleResultFor definitions, see: 01. Introduction to redis data structure)

//Performs string type number dependent operations
func executeNumberOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "number")))
    //Get value, output - > error: redigo: Nil returned
    handleResult(redis.Int(conn.Do("GET", "number")))
    //Auto increment 1, return the value after auto increment
    handleResult(redis.Int(conn.Do("INCR", "number")))
    //Auto increment 2, return the value after auto increment
    handleResult(redis.Int(conn.Do("INCRBY", "number", "2")))
    //Subtract 1, return the value after subtraction
    handleResult(redis.Int(conn.Do("DECR", "number")))
    //Subtract 2, return the value after subtraction
    handleResult(redis.Int(conn.Do("DECRBY", "number", "2")))
    //Auto increment 1.5, return the value after auto increment - > 1.5
    handleResult(redis.Float64(conn.Do("INCRBYFLOAT", "number", "1.5")))
    //Auto increment - 1.3, return the value after auto increment - > 0.2
    handleResult(redis.Float64(conn.Do("INCRBYFLOAT", "number", "-1.3")))
}
Commands for redis to process substrings and binary bitsP40
command format describe
APPEND APPEND key value Append value to the end of the current value of the key
GETRANGE GETRANGE key start end Returns the substring within the range of [start, end]
SETRANGE SETRANGE key offset value Set the substring [offset, offset + len (value)) to value
GETBIT GETBIT key offset The string is regarded as a binary bit string to get the bits on offset
SETBIT SETBIT key offset value Consider offset as a string bit value
BITCOUNT BITCOUNT key [start end] Count the number of 1s of the substring in the range of [start, end] under binary
BITOP BITOP operation destkey key [key …] Operation optional bit operations and, or, XOR, not, save the operation results of one or more binary bit strings to destkey

The relevant demonstration code is as follows:

//Performs string type string related operations
func executeStringOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "string")))
    //Appends the string, returns the current string length of 6, and the value changes to append
    handleResult(redis.Int(conn.Do("APPEND", "string", "append")))
    //Get substring, return to
    handleResult(redis.String(conn.Do("GETRANGE", "string", 3, 4)))
    //Set the substring to return the current string length of 6, and the value changes to applied
    handleResult(redis.Int(conn.Do("SETRANGE", "string", 3, "le")))
    //Set the substring, return the current string length to 11, and change the value to application
    handleResult(redis.Int(conn.Do("SETRANGE", "string", 3, "lication")))
    //Get binary bit, return to - > 1
    //(get the binary bit of the 7th / 8th character a on the 7th% 8th bit under binary, that is, the 7th bit 1 of 0110 0001)
    handleResult(redis.Int(conn.Do("GETBIT", "string", 7)))
    //Set the binary bit, return the original binary bit - > 0, and change the value to - > cpplication
    //(set the binary bit of the 6th / 8th character a on the 6th% 8th bit of binary to 1, that is, 0110 0001 becomes 0110 0011)
    handleResult(redis.Int(conn.Do("SETBIT", "string", 6, 1)))
    //Statistics binary bits, return to 7
    //(count the number of substrings CP in the range of [0, 1] with the binary bits of 0110 0011 0111 0000 being 1)
    handleResult(redis.Int(conn.Do("BITCOUNT", "string", 0, 1)))

    handleResult(redis.String(conn.Do("SET", "aKey", "aa")))
    handleResult(redis.String(conn.Do("SET", "bKey", "b")))
    //Perform bitwise or on AA (0110 0001 0110 0001) and B (0110 0010 0000 0000), and store the result in ckey
    //Returns the string length of 2, the value is Ca (0110 0011 0110 0001),
    handleResult(redis.Int(conn.Do("BITOP", "OR", "cKey", "aKey", "bKey")))
}

Redis can cooperate by using substring operation and binary bit operationWATCHMULTIandEXECCommand (which will be introduced initially later, and will be explained in depth later) to build any data structure you want.

listP42

Some common list commandsP42
command format describe
RPUSH RPUSH key value [value …] Insert one or more values from the right end of the list in turn
LPUSH LPUSH key value [value …] Insert one or more values from the left end of the list in turn
RPOP RPOP key Remove and return the rightmost element of the list
LPOP LPOP key Removes and returns the leftmost element of the list
LINDEX LINDEX key offset Returns the element with offset at the left end of the list
LRANGE LRANGE key start end Returns all elements within the range of [start, end] from the left end of the list
LTRIM LTRIM key start end Remove all elements outside the range of [start, end] from the left end of the list

The relevant demonstration code is as follows:

//Perform list type related operations
func executeListOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "list")))
    //Insert a, B, C at the right end, return the current list length to 3, and the list becomes a, B, C
    handleResult(redis.Int(conn.Do("RPUSH", "list", "a", "b", "c")))
    //Insert D, e, f once at the left end, return the current list length to 6, and the list changes to f e d a B C
    handleResult(redis.Int(conn.Do("LPUSH", "list", "d", "e", "f")))
    //Pop up and return the value at the right end of the list, return to C, and the list becomes f e d a B
    handleResult(redis.String(conn.Do("RPOP", "list")))
    //Pop up and return the leftmost value of the list, return to F, and the list becomes e d a B
    handleResult(redis.String(conn.Do("LPOP", "list")))
    //Return the value whose left end subscript offset is offset, and return the value of - > D
    handleResult(redis.String(conn.Do("LINDEX", "list", 1)))
    //Remove all the elements outside the range of [1,2] from the left end of the list, and the list will be changed to - > d a
    handleResult(redis.String(conn.Do("LTRIM", "list", 1, 2)))
}

utilizeLTRIMCommands can atomically pop up multiple elements.P43

And the block list between commandsP43
command format describe
BLPOP BLPOP key [key …] timeout Pop up the leftmost element from the first non empty list, or block within timeout seconds and wait for the pop-up element to appear. Return the name of the pop-up list and the element. A timeout of 0 means infinite waiting
BRPOP BRPOP key [key …] timeout Pop up the rightmost element from the first non empty list, or block within timeout seconds and wait for the pop-up element to appear. Return the name of the pop-up list and the element. A timeout of 0 means infinite waiting
RPOPLPUSH RPOPLPUSH source destination Pop up the rightmost element from the source list, then exit the element from the leftmost end of the destination list and return the element
BRPOPLPUSH BRPOPLPUSH source destination timeout Pop up the rightmost element from the source list, then exit the element from the leftmost end of the destination list and return the element; if the source list is empty, block and wait for the pop-up element to appear within timeout seconds

The relevant demonstration code is as follows:

//Perform list type blocking related operations
func executeListBlockOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "source", "destination")))
    //Pop up from the first non empty list and return the leftmost value of the list, wait for 1 second at most, and output - > error: redigo: Nil returned
    handleResult(redis.Strings(conn.Do("BLPOP", "source", "destination", 1)))

    //Initialization
    handleResult(redis.Int(conn.Do("RPUSH", "source", "a", "b", "c")))
    handleResult(redis.Int(conn.Do("RPUSH", "destination", "d", "e", "f")))
    //Pop up from the first non empty list and return the leftmost value of the list, wait indefinitely and return to a, source changes to B, C, destination changes to D, e, F
    handleResult(redis.Strings(conn.Do("BLPOP", "source", "destination", 0)))
    //Pop up from the first non empty list and return the value at the rightmost end of the list, wait indefinitely, and return to the following value: F, source changes to B, C, destination changes to D, e
    handleResult(redis.Strings(conn.Do("BRPOP", "destination", "source", 0)))

    //Pop up the rightmost element from source, then push it to the leftmost end of destination, and return the element
    //Return to C, source to B, destination to c d e
    handleResult(redis.String(conn.Do("RPOPLPUSH", "source", "destination")))
    //Pop up the rightmost element from source, then push it to the leftmost end of destination, and return the element,无限等待
    //Return to B, the source changes to - > < nil >, and the destination changes to B C D E
    handleResult(redis.String(conn.Do("BRPOPLPUSH", "source", "destination", 0)))
    //Pop up the rightmost element from source, then push it to the leftmost end of destination, and return the element,最多等待 1秒
    //Output - > error: redigo: Nil returned, source - > < nil >, destination - > < b c d e
    handleResult(redis.String(conn.Do("BRPOPLPUSH", "source", "destination", 1)))
}

For blocking pop-up commands and pop-up and push commands, the most common use cases are messaging and task queue, which will be introduced later.P44

Exercise: reduce memory consumption by listingP44

In the last article, we use ordered set to record the products that users have browsed recently, and set the time stamp of users browsing these products to the score value, so that the program can carry out the corresponding data analysis in the process of cleaning up the old session or after the purchase operation. However, due to the time stamp needs to occupy the corresponding space, if the analysis operation does not need to use the time stamp, then there is no need to use the ordered set to save the products that the user has recently browsed. To do this, set theUpdateTokenFunction is used to replace the ordered set with a list.

Tip: if you encounter difficulties in answering this question, you can find inspiration in section 6.1.1.

  • Because the list is ordered, all the latest accesses must be at the left end of the list. Therefore, in each operation, first delete the access record in the list, then push it to the left end of the list, and finally trim the list to a length of 25. Due to the need to traverse the entire list each time, the time complexity is high, but the total length of the list is only 25, the time difference is not big, but the space can be saved a lot.

    //Update recent product access list
    func UpdateLatestViewedItem(conn redis.Conn, itemId int) {
        //Remove all elements in the list with the value of itemid
        _ = conn.Send("LREM", "latestViewedItem", 0, itemId)
        //Push the most recently visited items to the far left of the list
        _ = conn.Send("LPUSH", "latestViewedItem", itemId)
        //Prune the list to keep the most recently accessed 25
        _ = conn.Send("LTRIM", "latestViewedItem", 0, 24)
        //Execute the above order
        _ = conn.Flush()
    }

aggregateP44

Some common collection commandsP45
command format describe
SADD SADD key member [member …] Adds one or more elements to a collection, returns the number of new elements added to the collection (excluding existing elements)
SREM SREM keymember [member …] Delete one or more elements from the collection, and return the elements successfully deleted from the collection (excluding those that do not exist)
SISMEMBER SISMEMBER key member Judge whether the element member is in the set key
SCARD SCARD key Returns the number of elements in the union
SMEMBERS SMEMBERS key Returns all elements of the collection
SRANDMEMBER SRANDMEMBER key [count] Returns one or more elements of a collection at random. When count is a positive number, it returns count of different elements (up to the whole set); when count is a negative number, it returns\ count\ There is no limit on the maximum length of elements that may repeat.
SPOP SPOP key Randomly removes and returns an element in the collection
SMOVE SMOVE source destination member Move the element member from the collection source to the collection destination

The relevant demonstration code is as follows:

//Perform collection type related operations
func executeSetOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "source", "destination")))
    //Add three elements to the collection, output is > 6, and source is changed to > 1 2 3 4 5 6 7
    handleResult(redis.Int(conn.Do("SADD", "source", 1, 2, 3, 4, 5, 6, 7, 1)))
    //Remove two elements from the collection: 1, 2, output - > 2, source changes to - > 3, 4, 5, 6, 7
    handleResult(redis.Int(conn.Do("SREM", "source", 1, 2)))
    //Judge whether the set contains element 3, output - > 1
    handleResult(redis.Int(conn.Do("SISMEMBER", "source", 3)))
    //Return the number of elements in the set, output - > 5
    handleResult(redis.Int(conn.Do("SCARD", "source")))
    //Return all elements of the set, output - > [3 4 5 6 7]
    handleResult(redis.Ints(conn.Do("SMEMBERS", "source")))
    //Randomly return three different elements in the set, output - > [6 5 3] (random results may be different, subject to the actual situation)
    handleResult(redis.Ints(conn.Do("SRANDMEMBER", "source", 3)))
    //Randomly return 6 repeatable elements in the set, output - > [7 5 6 3 7 6] (random results may be different, subject to the actual situation)
    handleResult(redis.Ints(conn.Do("SRANDMEMBER", "source", -6)))
    //Randomly delete one element in the set, and the output will be changed to 3, and the source will be changed to 4 5 6 7 (random results may be different, subject to the actual situation)
    handleResult(redis.Int(conn.Do("SPOP", "source")))
    //Move the element 7 in the source set to the destination set (due to the randomness in the front, the results may be different, subject to the actual situation)
    //Output is changed to 1, source is changed to 4, 5, 6, and destination is changed to 7
    handleResult(redis.Int(conn.Do("SMOVE", "source", "destination", 7)))
}
Commands for combining and processing multiple setsP45
command format describe
SDIFF SDIFF key [key …] Returns elements that exist in the first set but not in other sets (difference sets)
SDIFFSTORE SDIFFSTORE destination key [key …] Stores the elements (difference sets) that exist in the first set but not in other sets in destination, and returns the difference set size
SINTER SINTER key [key …] Returns elements (intersections) that exist in all sets at the same time
SINTERSTORE SINTERSTORE destination key [key …] Store the elements (intersection) that exist in all sets at the same time in destination, and return the intersection size
SUNION SUNIONkey [key …] Returns elements (Union) that exist in at least one collection
SUNIONSTORE SUNIONSTORE destination key [key …] Stores the elements (Union) that exist in at least one collection in destination and returns the union size

The relevant demonstration code is as follows:

//Performs multiple collection related operations for a collection type
func executeSetMutiOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "source_1", "source_2", "source_3", "destination")))
    //Initialization
    handleResult(redis.Int(conn.Do("SADD", "source_1", 1, 2, 4, 8)))
    handleResult(redis.Int(conn.Do("SADD", "source_2", 2, 3, 4, 5)))
    handleResult(redis.Int(conn.Do("SADD", "source_3", 5, 6, 7, 8)))
    //Return the difference set of three sets, output - > [1]
    handleResult(redis.Ints(conn.Do("SDIFF", "source_1", "source_2", "source_3")))
    //The difference set of three sets is stored in the destination, and the output is - > 1, and the destination becomes - > 1
    handleResult(redis.Int(conn.Do("SDIFFSTORE", "destination", "source_1", "source_2", "source_3")))
    //Return the intersection of two sets, output - > [2,4]
    handleResult(redis.Ints(conn.Do("SINTER", "source_1", "source_2")))
    //The intersection of two sets is stored in the destination, and the output is changed to 2, and the destination is changed to 2, 4
    handleResult(redis.Int(conn.Do("SINTERSTORE", "destination", "source_1", "source_2")))
    //Return union of three sets, output - > [1 2 3 4 5 6 7 8]
    handleResult(redis.Ints(conn.Do("SUNION", "source_1", "source_2", "source_3")))
    //The union of the three sets is stored in the destination, and the output is "8", and the destination becomes "1 2 3 4 5 6 7 8"
    handleResult(redis.Int(conn.Do("SUNIONSTORE", "destination", "source_1", "source_2", "source_3")))
}

Hashtable P46

Hash operations for adding and deleting key value pairsP47
command format describe
HMGET HMGET key field [field …] Gets the value of one or more fields from the hash table
HMSET HMSET key field value [field value …] Sets one or more field values to the hash table
HDEL HDEL key field [field …] Removes the value of one or more fields from the hash table
HLEN HLEN key Returns the number of fields contained in the hash table

The relevant demonstration code is as follows:

//Perform hash table type related operations
func executeHashOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "hash")))
    //Set one or more field values to the hash table, and the output will be OK, and the hash will be - > {field}_ 1: value_ 1, field_ 2: value_ 2, field_ 3: value_ 3}
    handleResult(redis.String(conn.Do("HMSET", "hash", "field_1", "value_1", "field_2", "value_2", "field_3", "value_3")))
    //Get the value of one or more fields from the hash table and output - > [value]_ 1 value_ 3 value_ 2]
    handleResult(redis.Strings(conn.Do("HMGET", "hash", "field_1", "field_3", "field_2")))
    //Delete the value of one or more fields from the hash table, output the value of - > 2, and change the hash to - > field_ 2: value_ 2}
    handleResult(redis.Int(conn.Do("HDEL", "hash", "field_1", "field_3")))
    //Return the number of fields contained in the hash table, output - > 1
    handleResult(redis.Int(conn.Do("HLEN", "hash")))
}
More advanced features of hash tableP47
command format describe
HEXISTS HEXISTS key field Determine whether the field exists in the hash table
HKEYS HKEYS key Returns all fields in the hash table
HVALS HVALS key Returns the values of all fields in the hash table
HGETALL HGETALL key Returns all fields and their values in the hash table
HINCRBY HINCRBY key field increment Increase the value of field in the hash table by the integer increment
HINCRBYFLOAT HINCRBYFLOAT key field increment Increase the value of field in hash table by floating point increment

The relevant demonstration code is as follows:

//Perform hash table type advanced properties related operations
func executeHashFeatureOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "hash")))
    //Initialization
    handleResult(redis.String(conn.Do("HMSET", "hash", "field_1", "value_1", "field_2", "value_2", "field_3", "3")))
    //Determine whether the field exists in the hash table, and output - > 1
    handleResult(redis.Int(conn.Do("HEXISTS", "hash", "field_1")))
    //Return all fields in hash table, output - > [field]_ 1 field_ 2 3]
    handleResult(redis.Strings(conn.Do("HKEYS", "hash")))
    //Returns the values of all fields in the hash table, and outputs - > [value]_ 1 value_ 2 value_ 3]
    handleResult(redis.Strings(conn.Do("HVALS", "hash")))
    //Return all the fields and their values in the hash table, and output - > map [field]_ 1:value_ 1 field_ 2:value_ 2 field_ 3:3]
    handleResult(redis.StringMap(conn.Do("HGETALL", "hash")))
    //Increase the value of field in the hash table by 1, and output the value of field_ The value of 3 changes to 4
    handleResult(redis.Int(conn.Do("HINCRBY", "hash", "field_3", 1)))
    //Increase the value of field in the hash table by - 1.5, and then output it to 2.5, field_ The value of 3 changes to 2.5
    handleResult(redis.Float64(conn.Do("HINCRBYFLOAT", "hash", "field_3", -1.5)))
}

If the hash table contains very large values, you can useHKEYSTake out all the fields and use them againHGETFetching values to prevent server blocking caused by fetching multiple large volume values at one time.P48

Ordered setP48

Some common ordered set commandsP49
command format describe
ZADD ZADD key socre member [score member …] Adds one or more elements and their scores to an ordered set
ZREM ZREM key member [member …] Removes one or more elements and their scores from an ordered set
ZCARD ZCARD key Returns the number of elements in an ordered set
ZINCRBY ZINCRBY key increment member Add increment to the score of elements in an ordered set
ZCOUNT ZCOUNT key min max Returns the number of elements whose score is in the range of [min, Max]
ZRANK ZRANK key member Returns the ascending ranking of elements (ascending, starting from 0)
ZREVRANK ZREVRANK key member Returns the descending ranking of elements (descending, starting from 0)
ZSCORE ZSCORE key member Returns the ranking score of the element
ZRANGE ZRANGE key start stop [WITHSCORES] Return the elements whose ascending ranking is within the range of [start, stop]. The with scores option will return the score after the corresponding elements at the same time
ZRANRANGE ZRANGE key start stop [WITHSCORES] Return the elements whose descending ranking is within the range of [start, stop]. The with scores option will return the score after the corresponding elements at the same time

The relevant demonstration code is as follows:

//Performing ordered collection related operations
func executeZsetOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "zset")))
    //Five elements and their scores are added to the ordered set, and the output is "5". Zset becomes - > ["a": 1, "B": 2, "C": 3, "d": 4, "e": 5]
    handleResult(redis.Int(conn.Do("ZADD", "zset", 1, "a", 2, "b", 3, "c", 4, "d", 5, "e")))
    //Three elements and their scores are deleted from the ordered set, and the output is "2". Zset becomes - > ["a": 1, "B": 2, "C": 3]
    handleResult(redis.Int(conn.Do("ZREM", "zset", "d", "e", "f")))
    //Returns the number of elements in an ordered set, output - > 3
    handleResult(redis.Int(conn.Do("ZCARD", "zset")))
    //Increase the score of elements in the ordered set by 0.5, the output value will be 1.5, and the value of a will be 1.5
    handleResult(redis.Int(conn.Do("ZINCRBY", "zset", 1, "a")))
    //Increase the score of elements in the ordered set by - 1.5, the output value will be 0.5, and the value of a will be 0.5
    handleResult(redis.Float64(conn.Do("ZINCRBY", "zset", -1.5, "a")))
    //Return the number of elements whose score is in the range of [1,3], output - > 2
    handleResult(redis.Int(conn.Do("ZCOUNT", "zset", 1, 3)))
    //Return the ascending ranking of elements (ascending, starting from 0), output - > 0
    handleResult(redis.Int(conn.Do("ZRANK", "zset", "a")))
    //Returns the descending rank of the element (descending, starting from 0), output - > 2
    handleResult(redis.Int(conn.Do("ZREVRANK", "zset", "a")))
    //Return the score of element ranking, output - > 0.5
    handleResult(redis.Float64(conn.Do("ZSCORE", "zset", "a")))
    //Return the elements whose ascending ranking is within the range of [1,2], and return the score value, output - > map [B: 2, C: 3]
    handleResult(redis.StringMap(conn.Do("ZRANGE", "zset", "1", "2", "WITHSCORES")))
    //Return the elements whose descending ranking is within the range of [1,2], and return the score, output - > map [A: 0.5, B: 2]
    handleResult(redis.StringMap(conn.Do("ZREVRANGE", "zset", "1", "2", "WITHSCORES")))
}
Range command and union intersection command of ordered setP50
command format describe
ZRANGEBYSCORE ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] Return the elements whose ascending score is within the range of [min, Max]. With scores option will return the score after the corresponding elements
ZREVRANGEBYSCORE ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] Return the elements whose descending score is within the range of [max, min]. With scores option will return the score after the corresponding elements
ZREMRANGEBYRANK ZREMRANGEBYRANK key start stop Remove the elements whose ascending ranking is in the range of [start, stop]
ZREMRANGEBYSCORE ZREMRANGEBYSCORE key min max Remove the elements whose ascending score is in the range of [min, Max]
ZINTERSTORE ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]]  [AGGREGATE SUM\ MIN\ MAX] Find the intersection of one or more (ordered) sets and store it in destination. When weights exist, the number of weights must be equal to numkeys (the default score of the set is 1)
ZUNIONSTORE ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]]  [AGGREGATE SUM\ MIN\ MAX] Find the union of one or more (ordered) sets and store it in destination. When weights exist, the number of weights must be equal to numkeys (the default score of the set is 1)

The relevant demonstration code is as follows:

//Perform operations related to ordered set range and intersection union set
func executeZsetMutiOperation(conn redis.Conn) {
    //Delete original value
    handleResult(redis.Int(conn.Do("DEL", "zset_1", "zset_2", "destination")))
    //Initialization
    handleResult(redis.Int(conn.Do("ZADD", "zset_1", 1, "a", 2, "b", 3, "c")))
    handleResult(redis.Int(conn.Do("ZADD", "zset_2", 2, "b", 3, "c", 4, "d")))
    //Return the elements whose ascending score value is within the range of [1,2], and return the score value, output - > map [A: 1, B: 2]
    handleResult(redis.StringMap(conn.Do("ZRANGEBYSCORE", "zset_1", "1", "2", "WITHSCORES")))
    //Return the elements whose descending score value is within the range of [4, 3], and return the score value, output - > map [C: 3, D: 4]
    handleResult(redis.StringMap(conn.Do("ZREVRANGEBYSCORE", "zset_2", "4", "3", "WITHSCORES")))
    //Remove the elements whose ascending ranking is in the range of [1,1], output - > 1, Zset_ 1 becomes - > ["B": 2, "C": 3]
    handleResult(redis.Int(conn.Do("ZREMRANGEBYRANK", "zset_1", "1", "1")))
    //Remove the elements whose descending rank is in the range of [2,2], output - > 1, Zset_ 2 becomes - > ["C": 3, "d": 4]
    handleResult(redis.Int(conn.Do("ZREMRANGEBYSCORE", "zset_2", "2", "2")))
    //Find the intersection of two ordered sets, the weights are 2 and 3 respectively, and the score is added by default
    //And store it in the destination, the output will be > > 1, and the destination will become - >
    handleResult(redis.Int(conn.Do("ZINTERSTORE", "destination", 2, "zset_1", "zset_2", "WEIGHTS", 2, 3)))
    //Find the union of two ordered sets, the weights are 2 and 3 respectively, and the maximum score is specified
    //And store it in the destination, the output is 3, and the destination becomes - > ["a": 2, "C": 9, "d": 12]
    handleResult(redis.Int(conn.Do("ZUNIONSTORE", "destination", 2, "zset_1", "zset_2", "WEIGHTS", 2, 3, "AGGREGATE", "MAX")))
}

What you think

  • This chapter is a rather boring introduction to commands, but I still insist on looking at it. I find it very useful. There are many commands that I don’t normally contact with, and I didn’t expect redis to be so powerful.
  • Even if you read carefully, you don’t need to read all of them. You can quickly browse the foundation you already know. The key point is to put in the place you don’t know, read with thinking, think first, and then verify with practice.

This article starts with the official account: full Fu machine (click to view the original), open source in GitHub:reading-notes/redis-in-action
Redis actual combat 04. Brief introduction to common commands of redis data structure