在 Redis 中保留大型列表(例如 10K 个项目)的最佳方法是什么,我还想通过键有效地检索项目。
似乎 Redis 没有与 Java 的 OrderedHashMap 等效的数据结构,它完成了这一点,所以可能有必要维护一个集合和一个列表并确保它们保持同步。
在 Redis 中保留大型列表(例如 10K 个项目)的最佳方法是什么,我还想通过键有效地检索项目。
似乎 Redis 没有与 Java 的 OrderedHashMap 等效的数据结构,它完成了这一点,所以可能有必要维护一个集合和一个列表并确保它们保持同步。
Use a sorted set;
Add some bookmarks; use current time for score
to sort chronologically:
> zadd bookmarks 123 "bk1"
> zadd bookmarks 456 "bk2"
> zadd bookmarks 789 "bk3"
> zadd bookmarks 999 "bk4"
To get a bookmark, you need the index first:
> zrank bookmarks "bk3"
> "3"
...then pull the bookmark by index:
> zrevrange bookmarks 3 3
> "bk3"
If you don't want to use timestamps, you can sort bookmarks lexicographically using "1" for score:
> zadd bookmarks 1 "link_xyz"
> zadd bookmarks 1 "link_abc"
> zadd bookmarks 1 "link_foo"
> zrange bookmarks 0 -1
1) "link_abc"
2) "link_foo"
3) "link_xyz"
The index lookup is O(log(n))
, add to that O(log(n)+1)
to pull a single member by index; better than O(n)
for lists.
Also, if you add the same bookmark twice, redis automatically replaces the previous member, so you avoid duplicates.
Hope it helps,