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

Man Fu Zhu Ji 2021-01-23 10:57:36
redis actual combat brief introduction


character string P39

Redis Is a sequence of bytes , You can store the following 3 Kinds of values : Byte string (byte string)、 Integers 、 Floating point numbers .

When needed , Redis Will convert integers to floating-point numbers . The value range of integers and the long integer type of the system (long) In the same , The range and precision of floating-point numbers are related to IEEE 754 Double precision floating point numbers in standard (double) In the same .

Redis Add and subtract commands in P39
command Format describe
INCR INCR key Add... To the numeric value stored in the key 1
DECR DECR key Subtract... From the numeric value stored in the key 1
INCRBY INCRBY key increment Add the numeric value stored by the key to the integer increment increment
DECRBY DECRBY key decrement Subtract the integer decrement from the numeric value stored by the key decrement
INCRBYFLOAT INCRBYFLOAT key increment Add a floating-point increment to the numeric value stored by the key increment

The relevant demo code is as follows (main And handleResult For the definition, see :01. Redis Data structure introduction ):

// Perform string type number dependent operations
func executeNumberOperation(conn redis.Conn) {
// Delete the original value
handleResult(redis.Int(conn.Do("DEL", "number")))
// Get value , Output -> ERROR: redigo: nil returned
handleResult(redis.Int(conn.Do("GET", "number")))
// Self increasing 1, Returns the value after increment -> 1
handleResult(redis.Int(conn.Do("INCR", "number")))
// Self increasing 2, Returns the value after increment -> 3
handleResult(redis.Int(conn.Do("INCRBY", "number", "2")))
// Self reduction 1, Returns the value after subtraction -> 2
handleResult(redis.Int(conn.Do("DECR", "number")))
// Self reduction 2, Returns the value after subtraction -> 0
handleResult(redis.Int(conn.Do("DECRBY", "number", "2")))
// Self increasing 1.5, Returns the value after increment -> 1.5
handleResult(redis.Float64(conn.Do("INCRBYFLOAT", "number", "1.5")))
// Self increasing -1.3, Returns the value after increment -> 0.2
handleResult(redis.Float64(conn.Do("INCRBYFLOAT", "number", "-1.3")))
}
for Redis Commands for handling substrings and binary bits P40
command Format describe
APPEND APPEND key value take value Append to key The end of the current value
GETRANGE GETRANGE key start end return [start, end] Substrings in range
SETRANGE SETRANGE key offset value Put substring [offset, offset + len(value)) Set to value
GETBIT GETBIT key offset Think of a string as a binary bit string , obtain offset On the table
SETBIT SETBIT key offset value Think of a string as a binary bit string , Set up offset The position above is value
BITCOUNT BITCOUNT key [start end] Statistics [start, end] How many substrings are there under binary in the range 1
BITOP BITOP operation destkey key [key ...] operation Optional bit operations AND , OR , XOR , NOT , Save the results of operations performed by one or more binary bit strings to destkey in

The relevant demo code is as follows :

// Perform string type string related operations
func executeStringOperation(conn redis.Conn) {
// Delete the original value
handleResult(redis.Int(conn.Do("DEL", "string")))
// Appending strings , Returns the current string length -> 6, Value to -> append
handleResult(redis.Int(conn.Do("APPEND", "string", "append")))
// Get substring , return -> en
handleResult(redis.String(conn.Do("GETRANGE", "string", 3, 4)))
// Set substring , Returns the current string length -> 6, Value to -> appled
handleResult(redis.Int(conn.Do("SETRANGE", "string", 3, "le")))
// Set substring , Returns the current string length -> 11, Value to -> application
handleResult(redis.Int(conn.Do("SETRANGE", "string", 3, "lication")))
// Get binary bits , return -> 1
// ( For the first 7/8 Characters a Under the binary system 7%8 Bit on bit , namely 0110 0001 Of the 7 position 1)
handleResult(redis.Int(conn.Do("GETBIT", "string", 7)))
// Set the binary bit , Returns the original binary bit -> 0, Value to -> cpplication
// ( Set the first 6/8 Characters a Under the binary system 6%8 The binary bit on bit is 1, namely 0110 0001 Turn into 0110 0011)
handleResult(redis.Int(conn.Do("SETBIT", "string", 6, 1)))
// Counting binary bits , return -> 7
// ( Statistics [0, 1] Substrings in range cp In binary 0110 0011 0111 0000 Binary bit is 1 The number of )
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")))
// Yes aa(0110 0001 0110 0001) and b(0110 0010 0000 0000) Conduct Press bit or , The results are stored in cKey in
// Return string length -> 2, The value is ca(0110 0011 0110 0001),
handleResult(redis.Int(conn.Do("BITOP", "OR", "cKey", "aKey", "bKey")))
}

