I've seen questions on how to create an array of hash objects in Redis. But I want another arrangement: a Hash object, whose fields are Strings and values are Sets.
Should I create the sets separately from the Hash? If so, how would I reference that set? Through the variable name?
Can I manipulate them from a higher level, ie: sadd Hash_name.field_name append_this_value_to_set
?
Furthermore, how would I read those inner Sets?
Redis doesn't provide nested data structures, therefore a Hash field's value can't be a Set and can only be a String.
One way of doing something similar to what the OP is trying to achieve is to use regular Sets and store their key names in the Hash's values. Dereferencing these, however, requires performing the additional operations in code.
For example, you may create a set named user
which contains all user ids in the system, and a set named asset
which contains all assets in the company.
> sadd user 1000 1001 1002 1003
(integer) 4
> type user
set
> sadd asset 20190001 20190002 20190003
(integer) 3
> type asset
set
Then use hashes to describe each user and asset record.
> hmset asset:20190001 desc laptop price 2000
OK
> hmset asset:20190002 desc pc price 1800
OK
> hmset asset:20190003 desc laptop price 2100
OK
> hmset asset:20190004 desc laptop price 2000
OK
A user could hold more than one asset.
> hmset user:1000 username Samuel birthyear 1980 asset 20190001:20190002
OK
> hmset user:1001 username David birthyear 1984 asset -1
OK
> hmset user:1002 username Marry birthyear 1987 asset 20190004
OK
> hmset user:1003 username Joe birthyear 1977 asset 20190003
Use your code to implement the logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With