Redis list of nested keys

Deadly 提交于 2021-02-08 07:29:17

问题


I have saved lists in following format in my Redis database.

key:inner-key1:inner-key2:inner-key3

For example my database looks like this:

A:B:X:val1

A:B:Y:val2

A:C:X:val3

A:C:Y:val4

How can I get inner keys for key B? I was trying to get it using KEYS A:B:*, but result of this are whole lines "A:B:X:val1" and "A:B:X:val2". All I need is to get only first inner key of "A:B" in format for example [X, Y].


回答1:


You can use Redis Hash to acheive the same:

Your Keys are

    A:B:X:val1
    A:B:Y:val2
    A:C:X:val3
    A:C:Y:val4

you can save your keys as

    HSET A:B  X val1
    HSET A:B  Y val2
    HSET A:C  X val1
    HSET A:C  Y val2

Now to get all keys for A:B you can do like

    HKEYS A:B           this will return [X Y]


来源:https://stackoverflow.com/questions/20271372/redis-list-of-nested-keys

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!