Previously, we focused on simple key-value pairs in Redis. Now, we will explore storing maps, updating fields, storing objects as values, and nested structures.
1. Basic Redis Map Setup
Redis supports storing hashes (maps), which allows grouping multiple key-value pairs under a single Redis key. In Redisson, we use RMap
.
import org.redisson.api.RMap; import org.redisson.api.RedissonClient; public class RedisMapExample { public static void main(String[] args) { RedissonClient redisson = RedissonConfig.getClient(); // Create a simple user map RMap<String, Object> userMap = redisson.getMap("user:1"); userMap.put("name", "Alice"); userMap.put("age", 25); userMap.put("city", "Atlanta"); System.out.println("User map created: " + userMap); } }
Verify in Redis CLI:
HGETALL user:1
Output:
1) "name" 2) "Alice" 3) "age" 4) "25" 5) "city" 6) "Atlanta"
2. Storing an Existing Java Map
If you already have a Java map, you can store it directly in Redis using RMap.putAll()
:
Map<String, Object> user2 = new HashMap<>(); user2.put("name", "Jake"); user2.put("age", 30); user2.put("city", "Miami"); RMap<String, Object> redisUser2Map = redisson.getMap("user:2"); redisUser2Map.putAll(user2);
Verify:
HGETALL user:2
Output:
1) "name" 2) "Jake" 3) "age" 4) "30" 5) "city" 6) "Miami"
3. Updating Individual Fields
Individual fields can be updated without overwriting the entire map:
redisUser2Map.put("city", "Orlando");
Check in Redis CLI:
HGET user:2 city
Output:
"Orlando"
4. Storing Complex Objects as Map Values
Redis maps are not limited to storing strings. You can store entire objects as values. This is useful for nested or structured data like user objects, student records, or any Java POJO.
import org.redisson.api.RMap; import java.util.Arrays; import java.util.List; RMap<Integer, Student> usersMap = redisson.getMap("users"); // Create student objects Student student1 = new Student("Alice", 25, "Atlanta", Arrays.asList(80, 85, 90)); Student student2 = new Student("Jake", 30, "Miami", Arrays.asList(70, 75, 80)); // Store students in the map usersMap.put(1, student1); usersMap.put(2, student2);
Here:
- Key =
Integer
(student ID) - Value =
Student
object - Student object can contain fields and nested lists (e.g., marks)
5. Retrieving Objects from the Map
You can retrieve the whole object using its key:
Student s = usersMap.get(1); System.out.println("Student 1: " + s);
This allows you to treat Redis like a simple in-memory database.
6. Nested Lists Inside Objects
Objects can have nested collections. For example, the Student
object may have a list of marks:
List<Integer> marks = Arrays.asList(80, 85, 90); Student student = new Student("Alice", 25, "Atlanta", marks); usersMap.put(1, student);
Redis will store this as a serialized object (binary or JSON if configured).
7. Using JSON Format for Complex Objects
To store objects in JSON format instead of binary, you can use Redisson’s JSON codec:
RMap<Integer, Student> usersMapJson = redisson.getMap( "users_json", new JsonJacksonCodec(Student.class) ); usersMapJson.put(1, student1); usersMapJson.put(2, student2);
This makes it easier to inspect the data in Redis CLI and integrate with other systems.
8. Summary and Best Practices
RMap
supports storing multiple fields under a single Redis key.- Use
putAll()
to store existing Java maps efficiently. - Individual fields can be updated independently.
- Objects can be stored as map values, including nested lists and complex structures.
- JSON serialization is recommended if you need human-readable data or cross-language access.
- Redis maps can act as an in-memory database, allowing quick storage and retrieval of structured objects.