gpt4 book ai didi

node.js - 如何从 JavaScript 初始化 Redis 中的位图

转载 作者:IT王子 更新时间:2023-10-29 06:11:45 25 4
gpt4 key购买 nike

我需要在 Redis 中初始化一个大位图一组 4 * n 位都等于 1。如果我这样做:

var redis = require('redis');
var client = redis.createClient();
var n = 8;
var mask = '\\x';
for (var i = 0; i < n; ++i) {
mask += 'f';
}
client.set('mybitmap', mask, callback);

...mybitmap 被设置为 xffffffff 而不是所需的 \xffffffff

关于如何以我们不会在 Redis 中丢失它的方式转义 JavaScript 中的反斜杠有什么想法吗?

注意:我使用的是 node_redis客户。

最佳答案

似乎您不能使用 redis 客户端放置只有“1”位的位图。事实上,如果你像那样构建你的字符串:

var mask = ''; 
for (var i = 0; i < n; ++i) {
mask += String.fromCharCode(0xffff);
}

你会在 Redis 中得到类似的东西: 11101111 10111111 10111111 11101111 10111111 10111111 11101111 10111111 10111111 11101111 10111111

虽然我确定在 Javascript 中以下内容是正确的:String.fromCharCode(0xffff).toString().charCodeAt(0) === 0xffff

(因为这个 https://stackoverflow.com/a/3482698/667433,我有些怀疑),Node 的 Redis 客户端和 Redis 之间的某个地方丢失了一些位。

一个构建纯“1”位图的解决方案是将所有“0”放在上面并使用NOT:

var client = require('redis').createClient();

var n = 8;
var mask = new Array(n + 1).join(String.fromCharCode(0));
client.set('mybitmap', mask, function() {
client.bitop(['not', 'mybitmap', 'mybitmap'], displayBits);
});

// Just to display what Redis actually stored.
function displayBits() {
var res = '';
(function getBit(idx) {
if (idx === (n + 1) * 8) {
console.log(res);
client.quit();
return;
}

client.getbit(['mybitmap', idx], function(e, r) {
res += r;
if ((idx + 1) % 8 === 0) { res += '\n'; }
getBit(idx + 1);
});
})(0);
}

希望对你有帮助

关于node.js - 如何从 JavaScript 初始化 Redis 中的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26312728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com