问题描述
我是 Redis 的新手,想就我的用例从专家那里获得建议。我有一个事件对象,它具有 start_time_of_event 和 end_time_of_event 作为两个属性(对象中有更多的属性)。我有数百万个这样的事件,需要将它们存储在 Redis 中,以便我可以在运行时查询它们。我希望使用时间戳作为键和事件对象作为值。在查询时,我需要获取 start_time_of_event =1 周后的事件列表。不确定 Redis 中的 ZRANGE、LRANGE 或任何其他数据结构是否支持使用多个(复杂)键。关于使用 Redis 实现上述用例的最佳方法有什么建议吗?
非常感谢。
解决方法
您应该将事件存储在两个 Redis ZSET 中,一组分数应为 start_time_of_event
,另一组分数应为 end_time_of_event
让我们分别称它们为 start_events
和 end_events
。
添加:
ZADD start_events start_time event
ZADD end_events end_time event
搜索
-- randomly generate a destination id,search the events
-- and store in that based on the start time
ZRANGESTORE random-start-time-dst start_events 0 current_time
-- randomly generate a destination id for end-time search,post filter
-- store results in that destination
ZRANGESTORE random-end-time-dst end_events current_time+7.weeks -1
-- Take the intersection of these two sets
ZINTER INT_MAX random-start-time-dst random-end-time-dst
-- Delete the newly created collections to free up the memory
DEL random-start-time-dst
DEL random-end-time-dst
,
另一种选择是使用 zeeSQL 将这些信息存储在二级索引(SQL 表)中。
一旦这些数据在表中,您就可以使用标准 SQL 查询它们。
这只是一个简单的 SQL 查询,不需要维护 Redis 结构。