如果我在 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/