Redis SETBIT令

1年前 (2024-04-27)
Redis SETBIT 令用于对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。位的设置或清除取决于 value,可以是 0 或者是 1 。

当 key 不存在时,自动生成一个新的字符串值。字符串会进行伸展以确保它可以将 value 保存在指定的偏移量上。当字符串值进行伸展时,空白位置以 0 填充。offset 参数必须大于或等于 0 ,小于 2^32 (bit 被限制在 512 MB 之内)。

提示:如果 offset 偏移量的值较大,计算机进行内存分配时可能会造成 Redis 服务器被阻塞。

可用版本

SETBIT 令可用版本:>= 2.2.0

语法

Redis SETBIT 令的基本语法如下:

setbit key offset value  

返回值

返回值指定了偏移量原来储存的位。

令演示

127.0.0.1:6379> SETBIT mybit 0 1

(integer) 0

127.0.0.1:6379> SETBIT mybit 1 0

(integer) 0

127.0.0.1:6379> SETBIT mybit 2 1

(integer) 0

127.0.0.1:6379> GETBIT mybit 200 #默认初始化为0

(integer) 0