【Redis】四 数据类型 String
一个键最多存储 512 MB
get / set
set key value [ex seconds / px milliseconds / nx /xx ]同名key覆盖
127.0.0.1:6379> set test 'this is a test'
OK
127.0.0.1:6379> get test
"this is a test"
127.0.0.1:6379> set test 'this is a aaaaaa'
OK
127.0.0.1:6379> get test
"this is a aaaaaa"
127.0.0.1:6379>
##无key返回nil
127.0.0.1:6379> get notkey
(nil)
127.0.0.1:6379>
getset
127.0.0.1:6379> set test 'test'
OK
127.0.0.1:6379> get test
"test"
127.0.0.1:6379> getset test 'cccc'
"test"
127.0.0.1:6379> getset notkey 'notkey'
(nil)
gettrange key start end 截取
127.0.0.1:6379> getrange test 0 3
"this"
127.0.0.1:6379> get test
"this is a test"
127.0.0.1:6379> getrange test 0 -1
"this is a test"
127.0.0.1:6379> getrange test 0 -2
"this is a tes"
127.0.0.1:6379> getrange test 0 -3
"this is a te"
127.0.0.1:6379> getrange test -4 -1
"test"
setrange key offset value 设置偏移量
127.0.0.1:6379> set test 'hello test'
OK
127.0.0.1:6379> get test
"hello test"
127.0.0.1:6379> setrange test 6 'world'
(integer) 11
127.0.0.1:6379> get test
"hello world"
127.0.0.1:6379> strlen test
(integer) 11
//如Key原字符长度小于偏移量 , 会以\x00填充
127.0.0.1:6379> exists notkey48349
(integer) 0
127.0.0.1:6379> setrange notkey48349 5 'world'
(integer) 10
127.0.0.1:6379> get notkey48349
"\x00\x00\x00\x00\x00world"
mset/mget key value [key value … ] 批量设置/ 读取
127.0.0.1:6379> mset t1 1 t2 2
OK
127.0.0.1:6379> mget t1 t2
1) "1"
2) "2"
127.0.0.1:6379> mget t1 t2 t3 t4
1) "1"
2) "2"
3) (nil)
4) (nil)
strlen key 长度
127.0.0.1:6379> get test
"cccc"
127.0.0.1:6379> strlen test
(integer) 4
127.0.0.1:6379> strlen notkey321
(integer) 0
exists 判断key是否存在
127.0.0.1:6379> get test
"hello world"
127.0.0.1:6379> exists test
(integer) 1
127.0.0.1:6379> exists notkey48349
(integer) 0
127.0.0.1:6379>
setnx 当Key不存在,才成功
127.0.0.1:6379>
127.0.0.1:6379> exists notkey98345
(integer) 0
127.0.0.1:6379> setnx notkey98345 'notkey98345'
(integer) 1
127.0.0.1:6379> setnx notkey98345 'notkey98345'
(integer) 0
127.0.0.1:6379> get notkey98345
"notkey98345"
setex key 多少秒时间过期
原子操作 set key / expire key seconds
当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以毫秒为单位,返回 key 的剩余生存时间。
注意:在 Redis 2.8 以前,当 key 不存在,或者 key 没有设置剩余生存时间时,命令都返回 -1 。
127.0.0.1:6379> setex test 60 'test'
OK
127.0.0.1:6379> get test
"test"
127.0.0.1:6379> ttl test
(integer) 43
127.0.0.1:6379> ttl test
(integer) 40
127.0.0.1:6379> ttl test
(integer) 14
127.0.0.1:6379> ttl test
(integer) -2
127.0.0.1:6379> ttl test
(integer) -2
127.0.0.1:6379> ttl test
(integer) -2
127.0.0.1:6379> get test
(nil)
setex key 原子操作 相当于 set key / expire key seconds
127.0.0.1:6379> set test 'test'
OK
127.0.0.1:6379> expire test 10
(integer) 1
127.0.0.1:6379> ttl test
(integer) 6
127.0.0.1:6379> ttl test
(integer) 4
127.0.0.1:6379> ttl test
(integer) 3
127.0.0.1:6379> ttl test
(integer) 2
127.0.0.1:6379> ttl test
(integer) 2
127.0.0.1:6379> ttl test
(integer) -2
127.0.0.1:6379> ttl test
(integer) -2
127.0.0.1:6379> get tst
(nil)
127.0.0.1:6379> get test
(nil)
msetnx key不存在成功 原子性
127.0.0.1:6379> msetnx t5 't5' t6 't6'
(integer) 1
127.0.0.1:6379> get t5
"t5"
127.0.0.1:6379> get t6
"t6"
127.0.0.1:6379> msetnx t5 't5' t6 't6'
(integer) 0
127.0.0.1:6379> msetnx t5 't5' t6 't6' t7 't7'
(integer) 0
127.0.0.1:6379> exists t7
(integer) 0
psetex 毫秒 key 生命周期
127.0.0.1:6379> psetex hello 20000 'hello world'
OK
127.0.0.1:6379> pttl hello
(integer) 17672
127.0.0.1:6379> ttl hello
(integer) 12
127.0.0.1:6379> exists hello
(integer) 0
参数
command | explain |
---|---|
ex seconds | 过期时间 set key value ex seconds 等同 setex |
px milliseconds | 毫秒过期时间 set key value px milliseconds 等同 psetex |
nx | key不存在 才设置成功 set key value nx 等同 setnx |
xx | key已经存在才成功 |
127.0.0.1:6379> set t93 'hello world' ex 100 nx
OK
127.0.0.1:6379> get t93
"hello world"
127.0.0.1:6379> ttl t93
(integer) 85
ex px 后者覆盖前者
127.0.0.1:6379> set t94 't94' ex 100 px 30000
OK
127.0.0.1:6379> ttl t94
(integer) 25
127.0.0.1:6379> ttl t94
(integer) 22
127.0.0.1:6379> ttl t94
(integer) 19
127.0.0.1:6379> ttl t94
(integer) 14
127.0.0.1:6379> pttl t94
(integer) 8532
127.0.0.1:6379> pttl t94
(integer) 5710
127.0.0.1:6379> pttl t94
(integer) 4286
127.0.0.1:6379> pttl t94
(integer) 1078
127.0.0.1:6379> pttl t94
(integer) -2
127.0.0.1:6379> pttl t94
(integer) -2
127.0.0.1:6379> ttl t94
(integer) -2
incr 自增
127.0.0.1:6379> set count 1
OK
127.0.0.1:6379> get count
"1"
127.0.0.1:6379> incr count
(integer) 2
127.0.0.1:6379> incr count
(integer) 3
// 当key不纯在
127.0.0.1:6379> exists number
(integer) 0
127.0.0.1:6379> incr number
(integer) 1
127.0.0.1:6379> get number
"1"
//原key值不是数值
127.0.0.1:6379> set test 'txt'
OK
127.0.0.1:6379> incr test
(error) ERR value is not an integer or out of range
127.0.0.1:6379>
incrby 指定增加量
127.0.0.1:6379> set test 0
OK
127.0.0.1:6379> incrby test 5
(integer) 5
127.0.0.1:6379> get test
"5"
127.0.0.1:6379> incrby test -5
(integer) 0
127.0.0.1:6379> get test
"0"
//浮点数异常
127.0.0.1:6379> incrby test 1.1
(error) ERR value is not an integer or out of range
incrbyfloat 可以设置整数
127.0.0.1:6379> incrbyfloat test 1.1
"1.1"
127.0.0.1:6379> incrbyfloat test 1.1
"2.2"
127.0.0.1:6379> incrbyfloat test 1.1
"3.3"
127.0.0.1:6379> incrbyfloat test 1.1
"4.4"
127.0.0.1:6379> get test
"4.4"
//整数
127.0.0.1:6379> incrbyfloat test 7
"11.4"
decr
127.0.0.1:6379> set test 7
OK
127.0.0.1:6379> decr test
(integer) 6
decrby
127.0.0.1:6379> set test 19
OK
127.0.0.1:6379> decr test
(integer) 18
127.0.0.1:6379> decrby test 3
(integer) 15
127.0.0.1:6379> decrby test 3.3
(error) ERR value is not an integer or out of range
无decrbyfloat
append
127.0.0.1:6379> set test '1'
OK
127.0.0.1:6379> append test '23'
(integer) 3
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> exists notkey89983
(integer) 0
127.0.0.1:6379> append notkey89983 '_'
(integer) 1
127.0.0.1:6379> get notkey89983
"_"
Hash
List
127.0.0.1:6379> lpush t1 a b c
(integer) 3
127.0.0.1:6379>
## key不是string会error
127.0.0.1:6379> get t1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379>