Redis Setbit 命令用于对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。
语法
redis Setbit 命令基本语法如下:
redis 127.0.0.1:6379> Setbit KEY_NAME OFFSET
可用版本
>= 2.2.0
返回值
指定偏移量原来储存的位。
实例
redis> SETBIT bit 10086 1
(integer) 0

redis> GETBIT bit 10086
(integer) 1

redis> GETBIT bit 100 # bit 默认被初始化为 0
(integer) 0

Redis 字符串(string)

Redis Mget 命令返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil 。
语法
redis Mget 命令基本语法如下:
redis 127.0.0.1:6379> MGET KEY1 KEY2 .. KEYN
可用版本
>= 1.0.0
返回值
一个包含所有给定 key 的值的列表。
实例
redis 127.0.0.1:6379> SET key1 “hello”
OK
redis 127.0.0.1:6379> SET key2 “world”
OK
redis 127.0.0.1:6379> MGET key1 key2 someOtherKey
1) “Hello”
2) “World”
3) (nil)

Redis 字符串(string)

Redis Getbit 命令用于对 key 所储存的字符串值,获取指定偏移量上的位(bit)。
语法
redis Getbit 命令基本语法如下:
redis 127.0.0.1:6379> GETBIT KEY_NAME OFFSET
可用版本
>= 2.2.0
返回值
字符串值指定偏移量上的位(bit)。
当偏移量 OFFSET 比字符串值的长度大,或者 key 不存在时,返回 0 。
实例
# 对不存在的 key 或者不存在的 offset 进行 GETBIT, 返回 0

redis> EXISTS bit
(integer) 0

redis> GETBIT bit 10086
(integer) 0
# 对已存在的 offset 进行 GETBIT

redis> SETBIT bit 10086 1
(integer) 0

redis> GETBIT bit 10086
(integer) 1

Redis 字符串(string)

Redis Getset 命令用于设置指定 key 的值,并返回 key 旧的值。
语法
redis Getset 命令基本语法如下:
redis 127.0.0.1:6379> GETSET KEY_NAME VALUE
可用版本
>= 1.0.0
返回值
返回给定 key 的旧值。 当 key 没有旧值时,即 key 不存在时,返回 nil 。
当 key 存在但不是字符串类型时,返回一个错误。
实例
首先,设置 mykey 的值并截取字符串。
redis 127.0.0.1:6379> GETSET mynewkey “This is my test key”
(nil)
redis 127.0.0.1:6379> GETSET mynewkey “This is my new value to test getset”
“This is my test key”

Redis 字符串(string)

Redis Getrange 命令用于获取存储在指定 key 中字符串的子字符串。字符串的截取范围由 start 和 end 两个偏移量决定(包括 start 和 end 在内)。
语法
redis Getrange 命令基本语法如下:
redis 127.0.0.1:6379> GETRANGE KEY_NAME start end
可用版本
>= 2.4.0
返回值
截取得到的子字符串。
实例
首先,设置 mykey 的值并截取字符串。
redis 127.0.0.1:6379> SET mykey “This is my test key”
OK
redis 127.0.0.1:6379> GETRANGE mykey 0 3
“This”
redis 127.0.0.1:6379> GETRANGE mykey 0 -1
“This is my test key”

Redis 字符串(string)