Summary
Redis Introduction and Installation Reference: Introduction and installation of Redis
String type is the most basic data type in Redis. It can store any form of string, including binary data.
Introduction to Commands
set command
Syntax: SET key value [EX seconds] [PX milliseconds] [NX|XX] Initial version: 1.0.0 Summary: Set a string value for a key. Type: string Time complexity: O(1)
Introduction of parameters:
- EX seconds: Set the expiration time of the key in units of seconds.
- PX milliseconds: Set the expiration time of key in milliseconds.
- NX: The key value is set only when the key does not exist.
- XX: The key value is set only when the key exists.
Be careful:
Redis version 2.6.12 started to support the above parameters. Because the set commands can completely replace the functions of setnx, setex and psetex commands with the above parameters, they may be discarded in future versions. Therefore, it is not recommended to use these commands, but by adding parameters to the set.
The set command assigns a string value to a key, similar to assigning a variable in a compiled language. If the key has been assigned, the operation will directly override the original value and ignore the original type. When set assignment succeeds, the previous set expiration time will expire.
set command assignment returns ok successfully, and assignment failure returns (nil).
Example reference:
127.0.0.1:6379> set name "sgl" #Assign name to sgl
OK #Success, return to OK
127.0.0.1:6379> get name #View the value of key as name
"sgl"
127.0.0.1:6379> set name "guoguo" nx # Assignment only if name does not exist
(nil) # Since the name has been assigned above, the assignment is unsuccessful. Return (nil)
127.0.0.1:6379> get name # Because the set name "guoguo" nx assignment failed, the name value is still the original "sgl"
"sgl"
127.0.0.1:6379> set name "guoguo" xx # Assignment only if name exists
OK # Because name already exists, the assignment is successful
127.0.0.1:6379> get name # The value of name has changed to "guoguo"
"guoguo"
127.0.0.1:6379> set name "sgl" ex 10 # Assign the key name to "sgl" and set the effective time to 10 seconds
OK
127.0.0.1:6379> get name # View within 10 seconds after assignment. Value
"sgl"
127.0.0.1:6379> get name # 10 seconds to view again, the key name has been deleted
(nil)
127.0.0.1:6379> set name "sgl" px 5000 #The effective time is in milliseconds.
OK
127.0.0.1:6379> get name # Values are available in 5 seconds
"sgl"
127.0.0.1:6379> get name # Five seconds later, the key name has expired
(nil)
get command
Grammar: GET key Initial version: 1.0.0 Summary: Get the value of the key. Type: string Time complexity: O(1)
Gets the value of the specified key, returns the value corresponding to the key if it exists, returns (nil) if the key does not exist, and returns an error if the value corresponding to the key is not a string type, because get only processes the value of the string type.
Example reference:
127.0.0.1:6379> set name "sgl" # Set a key name with the value "sgl"
OK
127.0.0.1:6379> get name # Get the value of the key name
"sgl" # The key name exists and returns the corresponding value
127.0.0.1:6379> get age # Key age does not exist, return (nil)
(nil)
127.0.0.1:6379> lpush numbers 1 2 3 # Set a list of keys to numbers
(integer) 3
127.0.0.1:6379> get numbers # Because the value corresponding to the key numbers is not string type, the error message is very clear to indicate the type error by using get to get the value.
(error) WRONGTYPE Operation against a key holding the wrong kind of value
append command
Grammar: APPEND key value Initial version: 2.0.0 Summary: Adds a string value to the value corresponding to the specified key. Type: string Time complexity: Average time complexity is O(1)
The append command adds value to the end of the key value. Appnd adds several situations:
- If the key already exists and the value is a string, the value is appended to the original value.
- The key already exists, and the value is not a string, the error is reported, and the prompt type is incorrect.
- The key does not exist. First, create a key with an empty string value, and then append it.
After the append command is executed successfully, the length of the string value after appending is returned.
Example reference:
127.0.0.1:6379> get text
(nil)
127.0.0.1:6379> append text "sgl" # Append the string "sgl" to a non-existent key
(integer) 3 # It will set the string "sgl" and return the length
127.0.0.1:6379> get text
"sgl"
127.0.0.1:6379> append text " is a programmer." # Assign a key that already exists and is a string, appending it directly to the end
(integer) 20 # Returns the total length of the appended string
127.0.0.1:6379> get text
"sgl is a programmer."
127.0.0.1:6379> type numbers # Key numbers are list type
list
127.0.0.1:6379> append numbers " wrong type" # Errors will be reported for key s that are not string-type additions
(error) WRONGTYPE Operation against a key holding the wrong kind of value
strlen command
Grammar: STRLEN key Initial version: 2.2.0 Summary: Get the length of the key corresponding value Type: string Time complexity: O(1)
The strlen command returns the length of the string type value of the key, and if the value is not a string type, an error is reported. If key does not exist, return (integer)0
Example reference:
127.0.0.1:6379> strlen text # The key exists and its value is string type, and the length of the return value
(integer) 20
127.0.0.1:6379> get address
(nil)
127.0.0.1:6379> strlen address # The key does not exist, and the return length is 0, similar to the key of an empty string.
(integer) 0
127.0.0.1:6379> set address "" # Set the key address to an empty string
OK
127.0.0.1:6379> get address
""
127.0.0.1:6379> strlen address # Length 0
(integer) 0
127.0.0.1:6379> strlen numbers # key exists, but it is not string type. Error reporting
(error) WRONGTYPE Operation against a key holding the wrong kind of value
Note:
The examples are based on redis-3.2.9, CentOS 6.9, and Redis commands are case-insensitive. All the examples are in lowercase.
References: redis comes with its own help documentation, official documentation, and Redis Introduction Guide.