当我将字符串值 1 和 7 存储到mykey"中时,redis 中究竟存储了什么?以及 getbit 在 redis 中是如何工作的?
有人试图循环这个值内的位吗?我知道 bitcount 会给我数字 2,但我也想从中获得确切的字符串值 1 和 7,这可能吗?
--
我做了一些实验,使用 erlang redis 客户端读取输出.
>setbit mykey 1 1erlang 输出:
<<>然后我删除这个条目:
>德尔我的钥匙我做同样的事情来偏移2 4 8,在这里你可以看到映射:
当offset为1时,输出<<;当 offset 为 2 时,输出为 <<" ">>;当offset为4时,输出为<;当 offset 为 8 时,输出为 <<0,128>;老实说,我现在更糊涂了.
或者有人可以解释这个"bitops.c"
-- 更新 ---
也许我应该提一下我想这样做的原因,以使问题更清楚.我们都知道使用位图来存储在线用户会很酷.我想要做的是从 redis 存储的内容中获取在线的确切用户 ID.
刚刚完成了一个快速版本来提取偏移量Redis.请随时改进它.
解决方案顾名思义,SETBIT 允许您执行位操作 - 即在给定的位置将给定的位设置为 0 或 1位偏移,对于给定的键.
重要的是要了解结果并不总是只包含可打印的字符.这就是Redis使用自定义函数sdscatrepr的原因 格式化 CLI 输出:
附加到 sds 字符串 "s" 一个转义的字符串表示形式,其中所有不可打印的字符(用 isprint() 测试)都变成了 " a...." 或 "形式的转义字符x".
话虽如此,让我们从一个简单的例子开始.如果考虑十六进制数 0x7F (= 127),其 8 位二进制表示为:
pos: 0 1 2 3 4 5 6 7位:0 1 1 1 1 1 1 1^ ^||最高有效位最低有效位您通常可以使用 SETBIT 来存储这个值,记住偏移量 0 是 MSB,偏移量 7 是 LSB:
redis>SETBIT myval 0 0(整数)0Redis>SETBIT myval 1 1(整数)0Redis>SETBIT myval 2 1(整数)0Redis>SETBIT 米瓦尔 3 1(整数)0Redis>SETBIT myval 4 1(整数)0Redis>SETBIT 米瓦尔 5 1(整数)0Redis>SETBIT 米瓦尔 6 1(整数)0Redis>SETBIT 米瓦尔 7 1(整数)0在以下情况下检查您的价值:
redis>获取 myval"x7f"现在多字节会发生什么?假设您要存储 0x52 (= 82),它对应于 ASCII 中的字符 R.8 位表示是 01010010 位位置 (8, 9, ..., 15) 因为我们希望它在第一个值之后立即存储:
redis>SETBIT myval 8 0(整数)0Redis>SETBIT 米瓦尔 9 1(整数)0Redis>SETBIT myval 10 0(整数)0Redis>SETBIT 米瓦尔 11 1(整数)0Redis>SETBIT myval 12 0(整数)0Redis>SETBIT myval 13 0(整数)0Redis>SETBIT 米瓦尔 14 1(整数)0Redis>SETBIT myval 15 0(整数)0你会得到:
redis>获取 myval"x7fR"这里Redis CLI能够表示可打印字符R.
当我将字符串值 1 和 7 存储到mykey"中时
它对应于 01000001 等于 65 和 0x41 十六进制.它对应于ASCII字符A.这样做:
redis>SETBIT mykey 1 1(整数)0Redis>SETBIT mykey 7 1(整数)0给出:
redis>得到我的钥匙一个"getbit 在 redis 中是如何工作的?
它只是返回给定位置的位值.这里:
redis>获取我的密钥 1(整数)1但位 0 尚未设置(默认为 0)因此:
redis>获取我的密钥 0(整数)0> setbit mykey 1 1 > setbit mykey 7 1When 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 1erlang output:
<<"@">>Then I delete this entry:
> del mykeyI 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 <<"">>; 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 " 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 LSBYou can typically use SETBIT to store this value, keeping in mind that offset 0 is MSB and offset 7 is LSB:
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) 0The 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 character R in ASCII. The 8-bit representation is 01010010 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) 0And 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 and 0x41 in hex. It corresponds to ASCII character A. So doing:
redis> SETBIT mykey 1 1 (integer) 0 redis> SETBIT mykey 7 1 (integer) 0Gives:
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) 1But bit 0 has not been set (it is 0 by default) thus:
redis> GETBIT mykey 0 (integer) 0
更多推荐
有人可以解释 redis setbit 命令吗?
发布评论