Redis使用bitmap统计后获取用户id

大家都知道使用使用redis的bitmap能占用很少的空间就能进行用户的日活统计等

但是这个时候我们想要获取今天有哪些用户登录了就没有现成的方法来获取了。

当然,我们可以通过计算把offect(偏移量)算出来。具体代码如下:

const ioredis = require("ioredis");
const redis = new ioredis({
  host: "",
  db: 0,
});

(async () => {
  //获取buffer数据
  const data = await redis.getBuffer("redisKey");
  const userList = [];
  data.forEach((item, index) => {
    userList.push(...getUserID(item, index));
  });
  console.log(userList);
  process.exit(0);
})();

function getUserID(data, index = 0) {
  const userList = [];
  const offect = index * 8 - 1;
  for (let i = 7; i >= 0; i--) {
    if ((data >> i) & 1) {
      userList.push(8 - i + offect);
    }
  }
  return userList;
}
原文地址:https://www.cnblogs.com/naocanzhishen/p/15637840.html