You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
set key value [ex 秒数]|[px 毫秒数] [nx]|[xx]
# 设置kv时也可以设置有效期和判断key是否存在# ex和px不要同时写,否则以后面有效期为准# nx表示key不存在时执行操作# xx表示key存在时执行操作# 示例
127.0.0.1:6379>set name zhangsan
OK
127.0.0.1:6379>set name zhangsan ex 100
OK
127.0.0.1:6379> ttl name
(integer) 78
127.0.0.1:6379>set name lisi nx
(nil)
127.0.0.1:6379> get name
"zhangsan"
mset 一次性输入多个kv
mset key1 value1 key2 value2......
# 一次性输入多个key-value# 示例
127.0.0.1:6379> mset x 1 y 2 z 3
OK
127.0.0.1:6379> keys *
1) "y"
2) "z"
3) "x"
setrange 修改偏移字节值为valuev
setrange key offset value
# 把字符串的偏移字节改为value# 如果偏移量大于字符长度,中间字符自动补0x00# 示例
127.0.0.1:6379>set name zhangsan
OK
127.0.0.1:6379> setrange name 5 ***
(integer) 8
127.0.0.1:6379> get name
"zhang***"
append 在key的值后面追加字符串
append key value
# 在key的值后面追加value字符串# 示例
127.0.0.1:6379>set name zhangsan
OK
127.0.0.1:6379> append name "@126.com"
(integer) 16
127.0.0.1:6379> get name
"zhangsan@126.com"
getrange 获取key值的部分内容
getrange key start stop
# 获取key值的一部分内容# start表示起始位置# stop表示结束位置,可以为为负数,表示从最后数起# start>length 空字符串# stop>length 截取到结尾# 示例
127.0.0.1:6379>set title "hello world"
OK
127.0.0.1:6379> getrange title 6 11
"world"
127.0.0.1:6379> getrange title 0 -7
"hello"
getset 设置新值返回旧值
getset key newvalue
# 设置新值,并返回旧值# 示例
127.0.0.1:6379>set login on
OK
127.0.0.1:6379> get login
"on"
127.0.0.1:6379> getset login off
"on"
127.0.0.1:6379> get login
"off"
incr/decr 指定key的值加/减1
incr/decr key
# 指定key的值加/减1,返回结果# key不存在时,自动创建并加减1# key的值为字符串时无效# 示例
127.0.0.1:6379>set num 100
OK
127.0.0.1:6379> incr num
(integer) 101
127.0.0.1:6379> decr num
(integer) 100
incrby/decrby 指定key的值加/减number
incrby/decrby key number
# 指定key的值加减number大小# 示例
127.0.0.1:6379>set num 100
OK
127.0.0.1:6379> incrby num 50
(integer) 150
127.0.0.1:6379> decrby num 100
(integer) 50
incrbyfloat 指定key的值加浮点数
incrbyfloat key floatnumber
# 指定key的值加浮点数# 示例
127.0.0.1:6379>set num 10
OK
127.0.0.1:6379> incrbyfloat num 0.5
"10.5"
127.0.0.1:6379> incrbyfloat num -1.5
"9"
setbit 设置二进制位上的值
setbit key offset value
# 设置offset对应二进制位上的值# 返回改位的旧值# 如果offset过大则会在中间填充0# offset最大为2^32-1,即512M# 示例
127.0.0.1:6379>set letter A
OK
127.0.0.1:6379> setbit letter 2 1
(integer) 0
127.0.0.1:6379> get letter
"a"# 把0100 0001(65)改为0110 0001(97)即把大写A改为了小写a
getbit 获取二进制位上的值
getbit key offset
# 获取二进制offset对应位的值# 示例
127.0.0.1:6379>set letter A
OK
127.0.0.1:6379> getbit letter 0
(integer) 0
127.0.0.1:6379> getbit letter 1
(integer) 1
127.0.0.1:6379> getbit letter 7
(integer) 1
bitop 对多个key逻辑操作
bitop operation destkey key1 [key2 ......]
# 对key1 key2 keyN做operation,并把结果保存到destkey 上# operation有AND、OR、NOT、XOR# 示例
127.0.0.1:6379> setbit lower 2 1
(integer) 0
127.0.0.1:6379> setbit upper 2 0
(integer) 0
127.0.0.1:6379>set letter A
OK
127.0.0.1:6379> bitop or letter letter lower
(integer) 1
127.0.0.1:6379> get letter
"a"