在 Redis 中存储大量相似项目的随机列表
Redis 2.0.3
我需要在 Redis 中存储大量的项目列表。每个项目都是一个短字符串(少于 256 个字符)。
我需要在列表上执行两项操作:
添加许多(几千到一百万)相同的项目。 (一天几次)
从列表中随机删除一项。没有必要有“公平”的随机性。任何“足够好”的方法都可以。 (每秒最多数百次)
我没有足够的 RAM 来将列表中的所有项目一一存储。
我认为我需要批量存储物品,名称和柜台。 (将有多达数千个不同的项目,更像是数百个。)
但我不确定如何有效地组织它。
有什么提示吗?
Redis 2.0.3
I need to store a huge list of items in Redis. Each item is a short string (less than 256 characters).
I need to do two operations on the list:
Add many (several thousands to a million) identical items. (Several times a day)
Remove one random item from the list. It is not necessary to have "fair" random. Any "good enough" approach will do. (Up to several hundred times a second)
I do not have enough RAM to store all items in a list one by one.
I think that I need to store items in batches, name and a counter. (There will be up to several thousands distinct items, more like several hundreds.)
But I'm not sure how to organize this effectively.
Any hints?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,既然没有人愿意帮助我,这里有一个伪代码的“愚蠢”解决方案。
获取随机元素:
插入新元素
如果存在,请提出更好的解决方案,我仍在摸索Redis,并且可能会错过一些明显的东西。
我怀疑
insert_items()
中的LOCK
/UNLOCK
可能是多余的,可以用MULTI
/EXEC
,但我认为maybe_get_next_item()
中的LOCK
/UNLOCK
需要它才能正常工作(我确实这样做)不知道如何替换为MULTI
/EXEC
)...Well, since nobody is up to help me, here is a "dumb" solution, in pseudocode.
Get random element:
Insert new elements
Please do suggest a better solution if it exists, I'm still groking Redis, and may miss something obvious.
I suspect that
LOCK
/UNLOCK
ininsert_items()
may be superfluous and can be replaced withMULTI
/EXEC
, but I think that it is needed forLOCK
/UNLOCK
inmaybe_get_next_item()
to work properly (which I do not know how to replace withMULTI
/EXEC
)...