Redis By using substring operation and binary bit operation , coordination WATCHMULTI and EXEC command ( I'll give you a brief introduction later , I'll go into it later ), Build any data structure you want .

list P42

Some common list commands P42
command Format describe
RPUSH RPUSH key value [value ...] Turn one or more value Insert... From the right end of the list
LPUSH LPUSH key value [value ...] Turn one or more value Insert... From the left end of the list
RPOP RPOP key Remove and return the right most element of the list
LPOP LPOP key Remove and return the leftmost element of the list
LINDEX LINDEX key offset Returns the left end of the list with an offset of offset The elements of
LRANGE LRANGE key start end Go back to the left end of the list and start [start, end] All elements in the scope
LTRIM LTRIM key start end Remove the left end of the list and start [start, end] All elements outside the scope

The relevant demo code is as follows :

// Perform list type related operations
func executeListOperation(conn redis.Conn) {
// Delete the original value
handleResult(redis.Int(conn.Do("DEL", "list")))
// Right side insert one insert a, b, c, Returns the current list length -> 3, List becomes -> a b c
handleResult(redis.Int(conn.Do("RPUSH", "list", "a", "b", "c")))
// The left end is inserted once d, e, f, Returns the current list length -> 6, List becomes -> f e d a b c
handleResult(redis.Int(conn.Do("LPUSH", "list", "d", "e", "f")))
// Pop up and return the rightmost value of the list , return -> c, List becomes -> f e d a b
handleResult(redis.String(conn.Do("RPOP", "list")))
// Pop up and return the leftmost value in the list , return -> f, List becomes -> e d a b
handleResult(redis.String(conn.Do("LPOP", "list")))
// Return the left start subscript offset is offset Value , return -> d
handleResult(redis.String(conn.Do("LINDEX", "list", 1)))
// Remove the left end of the list and start [1, 2] All elements outside the scope , List becomes -> d a
handleResult(redis.String(conn.Do("LTRIM", "list", 1, 2)))
}

utilize LTRIM Commands can atomically pop up multiple elements . P43

Blocking list pop-up commands and commands to move elements between lists P43
command Format describe
BLPOP BLPOP key [key ...] timeout Pop the leftmost element from the first non empty list , Or in timeout Block in seconds and wait for pop-up elements to appear , Returns the name and element of the pop-up list , timeout by 0 It means infinite waiting
BRPOP BRPOP key [key ...] timeout Pop the rightmost element from the first non empty list , Or in timeout Block in seconds and wait for pop-up elements to appear , Returns the name and element of the pop-up list , timeout by 0 It means infinite waiting
RPOPLPUSH RPOPLPUSH source destination from source The right most element pops up in the list , Then exit this element destination At the far left of the list , And return this element
BRPOPLPUSH BRPOPLPUSH source destination timeout from source The right most element pops up in the list , Then exit this element destination At the far left of the list , And return this element ; If source The list is empty. , It's in timeout Block and wait for pop-up elements to appear within seconds

The relevant demo code is as follows :

