pagination - 无限滚动页面的Redis分页策略

标签 pagination redis language-agnostic lazy-loading

TL;DR:以下三个选项中哪一个对使用 Redis 进行分页最有效?

我正在实现一个包含多个用户生成的帖子的网站,这些帖子保存在关系数据库中,然后以 Hashes 的形式复制到 Redis使用 site:{site_id}:post:{post_id} 这样的键。

我想对 Redis 执行简单的分页查询,以在 Pinterest 风格的界面中实现延迟加载分页(即用户向下滚动,我们向服务器发送 Ajax 请求以请求下一批帖子) .

然后我创建了一个 Set使用 site:{site_id}:posts 等键跟踪已发布的帖子 ID。我选择 Sets 是因为我不想在集合中有重复的 ID,我可以使用简单的 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) 重构我的集合以使用 ListsSorted 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) 继续使用常规集并将页码存储为 key 的一部分

It would be something like site:{site_id}:{page_number}:posts. It was the recommended way before Scan commands were implemented.

那么,问题是:哪种方法最有效/最简单?是否还有其他未在此处列出的推荐选项?

最佳答案

“最佳”是主观的:)

我建议您使用第二种方法,但一定要使用排序集而不是列表。这不仅对此类工作有意义(请参阅 ZRANGE),而且与 LRANGE-ing 列表相比,它们在复杂性方面也更高效。

关于pagination - 无限滚动页面的Redis分页策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076783/

相关文章:

php - Codeigniter 分页使用页码重复内容

angular - 将分页添加到表格 Angular 2

amazon-web-services - 如何访问 AWS ElastiCache 上的 Redis 日志

c# - 如何从 C# 代码中使用 Redis 缓存中的 HSET 命令?

unit-testing - 是否应该避免服务对象的动态依赖?

algorithm - 如何计算两个字节数组之间的距离?

ruby-on-rails - Rails分页重复项

php - Laravel 7:MariaDB与Redis结合使用,但是Redis在大型对象上的运行速度较慢

language-agnostic - 何时使用私有(private)成员 - 何时作为函数/方法参数传递

javascript - 在 codeigniter 的一页上进行多页分页的最佳方法