Introduction
Hashes and strings are two ways to store data in Redis. Choosing between them helps you organize and access your data easily.
For string: SET key value GET key For hash: HSET key field value [field value ...] HGET key field HGETALL key
SET user:1 "John Doe"
HSET user:1 name "John Doe" age 30
HGET user:1 nameGET user:1HSET user:100 name "Alice" age 28 city "Paris" HGETALL user:100 GET user:100
hash over a string to store data?age to 30 in a hash named user:1?HSET command sets a field in a hash key.SET is for strings, HMSET is deprecated but similar to HSET, SETFIELD is invalid.HSET user:2 name "Alice" age 25 HSET user:2 city "Paris" GET user:2
GET user:2 command?HSET creates a hash key user:2. GET works only on string keys.GET on a hash key causes a type error in Redis.user:2 is a hash, not a string. -> Option DHSET user:3 name "Bob" age 40 SET user:3 "simple string"
user:3 key type conflicts; delete key before SET. -> Option Aname, age, and email in Redis. You also want to efficiently update individual fields without rewriting the whole object. Which approach is best and why?Pass the test and this pattern moves unknown