Reputation: 487
I'm new to Redis and databases in general. I'm confused about when I should use the GET/SET commands and when I should create a hash table and use the HGET/HSET commands.
Let's say I want to keep track of the prices of various products in a shop.
Using GET and SET:
SET pencil 3
SET eraser 4
SET calculator 60
GET pencil
GET eraser
GET calculator
Using HGET and HSET:
HSET mystore pencil 3
HSET mystore eraser 4
HSET mystore calculator 60
HGET mystore pencil
HGET mystore eraser
HGET mystore calculator
For this shop example, which method is preferred? What's the difference between using the GET/SET commands and using the HGET/HSET commands? What are some of their use cases?
Thanks
Upvotes: 9
Views: 8572
Reputation: 1092
Hashes are used to store objects in Redis and GET
/SET
are used to store a single string (or int).
From your description, in your case there is nothing different with HSET
and GET
/SET
. If your Redis database is used only for mystore
object (or something similar), you can just use GET
/SET
with the key.
And if there are multiple similar mystore
objects, I recommend to convert to hash since it will be much easier to organize the key-values.
For performance, key-value string and hash are both implemented using dict (hash will use ziplist if data set is small). So it will always be O(1) time complexity.
Upvotes: 11