This article is updated on January 31, 2021, using redis 6.0.8 and operating system deepin 15.11.
Note: in this paper, lowercase is a user-defined variable, which is filled in according to the actual situation. use[]
Causes the presentation content to be optional,|
Indicates to use left or right content,...
It means to repeat the previous content.
For official documents, please refer to:https://redis.io/commands
character string
That is, byte sequence. It can also be regarded as integer or floating point number.
APPEND
String append content, if the key does not exist to create.
APPEND str value
Returns the length of bytes appended:
(integer) 5
BITCOUNT
Count the number of 1 in binary bits. The byte interval [start, end] can be specified.
BITCOUNT str [start, end]
Returns the number of binary bits 1:
(integer) 4
BITOP
After bit operation, the result is stored in deststr.
BITOP AND|OR|XOR deststr str [...]
BITOP NOT deststr str
Returns the byte length of the result string deststr:
(integer) 1
DECR
Minus 1 can only be used for string types that are treated as integers. If the key does not exist, it is first created with a value of 0.
DECR str
Returns the value after subtraction:
“-1”
DECRBY
Reduces the specified integer value and can only be used for string types that are treated as integers. If the key does not exist, it is first created with a value of 0.
DECRBY str decrement
Returns the reduced value:
“-1”
GET
Gets the string.
GET str
Return string:
“value”
Or return when there is no such key:
(nil)
GETBIT
Gets the binary bit of the second offset. The first bit of the byte is 0. Negative numbers are not allowed.
GETBIT str offset
Only 0 or 1 will be returned. If the bit does not exist, 0 will be returned:
(integer) 1
GETRANGE
Gets the substring in the byte interval [start, end]. The first byte subscript is 0, a negative number can be used, and the last byte subscript is – 1.
GETRANGE str start end
Return substring:
“val”
Or if there is no return:
“”
INCR
Self incrementing 1 can only be used for string types that are treated as integers. If the key does not exist, it is first created with a value of 0.
INCR str
Return the value after auto increment:
(integer) 1
INCRBY
Increasing the integer value can only be used for string types that are treated as integers. If the key does not exist, it is first created with a value of 0.
INCRBY str increment
Return the added value:
(integer) 1
INCRBYFLOAT
Increasing floating-point values can only be used for string types that are treated as integers or floating-point numbers. If the key does not exist, it is first created with a value of 0.
INCRBYFLOAT str increment
Return the added value:
“0.1”
SET
Set the string.
SET str value
Returned successfully:
OK
SETBIT
Set the binary bit of the second offset, which can only be set to 0 or 1. If the key does not exist, it will be created. The first bit of the byte is 0. Negative numbers are not allowed. If the length of the string is insufficient, it is filled with 0 value bytes.
SETBIT str offset bit
Returns the original value of the binary bit:
(integer) 1
SETNX
Sets the string only if the key does not exist.
SETNX str value
1 for successful setting, 0 for unsuccessful setting:
(integer) 1
SETRANGE
Set the content from the offset byte. If the key does not exist, create it. The subscript of the first byte is 0. Negative numbers are not allowed. If the length of the string is insufficient, it is filled with 0 value bytes.
SETRANGE str offset value
Returns the set string byte length:
(integer) 5
list
The element is a string. The left side is the beginning and the right side is the end.
BLPOP
Block to pop up the leftmost element from the first non empty list.
BLPOP list [...] timeoutseconds
If the pop-up is not blocked, the pop-up list and elements will be returned
1) “list”
2) “element”
If there is blocking pop-up, the pop-up list, element and blocking time will be returned
1) “list”
2) “element”
(1.01s)
If the blocking timeout does not pop up, nil and blocking time are returned:
(nil)
(3.01s)
BRPOP
Block to pop up the rightmost element from the first non empty list.
BRPOP list [...] timeoutseconds
If there is no blocking, the pop-up list and element will be returned
1) “list”
2) “element”
In case of blocking, the pop-up list, element and blocking time will be returned
1) “list”
2) “element”
(1.01s)
If the blocking timeout occurs, nil and blocking time are returned
(nil)
(3.01s)
BRPOPLPUSH
Block to eject the rightmost element from srclist and push the element to the leftmost side of destlist. If destlist does not exist, create it.
BRPOPLPUSH srclist destlist timeoutseconds
If there is no blocking, the element is returned:
“element”
If there is a block, return the element and block time:
“element”
(1.01s)
If the blocking timeout occurs, nil and blocking time are returned
(nil)
(3.01s)
LINDEX
Returns the element with the specified subscript on the left. The first element has a subscript of 0 and a negative number can be used. The last element has a subscript of – 1.
LINDEX list n
Returns the element at the subscript:
“element”
Or without this element:
(nil)
LPOP
Pop up the leftmost element.
LPOP list
Return the pop-up element:
“element”
Or when the list is empty:
(nil)
LPUSH
Push the elements one by one to the left of the list. If the list does not exist, create it. The last element in the parameter is at the far left of the list.
LPUSH list element [...]
Length of return list:
(integer) 1
LRANGE
Find the elements in the interval [start, end] from the left. The first element has a subscript of 0 and a negative number can be used. The last element has a subscript of – 1.
LRANGE list start end
Return element list:
1) “element0”
2) “element1”
Or when the list is empty:
(empty array)
LTRIM
The list is cropped from the left, and only the elements in the interval [start, end] are retained. The first element has a subscript of 0 and a negative number can be used. The last element has a subscript of – 1.
LTRIM list start end
Returned successfully:
OK
RPOP
Pop up the rightmost element.
RPOP list
Return the pop-up element:
“element”
Or when the list is empty:
(nil)
RPOPLPUSH
Pop up the rightmost element from srclist and push it into the leftmost element of destlist. If destlist does not exist, create it.
RPOPLPUSH srclist destlist
Return the element:
“element”
Or return when srclist is empty:
(nil)
RPUSH
Push the elements to the right of the list in turn. If the list does not exist, create it. The last element in the parameter is at the far right of the list.
RPUSH list element [...]
Length of return list:
(integer) 1
Unordered set
The element is a string.
SADD
Add elements to the collection, or create if the collection does not exist.
SADD set element [...]
Returns the number of elements actually added. If the element already exists, it will not be included
(integer) 1
SCARD
Returns the number of elements in the collection.
SCARD set
Number of returned elements:
(integer) 1
SDIFF
Difference set operation.
SDIFF set excludeset [...]
Returns the elements of the result set:
1) “element0”
2) “element1”
Or when the result set is empty:
(empty array)
SDIFFSTORE
The result is stored in destset. If destset does not exist, it will be created. If destset already exists, it will be cleared.
SDIFFSTORE destset set excludeset [...]
Returns the number of elements in the result set:
(integer) 2
SINTER
Intersection operation.
SINTER set [...]
Returns the elements of the result set:
1) “element0”
2) “element1”
Or when the result set is empty:
(empty array)
SINTERSTORE
The result is stored in destset. If destset does not exist, it will be created. If destset already exists, it will be cleared.
SINTERSTORE destset set [...]
Returns the number of elements in the result set:
(integer) 2
SISMEMBER
Check for elements.
SISMEMBER set element
Include return 1, not include return 0:
(integer) 1
SMEMBERS
Returns all elements of the collection.
SMEMBERS set
Return all elements:
1) “element0”
2) “element1”
Or when the collection is empty:
(empty array)
SMOVE
If the element is in srcset, delete it and move it into destset. If destset does not exist, create it.
SMOVE srcset destset element
If it moves, it returns 1; if it does not move, it returns 0:
(integer) 1
SPOP
Pop up elements randomly.
SPOP set [count]
Return to the pop-up element list:
“element0”
“element1”
Or return when no element pops up:
(empty array)
SRANDMEMBER
Returns elements randomly. When count is positive, the elements will not repeat, so the number may be insufficient; when count is negative, the elements may repeat.
SRANDMEMBER set [count]
Return element list:
“element0”
“element1”
Or when count is 0:
(empty array)
SREM
Delete elements from the collection.
SREM set element [...]
Returns the number of elements actually deleted. If the element does not exist, it will not be included in the number:
(integer) 1
SUNION
Union operation.
SUNION set [...]
Returns the elements of the result set:
1) “element0”
2) “element1”
Or when the result set is empty:
(empty array)
SUNIONSTORE
The result is stored in destset. If destset does not exist, it will be created. If destset already exists, it will be cleared.
SUNIONSTORE destset set [...]
Returns the number of elements in the result set:
(integer) 2
Ordered set
String and floating point value mapping.
ZADD
Add elements and scores to the ordered set. If the ordered set does not exist, create it.
ZADD zset score element [score element ...]
Returns the number of elements actually added. If the elements already exist and have the same score, the number will not be included
(integer) 1
ZCARD
Returns the number of elements in an ordered set.
ZCARD zset
Number of returned elements:
(integer) 1
ZCOUNT
Returns the number of elements in the interval [min, Max] of the ordered set score.
ZCOUNT zset min max
Number of returned elements:
(integer) 1
ZINCRBY
Increase the score of elements. If the element does not exist, add it with a score of 0.
ZINCRBY zset increment element
Return the increased score:
“0.1”
ZINTERSTORE
Similar to the intersection operation, and the results are stored in the destzset. If destset does not exist, it will be created. If destset already exists, it will be cleared.
ZINTERSTORE destzset nzset zset [...] [WEIGHTS weight [...]] [AGGREGATE SUM|MIN|MAX]
The Zset parameter can use a set as an ordered set with a score of 1. Aggregate is sum by default.
Returns the number of elements in the result ordered set:
(integer) 2
ZRANGE
Find the elements ranked in the range [start, end] from small to large. The first element has a subscript of 0 and a negative number can be used. The last element has a subscript of – 1.
ZRANGE zset start end [WITHSCORES]
Return element list:
1) “element0”
2) “element1”
As specifiedWITHSCORES
Then the score will be returned at the same time
1) “element0”
2) “1”
3) “element1”
4) “2”
Or when the list is empty:
(empty array)
ZRANK
Find the ranking of elements in an ordered set from small to large. The first is 0.
ZRANK zset element
Return the ranking of elements:
(integer) 0
Or return when the element does not exist:
(nil)
ZRANGEBYSCORE
From small to large, find the elements whose score is in the interval [min, Max].
ZRANGEBYSCORE zset min max [WITHSCORES] [LIMIT offset count]
Return element list:
1) “element0”
2) “element1”
As specifiedWITHSCORES
Then the score will be returned at the same time
1) “element0”
2) “1”
3) “element1”
4) “2”
Or when the list is empty:
(empty array)
ZREM
Removes elements from an ordered collection.
ZREM zset elment [...]
Returns the number of elements actually deleted. If the element does not exist, it will not be included in the number:
(integer) 1
ZREMRANGEBYRANK
Delete the elements in the range [start, end] from small to large in the ordered set. The first element has a subscript of 0 and a negative number can be used. The last element has a subscript of – 1.
ZREMRANGEBYRANK zset start end
Return the number of deleted elements:
(integer) 1
ZREMRANGEBYSCORE
Delete the elements in the ordered set whose score is in the interval [min, Max].
ZREMRANGEBYSCORE zset min max
Return the number of deleted elements:
(integer) 1
ZREVRANGE
Find the elements ranked in the range [start, end] from big to small. The first element has a subscript of 0 and a negative number can be used. The last element has a subscript of – 1.
ZREVRANGE zset start end [WITHSCORES]
Return element list:
1) “element0”
2) “element1”
As specifiedWITHSCORES
Then the score will be returned at the same time
1) “element0”
2) “2”
3) “element1”
4) “1”
Or when the list is empty:
(empty array)
ZREVRANGEBYSCORE
Find the element with score in the range [min, Max] from big to small.
ZRANGEBYSCORE zset max min [WITHSCORES] [LIMIT offset count]
Return element list:
1) “element0”
2) “element1”
As specifiedWITHSCORES
Then the score will be returned at the same time
1) “element0”
2) “2”
3) “element1”
4) “1”
Or when the list is empty:
(empty array)
ZREVRANK
Find the ranking of elements from large to small in an ordered set. The first is 0.
ZREVRANK zset element
Return the ranking of elements:
(integer) 0
Or return when the element does not exist:
(nil)
ZSCORE
Returns the score of the element.
ZSCORE zset element
Return score:
“1”
Or return when the element does not exist:
(nil)
ZUNIONSTORE
It is similar to union operation and stores the result in destzset. If destset does not exist, it will be created. If destset already exists, it will be cleared.
ZUNIONSTORE destzset nzset zset [...] [WEIGHTS weight [...]] [AGGREGATE SUM|MIN|MAX]
The Zset parameter can use a set as an ordered set with a score of 1. Aggregate is sum by default.
Returns the number of elements in the result ordered set:
(integer) 2
Unordered hash
Key values are strings, and values can also be regarded as integers and floating-point numbers.
HDEL
Key to delete hash.
HDEL hash field [...]
Returns the number of keys actually deleted. If the key does not exist, it will not be included in the number:
(integer) 1
HEXISTS
Check that the key is present.
HEXISTS hash field
Return 1 if it exists, return 0 if it does not exist:
(integer) 1
HGET
Gets the value of a single key.
HGET hash field
Return the value corresponding to the key:
“value”
Or return when there is no such key:
(nil)
HGETALL
Returns all key value pairs.
HGETALL hash
The key value interval returns in turn:
1) “field1”
2) “value1”
3) “field2”
4) “value2”
Or when the hash is empty:
(empty array)
HINCRBY
Increasing the value of a key can only be used for string types that are treated as integers. If the hash key does not exist, it will be created. If the hash key does not exist, it will be created with a value of 0.
HINCRBY hash field increment
Return the added value:
(integer) 1
HINCRBYFLOAT
The floating-point value of the increment key can only be used for string types treated as integers or floating-point numbers. If the hash key does not exist, it will be created. If the hash key does not exist, it will be created with a value of 0.
HINCRBYFLOAT hash field increment
Return the added value:
“0.1”
HKEYS
Returns all keys.
HKEYS hash
Return to the list of keys:
1) “field1”
2) “field2”
Or when the hash is empty:
(empty array)
HLEN
Returns the number of key value pairs.
HLEN hash
Quantity returned:
(integer) 1
HMGET
Gets the value of multiple keys.
HMGET hash field [...]
Return the value corresponding to the key in turn. If the key does not exist, the value is nil:
1) “value1”
2) (nil)
HMSET
Set the hash key value pair. If the hash does not exist, create it.
HMSET hash field value [field value ...]
Returns the number of keys actually added. If the key already exists, it will not be included in the number:
(integer) 1
HSET
Set the hash key value pair. If the hash does not exist, create it.
HSET hash field value [field value ...]
Returns the number of keys actually added. If the key already exists, it will not be included in the number:
(integer) 1
HVALS
Returns all values. The value may be duplicate.
HVALS hash
List of return values:
1) “value1”
2) “value2”
Or when the hash is empty:
(empty array)
delete
DEL
Delete key, available for all types.
DEL key [...]
Returns the number of deleted keys. If the key does not exist, it will not be included in the number:
(integer) 1
sort
SORT
Sort. It can sort list, set and ordered set (ignoring score).
SORT key [BY by_pattern] [LIMIT offset count] [GET get_pattern [GET get_pattern ...]] [ASC|DESC] [ALPHA] [STORE destlist]
BY
Specifies that external keys are used for sorting. Replace by with the element in key_ In patternNumber, which is used as the key to find the string, and then use this string as the basis for sorting (such as: user))。 Or replace by with the element in key_ In patternTo find out the unordered hash by using the key, and then use the value of the key in the unordered hash as the basis for sorting (e.g. user)->name)。 By_ If the pattern does not exist, the sorting will be skipped.
LIMIT
Limits the number of results returned, the number of skips, and the total number returned.
GET
Specifies the external key as the return result. Replace get with elements in key_ In patternNumber, which is used as the key to find the string, and then use this string as the return result (such as: user))。 Or replace by with the element in key_ In patternUse this as the key to find the unordered hash, and then use – > to take the value of the key in the unordered hash as the return result (for example: user)->name)。 get_ Pattern is #, indicating the element in the key.
ASC
In ascending order,DESC
The default is ascending.
ALPHA
To sort as a string, the default is to sort as a number.
STORE
Save the result to the list. If destlist does not exist, it will be created. If destlist already exists, it will be cleared.
Return sorted elements:
“1”
“2”
Or to return multiple external keys:
“1”
“a”
“2”
“b”
Or if there is no element:
(empty array)
If usedSTORE
Then return the number of sorted elements:
(integer) 1
be overdue
EXPIRE
The set key will expire in a few seconds.
EXPIRE key seconds
If the key exists, return 1, if it does not exist, return 0:
(integer) 1
EXPIREAT
Set the key to expire on second UNIX timestamp:
EXPIREAT key unix_timestamp_second
If the key exists, return 1, if it does not exist, return 0:
(integer) 1
PERSIST
Setting the key to permanent removes the expiration time.
PERSIST key
The original expiration time of the key returns 1, and the original permanent return time is 0
(integer) 1
PEXPIRE
The set key will expire in a few milliseconds.
PEXPIRE key millseconds
If the key exists, return 1, if it does not exist, return 0:
(integer) 1
PEXPIREAT
Set the key to expire in milliseconds UNIX timestamp:
PEXPIREAT key unix_timestamp_millsecond
If the key exists, return 1, if it does not exist, return 0:
(integer) 1
PTTL
View the number of milliseconds the key distance expired.
PTTL key
Returns the number of milliseconds, returns – 1 permanently, and returns – 2 when there is no message
(integer) 1
TTL
View the number of seconds the key distance has expired.
TTL key
Returns the number of seconds, returns – 1 permanently, and – 2 does not exist
(integer) 1
affair
The order of command execution is as follows:WATCH
、UNWATCH
(do not continue with the following command)MULTI
、DISCARD
(do not continue with the following command)EXEC
。
DISCARD
Cancels all monitoring of keys and clears usageMULTI
Order to join the team. stayMULTI
afterEXEC
Before.
DISCARD
return:
OK
EXEC
Execute the transaction.
EXEC
implementMULTI
toEXEC
Nesting returns the execution result of each command
1) “value”
2) 1) “element0”
2) “element1”
Execution failed, return:
(nil)
MULTI
Start the transaction.
MULTI
return:
OK
MULTI
toEXEC
All orders will be in the team and in theEXEC
During the execution, all commands return:
QUEUED
UNWATCH
Cancels all monitoring of keys. stayWATCH
afterMULTI
Before.
UNWATCH
return:
OK
WATCH
Monitor the key. implementWATCH
toEXEC
During this time, if other connections modify the key,EXEC
Execution will fail.
WATCH key [...]
return:
OK
Publish / subscribe
The message is a string.
PSUBSCRIBE
Subscribe to channels that match the glob pattern.
PSUBSCRIBE glob [...]
Return the event name (psubscribe), glob mode and the number of channels currently subscribed of each channel in turn
1) “psubscribe”
2) “glob1”
3) (integer) 1
1) “psubscribe”
2) “glob2”
3) (integer) 2
After that, the current connection is blocked until it is disconnected. During the period, the event name (pmessage), glob mode, channel name and message value of the message are output in turn
1) “pmessage”
2) “glob1”
3) “channel”
4) “msg”
PUNSUBSCRIBE
In redis cli, because the connection is blocked after subscription, this command cannot be used.
Unsubscribe from channels that match glob mode. If glob is not specified, unsubscribe all channels.
PUNSUBSCRIBE [glob [...]]
Return the event name (punsubscription), glob mode and the number of channels currently subscribed of each glob mode in turn. If the number of channels currently subscribed is 0, subsequent events will not be returned, but the events still exist in the connected queue
1) “punsubscribe”
2) “glob1”
3) (integer) 1
1) “punsubscribe”
2) “glob2”
3) (integer) 0
Return when no channel is specified and no channel is subscribed:
1) “punsubscribe”
2) (nil)
3) (integer) 0
PUBLISH
Send a message to the channel.
PUBLISH channel msg
Returns the number of connections that received the message:
(integer) 1
SUBSCRIBE
Subscribe to the channel.
SUBSCRIBE channel [...]
Return the event name (subscribe), channel name and the number of channels currently subscribed of each channel in turn
1) “subscribe”
2) “channel1”
3) (integer) 1
1) “subscribe”
2) “channel2”
3) (integer) 2
After that, the current connection is blocked until it is disconnected. The event name (message), channel name and message value of the message are output successively during the period
1) “message”
2) “channel1”
3) “msg”
UNSUBSCRIBE
In redis cli, because the connection is blocked after subscription, this command cannot be used.
Unsubscribe from the channel. If channel is not specified, unsubscribe all channels.
UNSUBSCRIBE [channel [...]]
Return the event name (unsubscribe), channel name and the number of channels currently subscribed of each channel in turn. If the number of channels currently subscribed is 0, subsequent events will not be returned, but the events still exist in the connected queue
1) “unsubscribe”
2) “channel1”
3) (integer) 1
1) “unsubscribe”
2) “channel2”
3) (integer) 0
Return when no channel is specified and no channel is subscribed:
1) “unsubscribe”
2) (nil)
3) (integer) 0
Persistence
BGREWRITEAOF
Rewrite the AOF file. The child process is responsible for writing AOF files to disk, and the parent process continues to process command requests.
BGREWRITEAOF
return:
Background append only file rewriting started
BGSAVE
Create a snapshot file in the background. The child process is responsible for writing the snapshot file to disk, and the parent process continues to process command requests.
BGSAVE
return:
Background saving started
SAVE
The foreground creates a snapshot file. No other commands are responded to until the snapshot file is created.
SAVE
return:
OK
copy
SLAVEOF
As a slave server, specify the master server, discard the existing data, and copy the data
SLAVEOF host port
Stop the replication, use the existing data, and switch from the slave server to the master server
SLAVEOF NO ONE
return:
OK
Administration
DEBUG OBJECT
Returns debugging information for an object.
DEBUG OBJECT key
Return debugging information:
Value at:0x7f4500a77c80 refcount:1 encoding:ziplist serializedlength:17 lru:11272693 lru_seconds_idle:6559102
If the key does not exist, return:
(error) ERR no such key
INFO
View server status information.
INFO section
The returned information includes several sections. Each section starts with a section name beginning with “#” and ends with an empty line. In the section, each information has a line with “:” to separate the name and value
# Modules
# Cluster
cluster_enabled:0
The meanings of the names are as follows:
- aof_ pending_ bio_ Fsync: the number of commands AOF waits to synchronize to disk.
SELECT
Select to switch to library n. The maximum value is 15 and the default value is 0. If it is not 0, the command line prompt is marked with “[n]”.
SELECT n
return:
OK
SHUTDOWN
Out of Service.
SHUTDOWN [NOSAVE|SAVE]
No return, connection disconnected.
script
EVAL
Execute Lua script code.
EVAL lua_code key_amount [key [...]] [arg [...]]
The key is Lua_ The keys used in code are used in Lua script in the form of keys [1]. Arg is Lua_ The additional parameters used in code are used in Lua script in the form of argv [1].
Return the return value of lua script. If there is a table, nested return:
1) “lua”
2) 1) (integer) 1
2) (integer) 2
Or when the failure returns an error:
(error) ERR wrong number of arguments for ‘eval’ command
EVALSHA
Execute the loaded Lua script code.
EVALSHA sha1 key_amount [key [...]] [arg [...]]
The key is Lua_ The keys used in code are used in Lua script in the form of keys [1]. Arg is Lua_ The additional parameters used in code are used in Lua script in the form of argv [1].
Return the return value of lua script. If there is a table, nested return:
1) “lua”
2) 1) (integer) 1
2) (integer) 2
Or when the failure returns an error:
(error) ERR wrong number of arguments for ‘evalsha’ command
SCRIPT FLUSH
Clear all loaded Lua script code.
SCRIPT FLUSH
return:
OK
SCRIPT KILL
Kill the executing Lua script.
SCRIPT KILL
return:
OK
If there is no executing Lua script, return:
(error) NOTBUSY No scripts in execution right now.
SCRIPT LOAD
Load but do not execute Lua script code:
SCRIPT LOAD lua_code
Return SHA1 check sum of script code:
“e0e1f9fabfc9d4800c877a703b823ac0578ff8db”
Or when the failure returns an error:
(error) ERR Unknown subcommand or wrong number of arguments for ‘load’. Try SCRIPT HELP.