redis - 了解 Redis 中排序集中缺失数字的最佳方法是什么?

标签 redis

如果我在 Redis 中有一个只包含整数值的 SortedSet,比方说从 1 到 200 万,是否有一种高效的方法可以快速找出 SortedSet 中缺失的整数值?或者,除 SortedSet 之外的其他数据结构是否更适合此类查找?

最佳答案

你可以试试位运算:setbit .您可以将整数作为位位置/偏移量,而不是将整数存储到 Redis 中。一句话,用一个位图来存储一个整数的存在。

当您尝试存储一个整数时,请使用setbit 命令设置相应的位。当您想检查丢失的整数时,获取值并检查位值为 0 的位置。

Store integers

// store 2
setbit key 2 1
// store 123
setbit key 123 1
// store N
setbit key N 1

Find missing integers

// get value
get key
// Check each bit of the returned value.
// If the bit value is 0, the position/offset of this bit is a missing integer

关于redis - 了解 Redis 中排序集中缺失数字的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937996/

相关文章:

java - Redis 中的范围查询 - Spring Data Redis

node.js - 访问不同 node.js 文件之间的 session 变量

go - 在这种情况下使用 redis 池的正确方法

go - 将结构传递给 redigo Send 函数会破坏它并且数据丢失

c# - StackExchange.Redis 发送结构

amazon-web-services - 以最少的停机时间将 Redis 迁移到 AWS Elasticache

redis-trib.rb 不再可用但 redis-cli --cluster create 抛出无法识别的选项错误

python - 如果 session.modified : 不是 session,如何理解 flask session 片段

redis - 在 getServerSideProps 中使用 redis 会导致错误 net.isIP is not a function

c# - ServiceStack.Redis 搜索缓存