无限滚动页面的Redis分页策略
Redis pagination strategy for infinite scrolling page
TL;DR:以下三个选项中哪个选项对使用 Redis 进行分页最有效?
我正在实现一个网站,其中包含多个用户生成的帖子,这些帖子保存在关系数据库中,然后以 Hashes 的形式使用 site:{site_id}:post:{post_id}
等键复制到 Redis。
我想对 Redis 执行简单的分页查询,以实现延迟加载分页(即用户向下滚动,我们向服务器发送一个 Ajax 请求以请求下一批帖子)在 Pinterest 风格的界面中。
然后我在每次数据库更新时创建了一个 Set to keep track of published posts ids, with keys like site:{site_id}:posts
. I've chosen Sets because I don't want to have duplicated IDs in the collection and I can do it fastly with a simple SADD(无需检查 id 是否存在)。
嗯,由于 Sets 没有排序,我正在考虑我必须分页的选项的优缺点:
1) 使用 SSCAN 命令对我已经实现的集合进行分页
In this case, I could persist the returned Scan cursor in the user's
session, then send it back to server on next request (it doesn't seem
reliable with multiple users accessing and updating the database: at
some time the cursor would be invalid and return weird results -
unless there is some caveat that I'm missing).
2) 重构我的集合以使用 Lists or Sorted Sets 代替
Then I could paginate using LRANGE or ZRANGE. List seems to
be the most performant and natural option for my use case. It's
perfect for pagination and ordering by date, but I simply can't check
for a single item existence without looping all list. Sorted Sets
seems to join the advantages of both Sets and Lists, but consumes more
server resources.
3) 继续使用常规集并将页码存储为键的一部分
It would be something like site:{site_id}:{page_number}:posts
. It
was the recommended way before Scan commands were implemented.
那么,问题是:哪种方法最有效/最简单?是否还有其他未在此处列出的推荐选项?
"Best" 最好是主观的:)
我建议您使用第二种方法,但一定要使用排序集而不是列表。不仅对此类工作有意义(参见 ZRANGE
),与 LRANGE
-ing 列表相比,它们在复杂性方面也更有效。
TL;DR:以下三个选项中哪个选项对使用 Redis 进行分页最有效?
我正在实现一个网站,其中包含多个用户生成的帖子,这些帖子保存在关系数据库中,然后以 Hashes 的形式使用 site:{site_id}:post:{post_id}
等键复制到 Redis。
我想对 Redis 执行简单的分页查询,以实现延迟加载分页(即用户向下滚动,我们向服务器发送一个 Ajax 请求以请求下一批帖子)在 Pinterest 风格的界面中。
然后我在每次数据库更新时创建了一个 Set to keep track of published posts ids, with keys like site:{site_id}:posts
. I've chosen Sets because I don't want to have duplicated IDs in the collection and I can do it fastly with a simple SADD(无需检查 id 是否存在)。
嗯,由于 Sets 没有排序,我正在考虑我必须分页的选项的优缺点:
1) 使用 SSCAN 命令对我已经实现的集合进行分页
In this case, I could persist the returned Scan cursor in the user's session, then send it back to server on next request (it doesn't seem reliable with multiple users accessing and updating the database: at some time the cursor would be invalid and return weird results - unless there is some caveat that I'm missing).
2) 重构我的集合以使用 Lists or Sorted Sets 代替
Then I could paginate using LRANGE or ZRANGE. List seems to be the most performant and natural option for my use case. It's perfect for pagination and ordering by date, but I simply can't check for a single item existence without looping all list. Sorted Sets seems to join the advantages of both Sets and Lists, but consumes more server resources.
3) 继续使用常规集并将页码存储为键的一部分
It would be something like
site:{site_id}:{page_number}:posts
. It was the recommended way before Scan commands were implemented.
那么,问题是:哪种方法最有效/最简单?是否还有其他未在此处列出的推荐选项?
"Best" 最好是主观的:)
我建议您使用第二种方法,但一定要使用排序集而不是列表。不仅对此类工作有意义(参见 ZRANGE
),与 LRANGE
-ing 列表相比,它们在复杂性方面也更有效。