NoSQL development in more or less will be used, but also the interview must ask knowledge points. I’ve been asked about every interview in the last few days. But feel the answer is not good, there are a lot of knowledge points need to comb.
You can view the command on the official website: www.redis.cn/commands.ht…
Redis-key
127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set name XXX OK 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> set age 1 OK 127.0.0.1:6379> keys * 1) "age" 2) "name" 127.0.0.1:6379> exists name # (integer) 1 127.0.0.1:6379> exists name1 (integer) 0 127.0.0.1:6379> Move Name 1 (integer) 1 127.0.0.1:6379> keys * 1) "Age" 127.0.0.1:6379> set name yyy OK 127.0.0.1:6379> expire name 10 # The unit is second (integer) 1 127.0.0.1:6379> TTL name # Checking the remaining expiration time of the current key (integer) 7 127.0.0.1:6379> TTL name (integer) -2 127.0.0.1:6379> string 127.0.0.1:6379>Copy the code
Redis has the following five basic data types
1, String (String)
127.0.0.1:6379> set key1 v1 # set value OK 127.0.0.1:6379> get key1 "v1" 127.0.0.1:6379> append key1 "hello" # append Equivalent to set key (integer) 7 127.0.0.1:6379> get key1 "v1Hello" 127.0.0.1:6379> strlen key1 # Get string length (integer) 7 127.0.0.1:6379 >Copy the code
Self-increasing and self-decreasing
127.0.0.1:6379> set views 0 OK 127.0.0.1:6379> get views "0" 127.0.0.1:6379> INCr views # increase 1 (integer) 1 127.0.0.1:6379> get views "1" 127.0.0.1:6379> Decr views # descending 1 (INTEGER) 0 127.0.0.1:6379> DECr views (integer) -1 127.0.0.1:6379> get views "-1" 127.0.0.1:6379> incrby views 10 # Set the step, increment 10 (integer) 9 127.0.0.1:6379> decrby views 5 # Set the step size to 5 (integer) 4Copy the code
String range
127.0.0.1:6379 > set key1 "hello, world!" OK 127.0.0.1:6379> get key1 "Hello,world!" 127.0.0.1:6379> getrange key1 0 3 # intercept string [0, 3] "hell" 127.0.0.1:6379> getrange key1 0 -1 Same as get key "Hello,world!" 127.0.0.1:6379 >Copy the code
Replacement:
127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> get key2
"abcdefg"
127.0.0.1:6379> setrange key2 1 xx
(integer) 7
127.0.0.1:6379> get key2
"axxdefg"
127.0.0.1:6379>
Copy the code
Setex (set with expire) : Sets the expiration time
And setnx(Set if not exist) : no more setting (often used in distributed locks)
127.0.0.1:6379> setex key3 30 "hello" # set to expire after 30 seconds OK 127.0.0.1:6379> TTL key3 # Remaining expired time (integer) 25 127.0.0.1:6379> (INTEGER) 1 127.0.0.1:6379> keys * 1) "key2" 2) "key1" 3) "views" 4) "mykey" 127.0.0.1:6379> setnx mykey "mongoDB" # mykey failed to be set (integer) 0 127.0.0.1:6379> get mykey "redis" 127.0.0.1:6379 >Copy the code
Mset and mget
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # set multiple values OK 127.0.0.1:6379> keys * 1) "k1" 2) "k3" 3) "k2" 127.0.0.1:6379> mget 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> msetnx k1 v1 k4 v4 # msetnx (integer) 0 127.0.0.1:6379> get k4 (nil) 127.0.0.1:6379>Copy the code
object
set user:1 {name:zhangsan, Age :3} # set a user:1 object value to json character to save an object 127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2 OK 127.0.0.1:6379> mget User :1:name user:1:age 1) "zhangsan" 2) "2" 127.0.0.1:6379>Copy the code
Getset: Get and then set
127.0.0.1:6379> getset db redis # 127.0.0.1:6379> get db "redis" 127.0.0.1:6379> getset db mongodb # 127.0.0.1:6379> get db" mongodb" 127.0.0.1:6379>Copy the code
A value can also be a number in addition to a String
- counter
- Count the number of multiple units
- Number of fans
- Object cache storage
The basic data type, List.
In Redis, you can use lists as stacks, queues, and blocking queues.
Most list commands start with l.
127.0.0.1:6379> lpush list one Insert into the head of the list (left) (INTEGER) 1 127.0.0.1:6379> lpush list two (integer) 2 127.0.0.1:6379> lpush list three (integer) 3 127.0.0.1:6379> lrange list 0-1 1) "three" 2) "two" 3) "one" 127.0.0.1:6379> lrange list 0 "Three" 2) "two" 127.0.0.1:6379> rpush list right # Insert into list (right) (INTEGER) 4 127.0.0.1:6379> lrange list 0-1 1) "three" 2) "two" 3) "one" 4) "right" 127.0.0.1:6379>Copy the code
Pop-up pop
127.0.0.1:6379> lrange list 0-1 1) "" 2) "world" 3) "world" 4) "hello" 127.0.0.1:6379> lpop list # remove first element "!" 127.0.0.1:6379> lrange list 0-1 1) "world" 2) "world" 3) "hello" 127.0.0.1:6379> rpop list # remove first element "hello" from list 127.0.0.1:6379> lrange list 0-1 1) "world" 2) "world" 127.0.0.1:6379>Copy the code
The index Lindex
127.0.0.1:6379> lrange list 0-1 1) "HJK" 2) "world" 3) "world" 127.0.0.1:6379> lindex list 1 127.0.0.1:6379> lindex list 0 "HJK" 127.0.0.1:6379>Copy the code
Llen length:
127.0.0.1:6379> llen list
(integer) 3
127.0.0.1:6379>
Copy the code
Removes the specified value:
127.0.0.1:6379> lrange list 0-1 1) "HJK" 2) "world" 3) "world" 127.0.0.1:6379> lrem list 1 world # Exact match (integer) 1 127.0.0.1:6379> lrange list 0-1 1) "HJK" 2) "world" 127.0.0.1:6379> lpush list HJK (integer) 3 127.0.0.1:6379> lrange list 0-1 1) "HJK" 2) "HJK" 3) "world" 127.0.0.1:6379> Lrem list 2 HJK (integer) 2 127.0.0.1:6379> lrange list 0-1 1) "world" 127.0.0.1:6379>Copy the code
Trim off
127.0.0.1:6379> lrange myList 0 -1 1) "hello1" 2) "hello2" 3) "hello3" 4) "hello4" 127.0.0.1:6379> ltrim myList 12 # 127.0.0.1:6379> lrange myList 0-1 1) "hello2" 2) "hello3" 127.0.0.1:6379>Copy the code
Rpoplpush: Remove the last element of the list and move it to the new list.
127.0.0.1:6379> lrange myList 0 -1 1) "hello1" 2) "hello2" 3) "hello3" 127.0.0.1:6379> rpoplpush myList myotherList # Remove the last element of the list and move it to the new list. "Hello3" 127.0.0.1:6379> lrange myList 0-1 # check the original list 1) "hello1" 2) "hello2" 127.0.0.1:6379> lrange myOtherList 0-1 # 1) "hello3" 127.0.0.1:6379>Copy the code
Lset: Replace the index value in the list with another value, update operation
127.0.0.1:6379> exists list # Check whether this list exists (integer) 0 127.0.0.1:6379> lset list 0 item # ERR no such key 127.0.0.1:6379> lpush list value1 (integer) 1 127.0.0.1:6379> lrange list 0 0 1) "value1" 127.0.0.1:6379> lset list 0 item # if present, update current subscript value OK 127.0.0.1:6379> lset list 1 other # if present, ERR index out of range 127.0.0.1:6379>Copy the code
Linsert: Inserts a specific value before or after an element in a list
127.0.0.1:6379> lrange myList 0-1 1) "hello1" 2) "hello2" 127.0.0.1:6379> linsert myList before "hello2" hello (INTEGER) 3 127.0.0.1:6379> lrange MyList 0-1 1) "hello1" 2) "hello" 3) "hello2" 127.0.0.1:6379> linsert myList after "Hello2 "hello (integer) 4 127.0.0.1:6379> lrange MyList 0-1 1) "hello1" 2) "hello" 3) "hello2" 4) "hello" 127.0.0.1:6379 >Copy the code