1. append key value

Append string, create if not

127.0. 01.:6379> set name hello
OK
127.0. 01.:6379> append name lin
(integer) 8
127.0. 01.:6379> get name
"hellolin"
127.0. 01.:6379> append age 18
(integer) 2
127.0. 01.:6379> get age
"18"
Copy the code

2. strlen

Strlen key gets the length of the string

127.0. 01.:6379> strlen age
(integer) 2
Copy the code

3. incr decr

Incr key = i++

Decr key decrement = I —

127.0. 01.:6379> incr age
(integer) 19
127.0. 01.:6379> incr age
(integer) 20
127.0. 01.:6379> decr age
(integer) 19
127.0. 01.:6379> decr age
(integer) 18
Copy the code

4. incrby decrby

Incrby key number The number of the key is automatically increased = I +=number

Decrby key number The key is subtracted from the number = I -=number

127.0. 01.:6379> incrby age 10
(integer) 28
127.0. 01.:6379> incrby age 10
(integer) 38
127.0. 01.:6379> decrby age 5
(integer) 33
127.0. 01.:6379> decrby age 5
(integer) 28
Copy the code

5. getrange

Getrange Key start end The character string is intercepted

127.0. 01.:6379> get name
"hellolin"
127.0. 01.:6379> getrange name 0 3# get name [0]~name[3]
"hell"
127.0. 01.:6379> getrange name 0 -1Get all strings"hellolin"
Copy the code

6. Replace setrange

Setrange key offest value

127.0. 01.:6379> get name
"hellolin"
127.0. 01.:6379> setrange name 4 o,
(integer) 8
127.0. 01.:6379> get name
"hello,in"
127.0. 01.:6379> setrange name 4 xxxxx
(integer) 9
127.0. 01.:6379> get name
"hellxxxxx"
Copy the code

7. setex setnx

# setex key seconds value (set_WITH_EXPIRE) sets a value and sets its expiration time127.0. 01.:6379> setex key1 30 hello
OK
127.0. 01.:6379> ttl key1
(integer) 24
127.0. 01.:6379> ttl key1
(integer) 23
127.0. 01.:6379> ttl key1
(integer) 17
127.0. 01.:6379> ttl key1
(integer) -2
127.0. 01.:6379> get key1
(nil)
# setnx key value (set ifNot exists (distributed lock) If the lock does not exist, the lock fails to be created127.0. 01.:6379> get name
"hellxxxxx"
127.0. 01.:6379> setnx name hello
(integer) 0
127.0. 01.:6379> get name
"hellxxxxx"
127.0. 01.:6379> setnx key2 hello
(integer) 1
127.0. 01.:6379> get key2
"hello"

Copy the code

8. Batch operation mset mget msetnx

127.0. 01.:6379> mset k1 v1 k2 v2 k3 v3 # mset key value key2 value2.... OK127.0. 01.:6379> keys *
1) "k2"
2) "k1"
3) "k3"
127.0. 01.:6379> mget k1 k2 k3 #批量获取 mget key1 key2.....
1) "v1"
2) "v2"
3) "v3"
127.0. 01.:6379> msetnx k4 v4 k1 v1 # Atomic operation, all successful or all failed (integer)0
127.0. 01.:6379> get k4
(nil)
127.0. 01.:6379> 
Copy the code

9. Object operations

127.0. 01.:6379> set user:1 {name:lin,age:18}
OK
127.0. 01.:6379> get user
(nil)
127.0. 01.:6379> get user:1
"{name:lin,age:18}"
127.0. 01.:6379> mset user:1:name lin user:1:age 11
OK
127.0. 01.:6379> mget user:1:name user:1:age
1) "lin"
2) "11"
127.0. 01.:6379> 
Copy the code

10. getset

Get first and then set, display the value of GET and then set it

127.0. 01.:6379> getset db redis
(nil)
127.0. 01.:6379> get db
"redis"
127.0. 01.:6379> getset db mongdb
"redis"
127.0. 01.:6379> get db
"mongdb"

Copy the code