// Perform list type blocking related operations
func executeListBlockOperation(conn redis.Conn) {
// Delete the 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 , Waiting for the most 1 second , 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 , return -> a,source Turn into -> b c,destination Turn into -> d e f
handleResult(redis.Strings(conn.Do("BLPOP", "source", "destination", 0)))
// Pop up from the first non empty list and return the rightmost value of the list , Wait indefinitely , return -> f,source Turn into -> b c,destination Turn into -> d e
handleResult(redis.Strings(conn.Do("BRPOP", "destination", "source", 0)))
// from source Pop up the rightmost element , And then push into destination Leftmost end , And return this element
// return -> c,source Turn into -> b,destination Turn into -> c d e
handleResult(redis.String(conn.Do("RPOPLPUSH", "source", "destination")))
// from source Pop up the rightmost element , And then push into destination Leftmost end , And return this element , Wait indefinitely
// return -> b,source Turn into -> <nil>,destination Turn into -> b c d e
handleResult(redis.String(conn.Do("BRPOPLPUSH", "source", "destination", 0)))
// from source Pop up the rightmost element , And then push into destination Leftmost end , And return this element , Waiting for the most 1 second
// Output -> ERROR: redigo: nil returned,source Turn into -> <nil>,destination Turn into -> b c d e
handleResult(redis.String(conn.Do("BRPOPLPUSH", "source", "destination", 1)))
}

For block eject command and eject and push command , The most common use case is messaging (messaging) And task queues (task queue), These two topics will be introduced later . P44

Exercises : Reduce memory footprint by listing P44

stay Last article in , We use ordered sets to record the items that users have recently viewed , And set the user's time stamp when browsing these products to the score value , So that the program can clean up the old session in the process or after the completion of the purchase operation , Carry out corresponding data analysis . But because the time stamp needs to take up the corresponding space , So if you don't need a timestamp for analysis , So there's no need to use an ordered collection to save the items that users have recently browsed . So , Please keep the semantics unchanged , take UpdateToken Function is used to replace the ordered set with a list .

Tips : If you have any difficulty in answering this question , You can go to 6.1.1 Find inspiration in the festival .

  • Because the list is ordered , All the latest visits must be at the left end of the list , So delete the access record in the list before each operation , Push it to the left of the list , The last trim list is length 25 that will do . Because you need to traverse the entire list each time , So the time complexity is high , But the total length of the list is just 25 , There's not much difference in time , But space can be saved a lot .

    // Update recent product access list
    func UpdateLatestViewedItem(conn redis.Conn, itemId int) {
    // Remove all values in the list as itemId The elements of
    _ = conn.Send("LREM", "latestViewedItem", 0, itemId)
    // Push the most recently visited items to the far left of the list
    _ = conn.Send("LPUSH", "latestViewedItem", itemId)
    // Trim list , Keep the most recently visited 25 individual
    _ = conn.Send("LTRIM", "latestViewedItem", 0, 24)
    // Execute the above command
    _ = conn.Flush()
    }
    

aggregate P44

Some common set commands P45
command Format describe
SADD SADD key member [member ...] Add one or more elements to the collection , Returns the number of new elements added to the collection ( Do not include existing elements )
SREM SREM keymember [member ...] Remove one or more elements from the collection , Returns the element that was successfully removed from the collection ( Don't include elements that don't exist )
SISMEMBER SISMEMBER key member Element of judgement member Is it gathering key in
SCARD SCARD key Returns the number of elements in the Union
SMEMBERS SMEMBERS key Returns all elements of the collection
SRANDMEMBER SRANDMEMBER key [count] Randomly returns one or more elements of a set .count Positive number , return count Different elements ( Return the entire collection at most );count When it's negative , return |count| Two elements that might repeat , No maximum limit .
SPOP SPOP key Randomly remove and return an element in the collection
SMOVE SMOVE source destination member Put the element member From the collection source Move to collection destination in

The relevant demo code is as follows :

