SETBIT
SETBIT key offset value
- Available since:
- 2.2.0
- Time complexity:
- O(1)
- ACL categories:
-
@write
,@bitmap
,@slow
,
设置或清除存储在key中的字符串值在offset处的位。
该位根据value设置或清除,value可以是0或1。
当key不存在时,会创建一个新的字符串值。 字符串会被扩展以确保它可以在offset处保存一个位。 offset参数必须大于或等于0,并且小于2^32(这将位图限制在512MB)。 当key处的字符串被扩展时,新增的位会被设置为0。
警告:当设置最后一个可能的位(偏移量等于2^32 -1)并且存储在键中的字符串值尚未持有字符串值,或者持有一个小的字符串值时,Redis需要分配所有中间内存,这可能会阻塞服务器一段时间。
在2010年的MacBook Pro上,设置位号2^32 -1(512MB分配)需要约300毫秒,设置位号2^30 -1(128MB分配)需要约80毫秒,设置位号2^28 -1(32MB分配)需要约30毫秒,设置位号2^26 -1(8MB分配)需要约8毫秒。
请注意,一旦完成第一次分配,后续对相同键的SETBIT
调用将不会有分配开销。
示例
模式:访问整个位图
在某些情况下,您需要一次性设置单个位图的所有位,例如在将其初始化为默认的非零值时。可以通过多次调用SETBIT
命令来实现这一点,每次调用设置一个需要设置的位。然而,作为一种优化,您可以使用单个SET
命令来设置整个位图。
位图不是一种实际的数据类型,而是一组基于字符串类型的位操作(更多信息请参考数据类型介绍页面的位图部分)。这意味着位图可以与字符串命令一起使用,最重要的是可以与SET
和GET
一起使用。
因为Redis的字符串是二进制安全的,位图可以简单地编码为字节流。字符串的第一个字节对应位图的偏移量0..7,第二个字节对应8..15范围,依此类推。
例如,设置几位后,获取位图的字符串值将如下所示:
> SETBIT bitmapsarestrings 2 1
> SETBIT bitmapsarestrings 3 1
> SETBIT bitmapsarestrings 5 1
> SETBIT bitmapsarestrings 10 1
> SETBIT bitmapsarestrings 11 1
> SETBIT bitmapsarestrings 14 1
> GET bitmapsarestrings
"42"
通过获取位图的字符串表示,客户端可以通过在其本地编程语言中使用本地位操作提取位值来解析响应的字节。对称地,也可以通过执行位到字节的编码并在客户端调用SET
与结果字符串来设置整个位图。
模式:设置多个位
SETBIT
擅长设置单个位,当需要设置多个位时,可以多次调用。为了优化此操作,您可以使用可变参数 BITFIELD
命令和类型为 u1
的字段来替换多个 SETBIT
调用。
例如,上面的例子可以被替换为:
> BITFIELD bitsinabitmap SET u1 2 1 SET u1 3 1 SET u1 5 1 SET u1 10 1 SET u1 11 1 SET u1 14 1
高级模式:访问位图范围
也可以使用GETRANGE
和SETRANGE
字符串命令来高效地访问位图中的一系列位偏移。以下是一个可以在EVAL
命令中运行的典型Redis Lua脚本示例实现:
--[[
Sets a bitmap range
Bitmaps are stored as Strings in Redis. A range spans one or more bytes,
so we can call [`SETRANGE`](/docs/latest/commands/setrange/) when entire bytes need to be set instead of flipping
individual bits. Also, to avoid multiple internal memory allocations in
Redis, we traverse in reverse.
Expected input:
KEYS[1] - bitfield key
ARGV[1] - start offset (0-based, inclusive)
ARGV[2] - end offset (same, should be bigger than start, no error checking)
ARGV[3] - value (should be 0 or 1, no error checking)
]]--
-- A helper function to stringify a binary string to semi-binary format
local function tobits(str)
local r = ''
for i = 1, string.len(str) do
local c = string.byte(str, i)
local b = ' '
for j = 0, 7 do
b = tostring(bit.band(c, 1)) .. b
c = bit.rshift(c, 1)
end
r = r .. b
end
return r
end
-- Main
local k = KEYS[1]
local s, e, v = tonumber(ARGV[1]), tonumber(ARGV[2]), tonumber(ARGV[3])
-- First treat the dangling bits in the last byte
local ms, me = s % 8, (e + 1) % 8
if me > 0 then
local t = math.max(e - me + 1, s)
for i = e, t, -1 do
redis.call('SETBIT', k, i, v)
end
e = t
end
-- Then the danglings in the first byte
if ms > 0 then
local t = math.min(s - ms + 7, e)
for i = s, t, 1 do
redis.call('SETBIT', k, i, v)
end
s = t + 1
end
-- Set a range accordingly, if at all
local rs, re = s / 8, (e + 1) / 8
local rl = re - rs
if rl > 0 then
local b = '\255'
if 0 == v then
b = '\0'
end
redis.call('SETRANGE', k, rs, string.rep(b, rl))
end
注意:从位图中获取一系列位偏移的实现留给读者作为练习。