Redis命令
redis命令不区分大小写 用 set 或 SET 都可以
但是 key value 是区分大小的
(1) 公共
查看key个数 DBSIZE
127.0.0.1:6379> DBSIZE
(integer) 5
是否存在key (EXISTS key)
127.0.0.1:6379> EXISTS key1
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> EXISTS key2
(integer) 0
exists存在key返回1,不存在key返回0
key设置过期时间
127.0.0.1:6379> EXPIRE key1 100
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> TTL key1
(integer) 96
查看key过期时间
127.0.0.1:6379> TTL key1
(integer) 96
查看key的类型
127.0.0.1:6379> TYPE key1
string
127.0.0.1:6379> TYPE key_num_list
list
127.0.0.1:6379>
key重命名
127.0.0.1:6379> RENAME key1 key2
OK
scan
https://redis.io/commands/scan/
查看key个数
127.0.0.1:6379> DBSIZE
(integer) 5
查看具体有哪些key
127.0.0.1:6379> KEYS *
1) "key1"
2) "key_nx_1"
3) "key_nx"
4) "key_num_list"
5) "key2"
127.0.0.1:6379>
通过游标扫描
127.0.0.1:6379> SCAN 0 COUNT 2
1) "1"
2) 1) "key_nx"
2) "key2"
127.0.0.1:6379> SCAN 1 COUNT 2
1) "5"
2) 1) "key1"
2) "key_nx_1"
127.0.0.1:6379> SCAN 5 COUNT 2
1) "0"
2) 1) "key_num_list"
127.0.0.1:6379>
(2) 字符串(String)
设置键值对 (SET key value)
127.0.0.1:6379> set key1 value1
OK
根据key获取value (Get key)
127.0.0.1:6379> GET key1
"value1"
127.0.0.1:6379>
127.0.0.1:6379> GET key1000
(nil)
key存在返回对应value,key不存在返回nil
设置带过期时间的键值对(SET key value EX seconds)
127.0.0.1:6379> SET key_ex value_ex EX 100
OK
127.0.0.1:6379> TTL key_ex
(integer) 93
127.0.0.1:6379> TTL key_ex
(integer) 91
如果不存在就设置键值对(SET key value NX)
127.0.0.1:6379> SET key_nx_1 value_nx_1 NX
OK
127.0.0.1:6379> SET key_nx_1 value_nx_1 NX
(nil)
SET key value NX 成功的结果是OK
,失败的结果是nil
如果不存在就设置键值对(SETNX key value)
127.0.0.1:6379> SETNX key_nx value_nx
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> SETNX key_nx value_nx
(integer) 0
127.0.0.1:6379>
setnx key value 成功的结果是1,失败的结果是0
如果不存在设置键值对并带过期时间(SET key value EX seconds NX)
127.0.0.1:6379> SET key2 value2 EX 100 NX
OK
127.0.0.1:6379>
127.0.0.1:6379> SET key2 value2 EX 100 NX
(nil)
127.0.0.1:6379>
获取多个给定key的value(MGET key1 key2 …)
127.0.0.1:6379> MGET key1 key2
1) "value1"
2) "value2"
127.0.0.1:6379>
127.0.0.1:6379> MGET key1 key2 key1000
1) "value1"
2) "value2"
3) (nil)
127.0.0.1:6379>
127.0.0.1:6379> MGET key1 key1000 key2
1) "value1"
2) (nil)
3) "value2"
127.0.0.1:6379>
key都存在时返回对应value,有key不存在时对应的value为nil
设置key值为value并返回key的旧值(GETSET)
127.0.0.1:6379> GETSET key1 value_x
"value1"
127.0.0.1:6379>
获取key对应值的字符串的长度(STRLEN)
127.0.0.1:6379> STRLEN key1
(integer) 6
127.0.0.1:6379> GET key1
"value1"
127.0.0.1:6379> STRLEN key_num_1
(integer) 2
127.0.0.1:6379> GET key_num_1
"99"
key对应的value可以是字符串,也可以是数字
将key对应value的数字值增加一(INCR key)
127.0.0.1:6379> SET key_num_1 100
OK
127.0.0.1:6379> INCR key_num_1
(integer) 101
127.0.0.1:6379> GET key_num_10
"99_age_20"
127.0.0.1:6379>
127.0.0.1:6379> INCR key_num_10
(error) ERR value is not an integer or out of range
127.0.0.1:6379>
如果key对应的value不是数字类型,会报错
将key对应value的数字值增加n(INCRBY key n)
127.0.0.1:6379> SET key_num_2 100
OK
127.0.0.1:6379> INCRBY key_num_2 10
(integer) 110
127.0.0.1:6379> INCRBY key_num_2 -20
(integer) 90
n可以是正数,也可以是负数
将key对应value的数字值增减一(DECR key)
127.0.0.1:6379> SET key_num_3 100
OK
127.0.0.1:6379> DECR key_num_3
(integer) 99
将key对应value的数字值增减n(DECRBY key n)
127.0.0.1:6379> SET key_num_4 100
OK
127.0.0.1:6379> DECRBY key_num_4 10
(integer) 90
127.0.0.1:6379>
APPEND
127.0.0.1:6379> GET key1
"value1"
127.0.0.1:6379> APPEND key1 age:20
(integer) 12
127.0.0.1:6379>
127.0.0.1:6379> GET key1
"value1age:20"
127.0.0.1:6379>
127.0.0.1:6379> APPEND key_num_1 _age_20
(integer) 9
127.0.0.1:6379>
127.0.0.1:6379> GET key_num_1
"99_age_20"
127.0.0.1:6379>
List
Hash
Set
Sorted Set
参考资料
[1] redis命令
[2] redis.io/commands