在redis中创建中型到大型列表/集合/zset/散列的最有效方法是什么?
使用 redis,有许多命令可以检索整个数据结构(LRANGE 用于列表,SMEMBERS 用于集合, ZRANGE 用于排序集,以及 HGETALL 用于哈希)。
只有哈希有一个方法(HMSET)使用单个命令插入多个项目。
我见过的所有示例都显示一次仅将一个项目添加到列表中(通过 RPUSH 或 LPUSH)或一组(通过 SADD/ZADD)。
我想要解决的更具体的问题是创建包含数据库 id 的列表和排序集,这些列表对于每个用户来说都是唯一的,并且包含几百到几千个 id。
它们通常是从数据库查询中收集的,在内存中进行一些处理,然后存储在 Redis 中,以便分页(列表)或执行基于集合的操作以检索子集(集合和排序集合)。
目前,我正在迭代列表并为每个元素调用适当的添加方法。这样做的缺点是需要通过线路发出多个请求并每次都重复密钥。
redis> RPUSH employee:ids 1000
(integer) 1
redis> RPUSH employee:ids 1001
(integer) 2
redis> RPUSH employee:ids 1002
(integer) 3
redis> RPUSH employee:ids 1003
(integer) 4
redis> del employee:ids
(integer) 1
我认为将 transaction 与 MULTI 和 EXEC 可以帮助将其变成单个请求,但这对重复密钥没有帮助。
redis> MULTI
OK
redis> RPUSH employee:ids 1000
QUEUED
redis> RPUSH employee:ids 1001
QUEUED
redis> RPUSH employee:ids 1002
QUEUED
redis> RPUSH employee:ids 1003
QUEUED
redis> RPUSH employee:ids 1004
QUEUED
redis> EXEC
1. (integer) 1
2. (integer) 2
3. (integer) 3
4. (integer) 4
5. (integer) 5
我是否缺少一些东西可以让我在单个命令中将元素添加到列表/集合中,或者不需要每次都重复该键?
如果重要的话,我还会使用 jedis 客户端库(或者如果我可以从 JVM 使用另一个库,那就更好了)。
Using redis, there are a number of commands to retrieve entire data structures (LRANGE for lists, SMEMBERS for sets, ZRANGE for sorted sets, and HGETALL for hashes).
Only the hash has a method (HMSET) to insert multiple items with a single command.
All of the examples I've seen have shown just adding a single item at a time to a list (through RPUSH or LPUSH) or a set (through SADD/ZADD).
The more specific problem that I want to solve is to create lists and sorted sets containing database ids, these lists are unique to each user and contain a few hundred to a few thousand ids.
They are normally gathered from a database query, massaged a little bit in memory and then stored in redis for either paginating through (lists) or doing set based operations on to retrieve subsets (sets and sorted sets).
Currently, I'm iterating through the list and calling the appropriate add method for each element. This has the drawbacks of making multiple requests over the wire and repeating the key every time.
redis> RPUSH employee:ids 1000
(integer) 1
redis> RPUSH employee:ids 1001
(integer) 2
redis> RPUSH employee:ids 1002
(integer) 3
redis> RPUSH employee:ids 1003
(integer) 4
redis> del employee:ids
(integer) 1
I'm thinking that using a transaction with MULTI and EXEC could help to turn it into a single request, but that wouldn't help with the repeated key.
redis> MULTI
OK
redis> RPUSH employee:ids 1000
QUEUED
redis> RPUSH employee:ids 1001
QUEUED
redis> RPUSH employee:ids 1002
QUEUED
redis> RPUSH employee:ids 1003
QUEUED
redis> RPUSH employee:ids 1004
QUEUED
redis> EXEC
1. (integer) 1
2. (integer) 2
3. (integer) 3
4. (integer) 4
5. (integer) 5
Is there something that I'm missing that would let me add elements to lists/sets in a single command, or without repeating the key every time?
I'm also using the jedis client library if that matters (or if there's another library that I can use from the JVM that'd be better).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置交易在这里没有帮助。事务的目的是确保命令同时执行,这样服务器上就永远不会有一半的列表。他们仍然一次发送一个。
多个命令真的会导致性能问题吗? 1000 个项目实际上并没有那么多,只要您不执行诸如为每个项目打开新连接之类的操作,就不应该出现任何延迟问题。
Setting up a transaction won't help here. The purpose of transactions is to ensure the commands get executed at the same time so you never have half a list on the server. They still get sent one at a time.
Are the multiple commands really causing performance issues? 1000 items isn't actually that many, and as long as you aren't doing something like opening a new connection for each item there shouldn't be any latency issues.