1, Introduction to r edis supported data types
Redis supports five data types: string, hash, list, set and Zset.
2, String type
- String type is the most basic data type of Redis. String storage is stored in the form of key value
- String types are binary safe. This means that the redis string can contain any data. For example, jpg images or serialized objects.
- string type is the most basic data type of Redis. One key can store 512MB at most.
1. GET and SET commands:
#Set < key > < value > add a key value pair, get < key > query the corresponding key value 127.0.0.1:6379> set k "www.baidu.com" #Store www.baidu.com to the k value OK 127.0.0.1:6379> get k #View the value corresponding to the key value "www.baidu.com" 127.0.0.1:6379>
2. Key command (view all key values):
127.0.0.1:6379> keys * #View all key values 1) "age" 2) "k" 3) "name" 127.0.0.1:6379>
3. Exists command (judge whether a key exists):
#Syntax: exists key #Judge whether the key value exists. If it exists, it returns 1 and if not, it returns 0. 127.0.0.1:6379> keys * #View all key values 1) "age" 2) "k" 3) "name" 127.0.0.1:6379> exists age #Judge whether the key value is age, and return 1 if it exists, (integer) 1 127.0.0.1:6379> exists www #Judge whether the key value is www, and return 0 if it does not exist (integer) 0 127.0.0.1:6379>
4. Append appends the given value to the end of the original value (similar to the append method of String in java)
#Syntax append < key > < value > 127.0.0.1:6379> keys * #View all key values of the current library 1) "age" 2) "k" 3) "name" 127.0.0.1:6379> get name #Get the value value with key as name. "zhangsan" 127.0.0.1:6379> append name " is my friend" #Add is my friend after zhangsan (integer) 21 127.0.0.1:6379> get name #Get the value value with key name again "zhangsan is my friend" 127.0.0.1:6379>
5. strlen gets the length of the value corresponding to the key
127.0.0.1:6379> keys * #Get all key values 1) "age" 2) "k" 3) "name" 127.0.0.1:6379> get name "zhangsan is my friend" 127.0.0.1:6379> strlen name #View the length of value corresponding to name, that is, the length of value value (integer) 21 127.0.0.1:6379>
6,setnx <key> < Value > set the value of the key only when the key does not exist
#Syntax: etnx < key > < value > set the value of the key only when the key does not exist 127.0.0.1:6379> keys * 1) "age" 2) "k" 3) "name" 127.0.0.1:6379> get k "www.baidu.com" 127.0.0.1:6379> setnx k "lisi" #First judge whether there is a K, return 0 if there is, and do not modify the value value (integer) 0 127.0.0.1:6379> setnx k1 "lisi" #Judge whether k1 exists, return 1 if it does not exist, and modify the value to "lisi" (integer) 1 127.0.0.1:6379> keys * #View all the key s and create a new k1 1) "age" 2) "k" 3) "k1" 4) "name" 127.0.0.1:6379> get k1 #Get the value of k1 and verify "lisi" 127.0.0.1:6379>
7. Incr < key > increasing the digital value stored in the key by 1 can only operate on the digital value. If it is empty, the new increment is 1
127.0.0.1:6379> keys * #The current database is empty (empty array) 127.0.0.1:6379> set age 12 #Stored value OK 127.0.0.1:6379> incr age #Let's add one (integer) 13 #See that the returned result is 13 127.0.0.1:6379> keys * 1) "age" 127.0.0.1:6379> incr name #Create a new key with a value of name and a value of 1 (integer) 1 127.0.0.1:6379>
Atomic operation: perform atomic operation on incr < key >.
Atomic operation refers to the operation that will not be interrupted by thread scheduling mechanism; Once this operation starts, it runs until the end without any context switch (switching to another thread).
(1) In a single thread, any operation that can be completed in a single instruction can be regarded as an "atomic operation", because interrupts can only occur between instructions.
(2) In multithreading, operations that cannot be interrupted by other processes (threads) are called atomic operations.
Atomic operation mainly benefits from the single thread of redis.
8. Incr < key > subtracting 1 from the digital value stored in the key can only operate on the digital value. If it is empty, the new increment is - 1
127.0.0.1:6379> keys * 1) "age" 127.0.0.1:6379> get age "13" 127.0.0.1:6379> decr age #Age minus one (integer) 12 127.0.0.1:6379> decr name #Create a new key with a value of name and a value of - 1 (integer) -1 127.0.0.1:6379>
9. Incrby < key > < step > increase the numerical value stored in the key by the specified step
127.0.0.1:6379> set views 1 #Add a new String OK 127.0.0.1:6379> incrby views 10 #Increase by 10 on the original basis (integer) 11 127.0.0.1:6379> get views "11" 127.0.0.1:6379> incrby test 10 #If there is no test, create a new test. The initial value is 0 plus step 10 (integer) 10 127.0.0.1:6379> get test "10" 127.0.0.1:6379>
10. Decrby < key > < step > reduce the digital value stored in key by the specified step
127.0.0.1:6379> keys * 1) "views" 2) "test" 127.0.0.1:6379> get views "11" 127.0.0.1:6379> decrby views 10 #Reduce by 10 on the original basis (integer) 1 127.0.0.1:6379> decrby ww 10 #The initial default is 0, reduced by 10 (integer) -10 127.0.0.1:6379>
11. The Mset command is used to set one or more key value pairs at the same time.
127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 k4 v4 #Set multiple values OK 127.0.0.1:6379> keys * 1) "k2" 2) "k4" 3) "k3" 4) "k1" 127.0.0.1:6379>
12. The Mget command returns the values of all (one or more) given keys. If a given key does not exist, the key returns the special value nil.
127.0.0.1:6379> keys * 1) "k2" 2) "k4" 3) "k3" 4) "k1" 127.0.0.1:6379> mget k1 k2 k6 #k6 does not exist, return null 1) "v1" 2) "v2" 3) (nil) 127.0.0.1:6379>
13. The Getrange command gets the substring of the string stored in the specified key. The interception range of the string is determined by the offset of start and end (including start and end).
127.0.0.1:6379> set name "hello,redis world" #Set string OK 127.0.0.1:6379> get name "hello,redis world" 127.0.0.1:6379> getrange name 0 5 #Intercepting strings in the range 0-5 "hello," 127.0.0.1:6379> getrange name 0 -1 #View the string, which is equivalent to get < key > "hello,redis world" 127.0.0.1:6379>
14. The Setrange command overwrites the string value stored by a given key with the specified string, starting from the offset to the end.
127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> get name "hello,redis world" 127.0.0.1:6379> setrange name 12 study #Overwrite all strings after offset (integer) 18 127.0.0.1:6379> get name "hello,redis studyd" 127.0.0.1:6379>
15. The command is used to set the value of the specified key and return the old value of the key.
127.0.0.1:6379> set name zhangsan OK 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> getset name lisi #Get the original value and store the new value in the value corresponding to the current key. "zhangsan" 127.0.0.1:6379> get name "lisi" 127.0.0.1:6379>