admin管理员组文章数量:1314292
I am trying to store a number as a value in Redis key. For example, I want to store a value of 4. and I don't want it to be stored as "4". Why I need this? Because when I retrieve this value back, I will be doing some bitwise op on it. If it stores as "4" (instead of 4), the value actually stored in Redis seems to be 52 (that is... 00110100 instead of 00000100).
You might wonder, why I don't use Redis bitops. The reason is I have to store an array of many bits. I don't want to be doing redis bitops in a loop. I just want to locally create an equivalent array and upload it by calling set mand.
In Javascript, I tried doing
redis.set(key, 4)
obviously it didn't work. Then I tried
redis.set(key, "\x04")
This works. But how do I store an array of bytes by converting to this format? What am I missing here?
I am trying to store a number as a value in Redis key. For example, I want to store a value of 4. and I don't want it to be stored as "4". Why I need this? Because when I retrieve this value back, I will be doing some bitwise op on it. If it stores as "4" (instead of 4), the value actually stored in Redis seems to be 52 (that is... 00110100 instead of 00000100).
You might wonder, why I don't use Redis bitops. The reason is I have to store an array of many bits. I don't want to be doing redis bitops in a loop. I just want to locally create an equivalent array and upload it by calling set mand.
In Javascript, I tried doing
redis.set(key, 4)
obviously it didn't work. Then I tried
redis.set(key, "\x04")
This works. But how do I store an array of bytes by converting to this format? What am I missing here?
Share Improve this question asked Jun 21, 2016 at 19:27 Mopparthy RavindranathMopparthy Ravindranath 3,3288 gold badges48 silver badges88 bronze badges1 Answer
Reset to default 7Internally, if all of the values of a data type are numeric, then the data is stored by its numeric representation. Otherwise, the data is stored as a string. You cannot force Redis to use a specific representation method for a single data point, as far as I know, and anyways - you'll always get the value as a string. You'll need to parse the value yourself and convert it into an integer, float, etc.
本文标签: javascriptRedis how to store a number instead of stringStack Overflow
版权声明:本文标题:javascript - Redis how to store a number instead of string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741867502a2401997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论