I currently have a scenario where we are using REDIS to store string field-value
pairs within a hashed set HSET
.
The original reasoning behind using hashed sets instead of just sets is ease of retrieving the records using HSCAN
inside a GUI Search Bar as opposed to just SCAN
because it's easier to get the length of a hash to use in the COUNT
field.
I read in the Redis documentation that both GET
and HGET
commands execute with O(1) time complexity, but a member of my team thinks that if I store all the values inside a single key then it basically returns the entire hash during HGET
instead of the singular field-value
that I need.
So for a made up but similar example:
users
. field:value
pairs of username:email
If when I execute hget users coolguy
, is the entire hash getting returned or just the email for user coolguy
?
First of all, HSET is not a hash set, it creates a hash table. The mechanism behind the hash table and set (which is indeed a hash set) in redis is the same, the difference is mainly that the hash table has values.
To answer your question:
If when I execute hget users coolguy, is the entire hash getting returned or just the email for user coolguy?
Just the email for that user. You can also use HMGET to get the emails of multiple users at once. It's O(1) for each user you fetch, or O(n) for n users.