Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis hset vs set for storing an object

Tags:

redis

I have a json object, such as a user object with about 10 key/value pairs that I store currently in sets for various groupings.

I stringify the object then parse it on the way out. Updating is sometimes a pain, because I have to get the data then reset it.

I'm thinking of switching over to hash sets and use hmset and hgetall.

Are there any downsides to using hashes? Performance or otherwise, or would this be a better solution?

like image 411
dzm Avatar asked Feb 10 '23 01:02

dzm


1 Answers

If the user object is flat ie no nested objects then hashes are perfect. Getting a single field with hget is O(1). Getting the entire object with hgetall is O(n) where n is the number of fields so in your case it will be super fast as well.

Just keep in mind that hash field values are stored as strings. So you might have to do some type conversion when you get the data back from redis.

like image 106
Barış Uşaklı Avatar answered May 26 '23 10:05

Barış Uşaklı