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 .md):

// 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

Redis actual combat —— 04. Redis More related articles about common commands of data structure

  1. Redis Startup services and String Common commands

    Redis Startup services and String Common commands 1. start-up Redis service E:\redis>redis-server.exe redis.windows.conf _._ _.-``__ ''-._ ...

  2. Redis The actual combat Redis + Jedis

    use Memcached, There are requirements for cache object size , A single object cannot be greater than 1MB, And does not support complex data types , for example SET etc. . Based on these limitations , It is necessary to consider Redis! Related links : Redis actual combat Redis The actual combat Redi ...

  3. Redis The actual combat Redis + Jedis[ turn ]

    http://blog.csdn.net/it_man/article/details/9730605 2013-08-03 11:01 1786 Human reading   Comment on (0)  Collection   report   Catalog (?)[-] ...

  4. The first 18 Chapter Redis Data structures are commonly used commands

    18-1 Some basic commands for Strings 18-1 : To configure Spring About Redis The running environment for Strings <bean id="poolConfig" class="redis.c ...

  5. Redis Common commands of various data structures

    redis Universal API keys * Traverse all of key Generally not used in production environment redis Single thread , Easy to block other command execution O(n) dbsize Calculation key Total of O(1)exists Check key Whether there is ...

  6. Redis introduction ,Jedis And common commands

    One .Redis brief introduction 1. About relational databases and nosql database A relational database is a database based on a relational table , Eventually, data will be persisted to disk , and nosql data      Libraries are based on special structures , And store the data in the memory database . Compliance ...

  7. redis Five data types, common commands and applicable scenarios

    One .redis Of 5 Type of data : 1. Basic understanding : string character string ( It can be used for plastic surgery . Floating point and string , Collectively referred to as element ) list list ( Implementation queue , The element is not unique , First in, first out ) set aggregate ( Different elements ) h ...

  8. Redis The actual combat Redis command

    Read the directory 1. String command 2. List command 3. Assemble orders 4. Hash command 5. To assemble orders in order 6. Publish and subscribe commands 7. A profound Redis Can store keys and 5 Mapping between different types of data structures , this 5 It's a data node ...

  9. Redis note , Installation and common commands

    Reproduced in :http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/96.html?1455870708 One .redis Brief introduction redis yes N ...

  10. java redis client jedis Tests and common commands

    package cn.byref.demo1; import java.util.HashMap;import java.util.List;import java.util.Map;import j ...

Random recommendation

  1. Use vlc Second development to do their own player

    May refer to : Use vlc The player does rtsp The server Use vlc The player plays rtsp video web Use... In web pages vlc Plug in play camera rtsp Streaming video Use https://github.com/ZeBobo5/Vlc.Do ...

  2. 【GOF23 Design patterns 】 The chain of responsibility model

    source :http://www.bjsxt.com/  One .[GOF23 Design patterns ]_ The chain of responsibility model . Official document approval . Purchasing approval of supply chain system . Abnormal chain . Filters and interceptors call procedures package com.test.chainO ...

  3. PHP Learning notes (1) - Development environment construction

    Running environment :phpstudy It basically includes running php Everything the application needs ,php. apache.mysql, One click installation After installation, you only need to configure the virtual host and modify it host Files can support multiple sites download : http://w ...

  4. Document After field changes , Wrong report

    2016-10-11 15:27:47,828 [ERROR] [main] SpringApplication:838 - Application startup failedorg.springf ...

  5. MediaPlayer Audio playback Example

    State machine . flow chart . Life cycle Yes, play the audio / The control of video files and streams is managed by a state machine . The figure below shows a MediaPlayer Object is supported by the playback control operation driven lifecycle and state . The ellipse stands for MediaPlayer The object can be ...

  6. C++ 3D Physical engine library BulletPhysics Basic use

    Preface : Recently in contact with OpenGl and DX11 When , By the way, I learned Bullet This 3D The basic use of physical engines , Make a note of . |BulletPhysics Introduce BulletPhysics It's a cross platform open source physical engine , It's also ...

  7. day11-mysql Medium mysql The database is gone

    mysql Medium mysql The database is gone Just yesterday in vmware The virtual machine linux Installation on mysql, I found a problem on the board today . Through the long search on the Internet , It's finally settled . So I'm here to share with you the steps I've taken to solve this problem . The problem is ...

  8. Windows Next, how to check who occupies a port

    When developing, we often encounter the situation that the port is occupied , It's always crazy at this time , It's OK to know which process is taking up , It's over , If we don't know what to do ?  I'll tell you a way ,^_^. 1. Start —-> function —->cmd, perhaps ...

  9. NameNode Memory overflow and DataNode Request timeout exception handling

    The problem background During the Spring Festival holiday , Received the abnormal data sent by the monitor one after another , Quickly connect to the springboard machine to check the status of each service room , Find out Datanode On the second . The third channel is disconnected from the node , By looking at Datanode and Namenode Run log , Hair ...

  10. ElastAlert The rules

    elastalert  It's based on elasticsearch Open source alarm products ( Official documents ). I believe many people will use ELK Do a log collection system , But produce a log based “ good ” It's really a difficult problem to alert the security of the system . Alarm rules are hard to write , ...