set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , 9秒有效 注: 如果ex,px同时写,以后面的有效期为准 如 set a 1 ex 100 px 9000, 实际有效期是9000毫秒 nx: 表示key不存在时,执行操作 xx: 表示key存在时,执行操作 127.0.0.1:6379> set site www.so.com OK 127.0.0.1:6379> set site www.baidu.com nx (nil) 127.0.0.1:6379> get site "www.so.com" 127.0.0.1:6379> set site www.google.com xx OK 127.0.0.1:6379> get site "www.google.com"
mset multi set , 一次性设置多个键值 例: mset key1 v1 key2 v2 .... get key 作用:获取key的值 mget key1 key2 ..keyn 作用:获取多个key的值 127.0.0.1:6379> mset a aman b bold c controller OK 127.0.0.1:6379> keys * 1) "a" 2) "c" 3) "b" 127.0.0.1:6379> get a "aman" 127.0.0.1:6379> get b "bold" 127.0.0.1:6379> get c "controller" 127.0.0.1:6379> mget a b c 1) "aman" 2) "bold" 3) "controller"
setrange key offset value 作用:把字符串的offset偏移字节,改成value 127.0.0.1:6379> set word hello OK 127.0.0.1:6379> get word "hello"
0 | 1 | 2 | 3 | 4 |
h | e | l | l | o |
127.0.0.1:6379> setrange word 2 ?? (integer) 5 127.0.0.1:6379> get word "he??o"
注意: 如果偏移量>字符长度, 该字符自动补0x00
127.0.0.1:6379> set word hello
OK
127.0.0.1:6379> get word
"hello"
127.0.0.1:6379> setrange word 6 !
(integer) 7
127.0.0.1:6379> get word
"hello\x00!"
append key value 作用: 把value追加到key的原值上 127.0.0.1:6379> get word "hello\x00!" 127.0.0.1:6379> append word @@ (integer) 9 127.0.0.1:6379> get word "hello\x00!@@"
getrange key start stop 作用: 是获取字符串中 [start, stop]范围的值 注意: 对于字符串的下标,左数从0开始,右数从-1开始 127.0.0.1:6379> set area chinese OK 127.0.0.1:6379> getrange area 1 4 "hine"
-7 | -6 | -5 | -4 | -3 | -2 | -1 |
w | o | r | k | i | n | g |
127.0.0.1:6379> set status working OK 127.0.0.1:6379> get status "working" 127.0.0.1:6379> getrange status 0 -4 "work"
注意:
1: start>=length, 则返回空字符串
2: stop>=length,则截取至字符结尾
3: 如果start 所处位置在stop右边, 返回空字符串
getset key newvalue 作用: 获取并返回旧值,设置新值 127.0.0.1:6379> set status sleep OK 127.0.0.1:6379> getset status wakeup "sleep" 127.0.0.1:6379> get status "wakeup"
incr key 作用: 指定的key的值加1,并返回加1后的值 注意: 1:不存在的key当成0,再incr操作 2: 范围为64有符号 decr key 作用: 指定的key的值减1,并返回减1后的值 incrby key number decrby key number 127.0.0.1:6379> set age 29 OK 127.0.0.1:6379> incr age (integer) 30 127.0.0.1:6379> decr age (integer) 29 127.0.0.1:6379> incrby age 5 (integer) 34 127.0.0.1:6379> incrby age 5 (integer) 39 127.0.0.1:6379> decrby age 10 (integer) 29
setbit key offset value 设置offset对应二进制位上的值 返回: 该位上的旧值可实现大小写转换
A | 65 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 1 |
a | 97 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 |
127.0.0.1:6379> set char A OK 127.0.0.1:6379> get char "A" 127.0.0.1:6379> setbit char 2 1 (integer) 0 127.0.0.1:6379> get char "a" 127.0.0.1:6379> set char B OK 127.0.0.1:6379> get char "B" 127.0.0.1:6379> setbit char 2 1 (integer) 0 127.0.0.1:6379> get char "b"