问题描述
我想在 redis 中存储哈希数组,最好的编码方式是什么?
I want to store array of hashes in redis , what is best way to code it ?
推荐答案
AFAIK 的唯一方法是取消引用它们.假设您有一个包含 2 个哈希的数组,例如:{foo: 'bar', baz: 'qux'}
.
The only way AFAIK is to de-reference them. Say you have an array of 2 hashes like: {foo: 'bar', baz: 'qux'}
.
您可以单独存储它们,然后创建一个引用它们的 SET:
You'd store them separately, and then create a SET that references them all:
HMSET myarr:0 foo bar baz qux
SADD myarr myarr:0
HMSET myarr:1 foo bar baz qux
SADD myarr myarr:1
然后你可以通过查询集合来检索它们:SMEMBERS myarr
然后在所有返回的键上调用 HGETALL
来重建你的原始哈希数组.
Then you can retrieve them all by querying the set: SMEMBERS myarr
and then call HGETALL <key>
on all the returned keys to rebuild your original array of hashes.
我希望这是有道理的.如果你能找到更聪明的方法,我会很高兴听到的.
I hope this makes sense. And if you find a smarter way I'd be happy to hear it.
这篇关于如何在redis中存储哈希数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!