1. Overview
Redis's Hash is a string-type field and value mapping table, which is especially suitable for storing objects. Let's take a look at the usage scenarios of this data type and how to use it in Spring.
2. Usage scenarios
We know that Hash stores a mapping table of fields and values, so it is more suitable for storing objects, so we can use the data type of Hash to store user information.
@Test public void testHash(){ HashOperations hashOperations = redisTemplate.opsForHash(); hashOperations.put("user","firstName","zhang"); hashOperations.put("user","lastName","san"); hashOperations.put("user","age",12); System.out.println(hashOperations.values("user")); }
The output is:
[zhang, 12, san]
3. Summary
From the above output we can see that the Hash data structure is indeed more suitable for storing objects.