// Perform set type related operations
func executeSetOperation(conn redis.Conn) {
// Delete the original value
handleResult(redis.Int(conn.Do("DEL", "source", "destination")))
// Add three elements to the collection , Output -> 6,source Turn into -> 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 Turn into -> 3 4 5 6 7
handleResult(redis.Int(conn.Do("SREM", "source", 1, 2)))
// Determine whether the set contains elements 3, Output -> 1
handleResult(redis.Int(conn.Do("SISMEMBER", "source", 3)))
// Returns the number of elements in the collection , Output -> 5
handleResult(redis.Int(conn.Do("SCARD", "source")))
// Returns all elements of the collection , Output -> [3 4 5 6 7]
handleResult(redis.Ints(conn.Do("SMEMBERS", "source")))
// Randomly returns different... In the set 3 Elements , Output -> [6 5 3] ( Random results can be different , Subject to the actual situation )
handleResult(redis.Ints(conn.Do("SRANDMEMBER", "source", 3)))
// Randomly returns the repeatable 6 Elements , Output -> [7 5 6 3 7 6] ( Random results can be different , Subject to the actual situation )
handleResult(redis.Ints(conn.Do("SRANDMEMBER", "source", -6)))
// Randomly delete... From the set 1 Elements , Output -> 3 ,source Turn into -> 4 5 6 7( Random results can be different , Subject to the actual situation )
handleResult(redis.Int(conn.Do("SPOP", "source")))
// Move source The elements in the collection 7 To destination Collection ( Because of the randomness ahead , The results may be different , Subject to the actual situation )
// Output -> 1 ,source Turn into -> 4 5 6 ,destination Turn into -> 7
handleResult(redis.Int(conn.Do("SMOVE", "source", "destination", 7)))
}
Commands for combining and processing multiple sets P45
command Format describe
SDIFF SDIFF key [key ...] Return exists in the first collection , Elements that don't exist in other sets ( Difference set )
SDIFFSTORE SDIFFSTORE destination key [key ...] Will exist in the first set , Elements that don't exist in other sets ( Difference set ) Store in destination in , Returns the difference set size
SINTER SINTER key [key ...] Returns elements that exist in all collections at the same time ( intersection )
SINTERSTORE SINTERSTORE destination key [key ...] Elements that will exist in all sets at the same time ( intersection ) Store in destination in , Returns the size of the intersection
SUNION SUNIONkey [key ...] Returns elements that exist in at least one collection ( Combine )
SUNIONSTORE SUNIONSTORE destination key [key ...] Elements that will exist in at least one collection ( Combine ) Store in destination in , Returns the union size

The relevant demo code is as follows :

