String of redis syntax introduction

Time:2019-12-10

String basic add, delete, modify and query commands:

127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379 > append K2 12dffsd // append the command
(integer) 9
127.0.0.1:6379> get k2
"v212dffsd"
127.0.0.1:6379 > strlen K2 // returns the length of the corresponding value of the key
(integer) 9
127.0.0.1:6379> get k2
"v212dffsd"
127.0.0.1:6379> set k1 4
OK
127.0.0.1:6379 > incr K1 // increase 1 at a time to return the increased value
(integer) 5
127.0.0.1:6379> get k1
"5"
127.0.0.1:6379 > decr K1 // decrease 1 at a time, and return the reduced value
(integer) 4
127.0.0.1:6379 > incrby K1 4 // directly increase the following value
(integer) 8
127.0.0.1:6379 > decrby K1 2 // directly reduce the later value
(integer) 6
127.0.0.1:6379 > incr K2 // cannot operate on a value that is not a number
(error) ERR value is not an integer or out of range

Incr / decr / incrby / decrby can only operate on values that are numeric

127.0.0.1:6379> get k2
"v212dffsd"
127.0.0.1:6379 > getrange K2 0 - 1 // returns the string between consecutive positions, including both sides
"v212dffsd"
127.0.0.1:6379> getrange k2 03
(error) ERR wrong number of arguments for 'getrange' command
127.0.0.1:6379> getrange k2 0 3
"v212"
127.0.0.1:6379 > setrange K2 0 XXX // replace characters with XXX from position 0
(integer) 9
127.0.0.1:6379> get k2
"xxx2dffsd"

Setex (set with expire) key second value / setnx (set if not exist)

127.0.0.1:6379> setex k2 10 sss
OK
127.0.0.1:6379> ttl k2
(integer) 6
127.0.0.1:6379> setnx k1 44
(integer) 1
127.0.0.1:6379> setnx k1 77
(integer) 0
127.0.0.1:6379>

You can also batch set and get values through Mset and mget

127.0.0.1:6379> mset k1 v1 k2 v2
OK
127.0.0.1:6379> mget k1 k2
1) "v1"
2) "v2"
127.0.0.1:6379 > msetnx K3 V3 K4 V4 // all instructions are executed unsuccessfully
(integer) 1
127.0.0.1:6379> msetnx k3 v33 k5 v5
(integer) 0
127.0.0.1:6379> get k3
"v3"