Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make a folder-like hierarchy in redis cache?

So I'm using redis cache in my c# webapi and being able to implement a similar hierarchy would make my life much easier (something like this:

a-> key1
    b-> c ->key2
            key3
        d ->...

)

My other option is to make a tree like approach with keys where a would give me 2 other keys one for key and another for b and so one (but would be a mess)

like image 281
Ali_Nass Avatar asked Dec 24 '22 08:12

Ali_Nass


2 Answers

If you use redis commander to view your cache, you can use keys separated with colons,e.g, set1:subset:subset:key. Its not really a hierarchy but it displays like folders in the commander view.

like image 171
Jack Sukerman Avatar answered Feb 16 '23 00:02

Jack Sukerman


Redis supports multiple datatypes. For your case you can use a Hashes since a hash can have another nested hash in it.

Since Redis doesn't support nested data structure, you can store it this way by storing the inner hash reference in outer hash which will have difficulty while retrieving the data back. Else, you can create the hierarchical object structure as a JSON (Or, if you already have one) and store that serialized object in Redis.

See Storing nested objects in Redis

like image 40
Rahul Avatar answered Feb 15 '23 23:02

Rahul