How to store array of hashes in redis

后端 未结 2 808
遇见更好的自我
遇见更好的自我 2021-02-18 23:24

I want to store array of hashes in redis , what is best way to code it ?

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-19 00:07

    The only way AFAIK is to de-reference them. Say you have an array of 2 hashes like: {foo: 'bar', baz: 'qux'}.

    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
    

    Then you can retrieve them all by querying the set: SMEMBERS myarr and then call HGETALL 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.

提交回复
热议问题