有人可以解释的Redis setbit命令? [英] Can someone explain redis setbit command?
问题描述
> setbit的myKey 1 1
> setbit的myKey 7 1
在我店字符串值1和7成的myKey,什么确切地存储在Redis的?
以及如何GETBIT内redis的作品?
有谁尝试循环这个值里面的位?我知道位计数会给我2号,但我也想从它那里得到确切的字符串值1和7,这可能吗?
-
我使用Redis的二郎客户端读取输出做了一些实验。
> setbit的myKey 1 1
二郎输出:
<<@>>
然后我删除此日志:
>德尔的myKey
我做同样的事情,以抵消2 4 8,在这里你可以看到映射:
当偏移量是1时,输出为<&下,@>取代;
当偏移量为2,则输出为<&下;>取代;
当偏移为4,则输出为<&下;\\ B>取代;
当偏移量是8时,输出为<&所述; 0128>取代;
老实说,我现在更糊涂了。
或者有人能解释这个bitops.c
- 更新---
也许我应该提到我为什么要这样做是为了使问题更清楚的原因。我们都知道这将是很酷的使用位图来存储在线用户。我所要做的是让用户究竟谁的ID在网上从Redis的存储内容。
刚刚完成了快速版本从提取偏移Redis的。请随时改进它。
正如其名称所暗示的, SETBIT
,您可以执行位操作 - 一个给定的位分别设置为0或1,在给定的位偏移量,对于一个给定的键
重要的是要明白的是,结果并不总是只包括可打印字符。这就是为什么Redis的使用自定义函数 sdscatrepr
格式化输出CLI:
附加到SDS字符串s的转义字符串重新presentation那里所有的非打印字符(isprint判断测试())都变成了逃逸的形式\\ n \\ r \\一... 。或\\ x的。
块引用>话虽这么说,让我们先从一个简单的例子。如果考虑十六进制数
0x7F的
(= 127)在8位的二进制重新presentation是:POS:0 1 2 3 4 5 6 7
位:0 1 1 1 1 1 1 1
^^
| |
MSB LSB您通常可以使用
SETBIT
来存储这个值,牢记偏移0
是MSB
和偏移7LSB
:Redis的> SETBIT 0设为myVal 0
(整数)0
Redis的> SETBIT 1设为myVal 1
(整数)0
Redis的> SETBIT 2设为myVal 1
(整数)0
Redis的> SETBIT 3设为myVal 1
(整数)0
Redis的> SETBIT 4设为myVal 1
(整数)0
Redis的> SETBIT 5设为myVal 1
(整数)0
Redis的> SETBIT 6设为myVal 1
(整数)0
Redis的> SETBIT 7设为myVal 1
(整数)0在得到你的价值,如果检查:
Redis的> GET设为myVal
\\ 0x7F部分现在什么多字节发生什么呢?比方说,你要存储
0×52
(= 82),它对应于ASCII字符研究
。 8位再presentation是01010010
与位位置(8,9,...,15)
因为我们希望它右边的第一个值后保存:Redis的> SETBIT 8设为myVal 0
(整数)0
Redis的> SETBIT 9设为myVal 1
(整数)0
Redis的> SETBIT 10设为myVal 0
(整数)0
Redis的> SETBIT 11设为myVal 1
(整数)0
Redis的> SETBIT 12设为myVal 0
(整数)0
Redis的> SETBIT 13设为myVal 0
(整数)0
Redis的> SETBIT 14设为myVal 1
(整数)0
Redis的> SETBIT 15设为myVal 0
(整数)0,你会得到:
Redis的> GET设为myVal
\\ x7fR下面Redis的CLI能够重新present的可打印字符
研究
。
在我店字符串值1和7成的myKey
块引用>它对应于
01000001
这是十六进制等于65和×41
。它对应的ASCII字符A
。这样做的:Redis的> SETBIT的myKey 1 1
(整数)0
Redis的> SETBIT的myKey 7 1
(整数)0给出:
Redis的> GET的myKey
一个
在里面GETBIT的Redis是如何工作的?
块引用>它简单地返回位在给定位置的值。这里:
Redis的> GETBIT的myKey 1
(整数)1但是第0位尚未设置(它的默认值为0),从而
Redis的> GETBIT的myKey 0
(整数)0> setbit mykey 1 1 > setbit mykey 7 1
When I store string value 1 and 7 into "mykey", what was exactly stored in redis? And how the getbit works inside redis?
Does anyone try to loop the bit inside this value? I know bitcount will give me number 2, but I also want to get the exact string value 1 and 7 from it, is it possible?
--
I doing some experiment by using erlang redis client to read the output.
> setbit mykey 1 1
erlang output:
<<"@">>
Then I delete this entry:
> del mykey
I do the same thing to offset 2 4 8, here you can see the mapping:
When offset is 1, the output is <<"@">>; When offset is 2, the output is <<" ">>; When offset is 4, the output is <<"\b">>; When offset is 8, the output is <<0,128>>;
Honestly, I am more confused now.
Or someone can explain this "bitops.c"
-- updates ---
Maybe I should mention the reason why I want to do this to make the question more clear. We all know it will be cool to use bitmap to store online users. What I am trying to do is get the exactly user id who is online from what redis stored.
Just finished a quick version to extract offsets from redis. Please feel free to improve it.
解决方案As its name implies,
SETBIT
allows you to perform bit operations - namely set a given bit to 0 or 1, at a given bit offset, for a given key.What is important to understand is that the result not always includes only printable characters. This is why Redis uses a custom function
sdscatrepr
to format the CLI output:Append to the sds string "s" an escaped string representation where all the non-printable characters (tested with isprint()) are turned into escapes in the form "\n\r\a...." or "\x".
That being said let's start with a simple example. If you consider the hex number
0x7F
(= 127) its binary representation on 8-bit is:pos: 0 1 2 3 4 5 6 7 bit: 0 1 1 1 1 1 1 1 ^ ^ | | MSB LSB
You can typically use
SETBIT
to store this value, keeping in mind that offset0
isMSB
and offset 7 isLSB
:redis> SETBIT myval 0 0 (integer) 0 redis> SETBIT myval 1 1 (integer) 0 redis> SETBIT myval 2 1 (integer) 0 redis> SETBIT myval 3 1 (integer) 0 redis> SETBIT myval 4 1 (integer) 0 redis> SETBIT myval 5 1 (integer) 0 redis> SETBIT myval 6 1 (integer) 0 redis> SETBIT myval 7 1 (integer) 0
The get your value to inspect if:
redis> GET myval "\x7f"
Now what happens with multi bytes? Let's say you want to store
0x52
(= 82) which corresponds to characterR
in ASCII. The 8-bit representation is01010010
with bit positions(8, 9, ..., 15)
since we want it to be stored right after the first value:redis> SETBIT myval 8 0 (integer) 0 redis> SETBIT myval 9 1 (integer) 0 redis> SETBIT myval 10 0 (integer) 0 redis> SETBIT myval 11 1 (integer) 0 redis> SETBIT myval 12 0 (integer) 0 redis> SETBIT myval 13 0 (integer) 0 redis> SETBIT myval 14 1 (integer) 0 redis> SETBIT myval 15 0 (integer) 0
And you get:
redis> GET myval "\x7fR"
Here Redis CLI is able to represent the printable character
R
.When I store string value 1 and 7 into "mykey"
It corresponds to
01000001
which is equal to 65 and0x41
in hex. It corresponds to ASCII characterA
. So doing:redis> SETBIT mykey 1 1 (integer) 0 redis> SETBIT mykey 7 1 (integer) 0
Gives:
redis> GET mykey "A"
how the getbit works inside redis?
It simply returns the value of the bit at the given position. Here:
redis> GETBIT mykey 1 (integer) 1
But bit 0 has not been set (it is 0 by default) thus:
redis> GETBIT mykey 0 (integer) 0
这篇关于有人可以解释的Redis setbit命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!