// Perform collection type multiple collection related operations
func executeSetMutiOperation(conn redis.Conn) {
// Delete the 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)))
// Returns the difference set of three sets , Output -> [1]
handleResult(redis.Ints(conn.Do("SDIFF", "source_1", "source_2", "source_3")))
// Store the difference set of three sets in destination in , Output -> 1,destination Turn into -> 1
handleResult(redis.Int(conn.Do("SDIFFSTORE", "destination", "source_1", "source_2", "source_3")))
// Returns the intersection of two sets , Output -> [2 4]
handleResult(redis.Ints(conn.Do("SINTER", "source_1", "source_2")))
// Store the intersection of two sets in destination in , Output -> 2,destination Turn into -> 2 4
handleResult(redis.Int(conn.Do("SINTERSTORE", "destination", "source_1", "source_2")))
// Returns the 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")))
// Store the union of three sets in destination in , Output -> 8,destination Turn into -> 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 pairs P47
command Format describe
HMGET HMGET key field [field ...] Get one or more... From the hash table field Value
HMSET HMSET key field value [field value ...] Set one or more... To the hash table field Value
HDEL HDEL key field [field ...] Remove one or more... From the hash table field Value
HLEN HLEN key Returns the... Contained in the hash table field The number of

The relevant demo code is as follows :

// Perform hash table type related operations
func executeHashOperation(conn redis.Conn) {
// Delete the original value
handleResult(redis.Int(conn.Do("DEL", "hash")))
// Set one or more... To the hash table field Value , Output -> OK,hash Turn into -> {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 one or more... From the hash table field Value , Output -> [value_1 value_3 value_2]
handleResult(redis.Strings(conn.Do("HMGET", "hash", "field_1", "field_3", "field_2")))
// Remove one or more... From the hash table field Value , Output -> 2,hash Turn into -> field_2: value_2}
handleResult(redis.Int(conn.Do("HDEL", "hash", "field_1", "field_3")))
// Returns the... Contained in the hash table field The number of , Output -> 1
handleResult(redis.Int(conn.Do("HLEN", "hash")))
}
More advanced features of hash tables P47
command Format describe
HEXISTS HEXISTS key field Judge field Whether it exists in the hash table
HKEYS HKEYS key Returns all... In the hash table field
HVALS HVALS key Returns all... In the hash table field Value
HGETALL HGETALL key Returns all... In the hash table field And its value
HINCRBY HINCRBY key field increment In the hash table field Increase the value of by an integer increment
HINCRBYFLOAT HINCRBYFLOAT key field increment In the hash table field The value of increases the floating-point number increment

The relevant demo code is as follows :

// Perform operations related to advanced properties of hash table types
func executeHashFeatureOperation(conn redis.Conn) {
// Delete the 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")))
// Judge field Whether it exists in the hash table , Output -> 1
handleResult(redis.Int(conn.Do("HEXISTS", "hash", "field_1")))
// Returns all... In the hash table field, Output -> [field_1 field_2 3]
handleResult(redis.Strings(conn.Do("HKEYS", "hash")))
// Returns all... In the hash table field Value , Output -> [value_1 value_2 value_3]
handleResult(redis.Strings(conn.Do("HVALS", "hash")))
// Returns all... In the hash table field And its value , Output -> map[field_1:value_1 field_2:value_2 field_3:3]
handleResult(redis.StringMap(conn.Do("HGETALL", "hash")))
// In the hash table field The value of the increase 1, Output -> 4,field_3 The value of a -> 4
handleResult(redis.Int(conn.Do("HINCRBY", "hash", "field_3", 1)))
// In the hash table field The value of the increase -1.5, Output -> 2.5,field_3 The value of a -> 2.5
handleResult(redis.Float64(conn.Do("HINCRBYFLOAT", "hash", "field_3", -1.5)))
}

If the hash table contains very large values , You can use it first HKEYS Take out all field, And then use HGET Extract value , Prevent the server from blocking by fetching multiple large values at one time . P48

Ordered set P48

Some common ordered set commands P49
command Format describe
ZADD ZADD key socre member [score member ...] Add one or more elements and their scores to an ordered set
ZREM ZREM key member [member ...] Remove 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 Increase the score of elements in an ordered set increment
ZCOUNT ZCOUNT key min max Return the score in [min, max] The number of elements in the range
ZRANK ZRANK key member Returns the ascending ranking of elements ( Ascending , from 0 Start )
ZREVRANK ZREVRANK key member Returns the descending ranking of elements ( Descending , from 0 Start )
ZSCORE ZSCORE key member Returns the ranking score of the element
ZRANGE ZRANGE key start stop [WITHSCORES] Return to ascending rank in [start, stop] Elements in scope ,WITHSCORES Option will return the score after the corresponding element
ZRANRANGE ZRANGE key start stop [WITHSCORES] Return to descending rank in [start, stop] Elements in scope ,WITHSCORES Option will return the score after the corresponding element

The relevant demo code is as follows :

// Perform operations related to ordered sets
func executeZsetOperation(conn redis.Conn) {
// Delete the original value
handleResult(redis.Int(conn.Do("DEL", "zset")))
// Add... To an ordered set 5 Elements and their scores , Output -> 5,zset Turn into -> ["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")))
// Delete... From an ordered set 3 Elements and their scores , Output -> 2,zset Turn into -> ["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 an ordered set 0.5, Output -> 1.5,a The value of a -> 1.5
handleResult(redis.Int(conn.Do("ZINCRBY", "zset", 1, "a")))
// Increase the score of elements in an ordered set -1.5, Output -> 0.5,a The value of a -> 0.5
handleResult(redis.Float64(conn.Do("ZINCRBY", "zset", -1.5, "a")))
// Return the score in [1, 3] The number of elements in the range , Output -> 2
handleResult(redis.Int(conn.Do("ZCOUNT", "zset", 1, 3)))
// Returns the ascending ranking of elements ( Ascending , from 0 Start ), Output -> 0
handleResult(redis.Int(conn.Do("ZRANK", "zset", "a")))
// Returns the descending ranking of elements ( Descending , from 0 Start ), Output -> 2
handleResult(redis.Int(conn.Do("ZREVRANK", "zset", "a")))
// Returns the ranking score of the element , Output -> 0.5
handleResult(redis.Float64(conn.Do("ZSCORE", "zset", "a")))
// Return to ascending rank in [1, 2] Elements in scope , And return the score , Output -> map[b:2 c:3]
handleResult(redis.StringMap(conn.Do("ZRANGE", "zset", "1", "2", "WITHSCORES")))
// Return to descending rank in [1, 2] Elements in scope , And return the score , Output -> map[a:0.5 b:2]
handleResult(redis.StringMap(conn.Do("ZREVRANGE", "zset", "1", "2", "WITHSCORES")))
}
The range command and union intersection command of ordered sets P50
command Format describe
ZRANGEBYSCORE ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] Return the ascending score in [min, max] Elements in scope ,WITHSCORES Option will return the score after the corresponding element
ZREVRANGEBYSCORE ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] Return the descending score in [max, min] Elements in scope ,WITHSCORES Option will return the score after the corresponding element
ZREMRANGEBYRANK ZREMRANGEBYRANK key start stop Remove ascending rank in [start, stop] Elements in scope
ZREMRANGEBYSCORE ZREMRANGEBYSCORE key min max Remove the ascending score in [min, max] Elements in scope
ZINTERSTORE ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]]  [AGGREGATE SUM|MIN|MAX] Ask for one or more ( Orderly ) Intersection of sets , And store it to destination in ,WEIGHTS When weights exist ,weight The quantity 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] Ask for one or more ( Orderly ) Union of sets , And store it to destination in ,WEIGHTS When weights exist ,weight The quantity must be equal to numkeys( The default score of the set is 1)

