0x001 設定值並獲取
命令格式:SET key | GET key
127.0.0.1:6379> SET name helloworld
OK
127.0.0.1:6379> GET name
"helloworld"
127.0.0.1:6379> GET name2
(nil)
0x002 獲取指定區間的值
命令格式: GRANGE name start end
127.0.0.1:6379> GETRANGE name 2 6
"llowo"
# 如果`key`不存在,則返回空
127.0.0.1:6379> GETRANGE name2 1 10
""
0X003 設定新值,並返回舊的值
命令格式: GETSET key value
127.0.0.1:6379> GETSET name helloworld2
"helloworld"
127.0.0.1:6379> get name
"helloworld2"
# 如果值不存在,則返回nil,但是設定的新值是成功的
127.0.0.1:6379> GETSET name2 helloworld2
(nil)
127.0.0.1:6379> GET name2
"helloworld2"
0x004 獲取多個KEY
命令格式: MGET key [key,key,key]
127.0.0.1:6379> set name helloworld
OK
127.0.0.1:6379> set name2 helloworld2
OK
127.0.0.1:6379> MGET name name2 name3
1) "helloworld"
2) "helloworld2"
3) (nil)
0x005 不存在的時候才設定key的值
命令格式: SETNX key
127.0.0.1:6379> SET name helloworld
OK
127.0.0.1:6379> SETNX name helloworld2
(integer) 0
127.0.0.1:6379> GET name
"helloworld"
0x006 獲取字串長度
命令格式: STRLEN key
127.0.0.1:6379> STRLEN name
(integer) 10
0x007 設定多個鍵值對
命令格式: MSET key value key value
127.0.0.1:6379> MSET name helloworld name2 helloworld2
OK
127.0.0.1:6379> MGET name name2
1) "helloworld"
2) "helloworld2"
0x008 value是數字的情況下,增加value的值
命令格式:INCR key | INCRBY key
127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> INCR num
(integer) 2
127.0.0.1:6379> GET num
"2"
# 如果value不是數字,將會報錯
127.0.0.1:6379> INCR name
(error) ERR value is not an integer or out of range
# 如果key不存在,則自動建立
127.0.0.1:6379> INCR num2
(integer) 1
# 增加指定量
127.0.0.1:6379> INCRBY num 5
(integer) 7
127.0.0.1:6379> GET num
"7"
0x009 value是數字的情況下,減少value的值
命令格式:DECR key | DECRBY key
127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> DECR num
(integer) 0
127.0.0.1:6379> DECR num
(integer) -1
127.0.0.1:6379> DECRBY num 4
(integer) -5
127.0.0.1:6379> DECRBY num2
(error) ERR wrong number of arguments for `decrby` command
127.0.0.1:6379> DECR num2
(integer) 0
127.0.0.1:6379> DECRBY num3 2
(integer) -2
0x010 追加value
命令格式:APPEND key | DECRBY key
127.0.0.1:6379> set name helloworld
OK
127.0.0.1:6379> APPEND name 2
(integer) 11
127.0.0.1:6379> GET name
"helloworld2"
# 如果key不存在將報錯
127.0.0.1:6379> APPEND name2
(error) ERR wrong number of arguments for `append` command