hash - 以 O(1) 时间复杂度获取 Redis 中的最后 20 个哈希项?

标签 hash redis

遵循 Redis 建议,我必须尽可能使用哈希:

Small hashes are encoded in a very small space, so you should try representing your data using hashes every time it is possible. For instance if you have objects representing users in a web application, instead of using different keys for name, surname, email, password, use a single hash with all the required fields.

我将拥有数百万个项目,我正在尝试将帖子存储为散列:

hmset post:1 title "hello"

hmset post:2 title "hi"

hmset post:3 title "g'day"

现在,我知道我可以像创建哈希一样简单地检索它:

hgetall post:1

问题是我想检索多个,可能是一行中的 20 个。

我尝试了以下命令,但不起作用(当然没有 3 个点):

hgetall post:1 post:2 post:3 post:4...

出现以下错误:

(error) ERR wrong number of arguments for 'hgetall' command

我可以做这个客户端:

hgetall post:1

然后

hgetall post:2

等等......但这将是对网络资源的巨大浪费,并且是潜在的性能瓶颈,因为系统平均每秒应该能够处理至少 5000 个用户/秒。

如果我所做的失败,完成此任务的最佳方法是什么?有没有一种方法可以一次性发送此命令而不是依次执行?是否有可以对哈希值进行lrange类型的操作?

最佳答案

您的方向是正确的 - 对这 20 个帖子中的每一个帖子都进行了一个接一个的 HGETALL。然而,为了节省网络往返次数,请考虑使用 Redis 的管道,它本质上允许您对请求进行分组/批量处理。或者,您可以通过实现您自己的 HGETALL 版本来获得类似的结果,该版本使用服务器端 Lua 脚本接受多个键名称。

关于hash - 以 O(1) 时间复杂度获取 Redis 中的最后 20 个哈希项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057741/

相关文章:

bash - 为什么我不能用expect脚本来启动redis?

redis-cli 命令没有响应

php - 在 Laravel 中配置 Redis 时遇到的问题

spring - 将 SSE-Emitter 对象保存到 MongoDB/Redis 中,从数据库中获取它并通过它发送事件

docker - 为什么摘要不同取决于注册表?

php - 无法使用 PHP base64_encode 获取 Twitter 的 OAuth 签名

python - 比较在 python 中作为输入传递的 2 个 sha512 哈希值

php - 如何在数据库中存储为哈希创建的盐?

python - 为什么 Python 的无穷大哈希有 π 的数字?

ruby-on-rails-4 - Redis 用于 Rails 中的数据存储(作业)和片段缓存