Redis数据结构之字符串
目录
Redis数据结构之字符串
添加
SET
127.0.0.1:6379> set a 12
OK
获取
GET
127.0.0.1:6379> get a
12
#获取所有key
127.0.0.1:6379> keys *
c
b
a
修改
SET
127.0.0.1:6379> set a 15
OK
127.0.0.1:6379> get a
15
删除
DEL
127.0.0.1:6379> del a
1
127.0.0.1:6379> get a
判断一个key是否存在
EXISTS
127.0.0.1:6379> EXISTS name
0
127.0.0.1:6379> SET name df
OK
127.0.0.1:6379> EXISTS name
1
查看过期时间
TTL
127.0.0.1:6379> TTL name
-1 #永不过期
127.0.0.1:6379> TTL name
3 #还有3s过期
127.0.0.1:6379> TTL name
-2 #已经过期
设置过期时间
px ex
#设置一个以毫秒为单位的过期时间
127.0.0.1:6379> set name df px 10000
OK
#设置一个以秒为单位的过期时间
127.0.0.1:6379> set df ddff ex 10
OK
合并set和ex
SETEX
127.0.0.1:6379> SETEX name 10 df
OK
合并set和px
PSETEX
127.0.0.1:6379> PSETEX name 1000 df
OK
判断一个key是否存在,存在则忽略,不存在则创建
nx
127.0.0.1:6379> set a 23
OK
127.0.0.1:6379> set a 34 nx #此时key a 存在,则值不变
127.0.0.1:6379> get a
23
127.0.0.1:6379> set b 34 nx #此时key b 不存在,则创建
OK
127.0.0.1:6379> get b
34
合并set和nx
SETNX
127.0.0.1:6379> SETNX a 34
0
127.0.0.1:6379> get a
23
127.0.0.1:6379> SETNX c 73
1
127.0.0.1:6379> get c
73
判断一个kye是否存在,存在则更新,不存在则忽略
xx
127.0.0.1:6379> get a
23
127.0.0.1:6379> set a 84 xx
OK
127.0.0.1:6379> get a
84
127.0.0.1:6379> set d 85 xx
127.0.0.1:6379> get d
设置多个值
MSET
127.0.0.1:6379> MSET a 12 b 15 c 18
OK
127.0.0.1:6379> keys *
c
b
a
127.0.0.1:6379> get a
12
127.0.0.1:6379> get b
15
127.0.0.1:6379> get c
18
获取后更改
GETSET
127.0.0.1:6379> get a
12
127.0.0.1:6379> getset a 14
12
127.0.0.1:6379> get a
14
按照下标更新
SETRANGE
127.0.0.1:6379> set a 1234567
OK
127.0.0.1:6379> get a
1234567
127.0.0.1:6379> SETRANGE a 3 xx
7
127.0.0.1:6379> get a
123xx67
获取多个key的值
MGET
127.0.0.1:6379> get a
123xx67
127.0.0.1:6379> get b
23
127.0.0.1:6379> MGET a b
123xx67
23
截取
GETRANGE
127.0.0.1:6379> get a
1234567
127.0.0.1:6379> GETRANGE a 2 -1 #获取全部
34567
127.0.0.1:6379> GETRANGE a 2 2 #获取第2个到第2个
3
127.0.0.1:6379> GETRANGE a 2 4 #获取第2个到第4个
345
计数
递增
INCR
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> INCR a
2
127.0.0.1:6379> INCR a
3
127.0.0.1:6379> INCR a
4
127.0.0.1:6379> INCR a
5
递减
DECR
127.0.0.1:6379> get a
5
127.0.0.1:6379> DECR a
4
127.0.0.1:6379> DECR a
3
127.0.0.1:6379> DECR a
2
127.0.0.1:6379> DECR a
1
按照步长递增
INCRBY
127.0.0.1:6379> get a
1
127.0.0.1:6379> INCRBY a 5
6
127.0.0.1:6379> INCRBY a 5
11
127.0.0.1:6379> INCRBY a 5
16
127.0.0.1:6379> INCRBY a 5
21
127.0.0.1:6379> INCRBY a 5
26
按照步长递减
DECRBY
127.0.0.1:6379> get a
26
127.0.0.1:6379> DECRBY a 6
20
127.0.0.1:6379> DECRBY a 6
14
127.0.0.1:6379> DECRBY a 6
8
127.0.0.1:6379> DECRBY a 6
2
追加
APPEND
127.0.0.1:6379> get a
2
127.0.0.1:6379> APPEND a dddfff
7
127.0.0.1:6379> get a
2dddfff