set/get
语法: set key value [EX seconds] [PX milliseconds] [NX|XX] get key # 示例 127.0.0.1:6379> set site dbase.cc ex 20 OK 127.0.0.1:6379> set site dbase.cc px 20000 OK 127.0.0.1:6379> pttl site (integer) 19980 127.0.0.1:6379> set site google.com nx (nil) 127.0.0.1:6379> set site google.com xx OK 127.0.0.1:6379> get site "google.com" # nx: 表示key不存在时,执行操作 # xx: 表示key存在时,执行操作 |
mset/mget
语法: mset key value [key value ...] mget key [key ...] # 示例 127.0.0.1:6379> mset a aman b bold c control d diamond OK 127.0.0.1:6379> mget a b c d 1) "aman" 2) "bold" 3) "control" 4) "diamond" |
setrange
语法: setrange key offset value # 作用:把字符串的offset偏移字节,改成value # 如果偏移量>字符长度, 该字符自动补0x00 # 示例 127.0.0.1:6379> set word hello OK 127.0.0.1:6379> setrange word 2 ?? (integer) 5 127.0.0.1:6379> get word "he??o" 127.0.0.1:6379> set word hello OK 127.0.0.1:6379> setrange word 6 ! (integer) 7 127.0.0.1:6379> get word "hello\x00!" |
append
语法: append key value # 示例 127.0.0.1:6379> set word hello OK 127.0.0.1:6379> append word " world" (integer) 11 127.0.0.1:6379> get word "hello world" |
getrange
语法: getrange key start end # 作用: 是获取字符串中 [start, stop]范围的值 # 对于字符串的下标,左数从0开始,右数从-1开始 # 示例 127.0.0.1:6379> set area chinese OK 127.0.0.1:6379> getrange area 0 4 "chine" 127.0.0.1:6379> set status working OK 127.0.0.1:6379> getrange status 0 -4 "work" 127.0.0.1:6379> getrange status 6 3 "" 注意: 1: start>=length, 则返回空字符串 2: stop>=length,则截取至字符结尾 3: 如果start 所处位置在stop右边, 返回空字符串 |
getset
语法: getset key value # 作用: 获取并返回旧值,设置新值 # 示例 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" 127.0.0.1:6379> getset status working "wakeup" |
incr/decr
语法: incr key decr key # 作用: 指定的key的值加1,并返回加1后的值 # 示例 127.0.0.1:6379> set age 29 OK 127.0.0.1:6379> incr age (integer) 30 127.0.0.1:6379> incr age (integer) 31 127.0.0.1:6379> decr age (integer) 30 注意: 1: 不存在的key当成0,再incr操作 2: 范围为64有符号 |
incrby/decrby
语法: incrby key increment decrby key decrement # 示例 127.0.0.1:6379> incrby age 10 (integer) 39 127.0.0.1:6379> decrby age 10 (integer) 29 |
incrbyfloat
语法: incrbyfloat key increment # 示例 127.0.0.1:6379> incrbyfloat age 2.5 "31.5" |
setbit/getbit
语法: setbit key offset value getbit key offset # 作用: 设置/获取值的二进制位上的值(从左,从0编号) # setbig返回: 该位上的旧值 # 示例1 任意大写字母变小写 127.0.0.1:6379> set char A OK 127.0.0.1:6379> getbit char 2 (integer) 0 127.0.0.1:6379> setbit char 2 1 (integer) 0 127.0.0.1:6379> getbit char 2 (integer) 1 127.0.0.1:6379> get char "a" # 示例2 任意大写字母变小写 127.0.0.1:6379> set char a OK 127.0.0.1:6379> setbit char 2 0 (integer) 1 127.0.0.1:6379> get char "A" 示例原理: A 65 0100 0001 a 97 0110 0001 # offset最大值是2^32-1 127.0.0.1:6379> set char B OK 127.0.0.1:6379> setbit char 4294967296 1 (error) ERR bit offset is not an integer or out of range # 最大值是2^32 - 1,超出范围会报错 127.0.0.1:6379> setbit char 4294967295 1 (integer) 0 # 推出最大的的字符串为512M 2^32/8 = 2^32 * 2^-3 = 2^29 K->M 2^29 / 1024 / 1024 = 512M |
bitop
语法: bitop operation destkey key [key ...] # 对key1,key2..keyN作operation,并将结果保存到 destkey 上。 # operation 可以是 AND 、 OR 、 NOT 、 XOR # 示例 127.0.0.1:6379> get lower (nil) 127.0.0.1:6379> setbit lower 2 1 (integer) 0 127.0.0.1:6379> get lower " " 127.0.0.1:6379> set char Q OK 127.0.0.1:6379> bitop or char char lower (integer) 1 127.0.0.1:6379> get char "q" 注意: 对于NOT操作, key不能多个 |