The relevant demo code is as follows :

// Perform operations related to ordered set range and intersection union set
func executeZsetMutiOperation(conn redis.Conn) {
// Delete the 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 ascending score in [1, 2] Elements in scope , And return the score , Output -> map[a:1 b:2]
handleResult(redis.StringMap(conn.Do("ZRANGEBYSCORE", "zset_1", "1", "2", "WITHSCORES")))
// Return the descending score in [4, 3] Elements in scope , And return the score , Output -> map[c:3 d:4]
handleResult(redis.StringMap(conn.Do("ZREVRANGEBYSCORE", "zset_2", "4", "3", "WITHSCORES")))
// Remove ascending rank in [1, 1] Elements in scope , Output -> 1,zset_1 Turn into -> ["b":2, "c":3]
handleResult(redis.Int(conn.Do("ZREMRANGEBYRANK", "zset_1", "1", "1")))
// Remove descending rank in [2, 2] Elements in scope , Output -> 1,zset_2 Turn into -> ["c":3, "d":4]
handleResult(redis.Int(conn.Do("ZREMRANGEBYSCORE", "zset_2", "2", "2")))
// seek 2 The intersection of two ordered sets , The weights are 2, 3, The default value is addition
// And store it to destination in , Output -> 1,destination Turn into ->
handleResult(redis.Int(conn.Do("ZINTERSTORE", "destination", 2, "zset_1", "zset_2", "WEIGHTS", 2, 3)))
// seek 2 The union of two ordered sets , The weights are 2, 3, The maximum value is specified
// And store it to destination in , Output -> 3,destination Turn into -> ["a":2, "c":9, "d":12]
handleResult(redis.Int(conn.Do("ZUNIONSTORE", "destination", 2, "zset_1", "zset_2", "WEIGHTS", 2, 3, "AGGREGATE", "MAX")))
}

Thinking and thinking

  • This chapter is a boring introduction to the command , But I still insist on it , It's useful to find out , There are a lot of commands that you don't normally touch , I didn't expect Redis So powerful .

  • Even if you read more carefully , And you don't need to read all of them , You can quickly browse what you already know , The focus is still on the unknown , Read with thought , First think about it and then test it with practice .

This article was first published on the official account : Man Fu Zhu Ji ( Click to view the original ) Open source in GitHub :reading-notes/redis-in-action

版权声明
本文为[Man Fu Zhu Ji]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210123105643422l.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云