|
1 | 1 | package com.example.data.redis;
|
2 | 2 |
|
3 | 3 | import java.util.Arrays;
|
| 4 | +import java.util.Map; |
4 | 5 |
|
5 | 6 | import org.springframework.beans.factory.annotation.Autowired;
|
6 | 7 | import org.springframework.boot.CommandLineRunner;
|
7 | 8 | import org.springframework.data.redis.core.BoundValueOperations;
|
8 | 9 | import org.springframework.data.redis.core.RedisCallback;
|
9 | 10 | import org.springframework.data.redis.core.RedisTemplate;
|
10 | 11 | import org.springframework.data.redis.core.StringRedisTemplate;
|
| 12 | +import org.springframework.data.redis.hash.Jackson2HashMapper; |
11 | 13 | import org.springframework.data.redis.listener.ChannelTopic;
|
12 | 14 | import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
13 | 15 | import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
|
@@ -35,6 +37,8 @@ public void run(String... args) throws Exception {
|
35 | 37 | templateOperations();
|
36 | 38 | keyBoundOperations();
|
37 | 39 | redisBackedSet();
|
| 40 | + hashMapper(HashStructure.DEFAULT); |
| 41 | + hashMapper(HashStructure.FLAT); |
38 | 42 | jsonSerializer();
|
39 | 43 | pubSub();
|
40 | 44 |
|
@@ -67,6 +71,16 @@ private void redisBackedSet() {
|
67 | 71 | System.out.printf("redis set: %s%n", redisSet.iterator().next());
|
68 | 72 | }
|
69 | 73 |
|
| 74 | + private void hashMapper(HashStructure structure) { |
| 75 | + |
| 76 | + Jackson2HashMapper hashMapper = new Jackson2HashMapper(HashStructure.FLAT.equals(structure)); |
| 77 | + template.opsForHash().putAll("hash", hashMapper.toHash(new Person("hashed-fn", "hashed-ln"))); |
| 78 | + |
| 79 | + Map<String, Object> hashedEntry = template.<String, Object>opsForHash().entries("hash"); |
| 80 | + System.out.printf("hash-mapper-%s-raw: %s%n", structure, hashedEntry); |
| 81 | + System.out.printf("hash-mapper-%s-mapped: %s%n", structure, hashMapper.fromHash(hashedEntry)); |
| 82 | + } |
| 83 | + |
70 | 84 | private void keyBoundOperations() {
|
71 | 85 | BoundValueOperations<String, String> keyBoundOps = template.boundValueOps("bound-key");
|
72 | 86 | keyBoundOps.set("OK");
|
@@ -107,4 +121,15 @@ private void pubSub() throws InterruptedException {
|
107 | 121 | System.out.printf("pub/sub: %s%n", messageHandler.receivedMessages());
|
108 | 122 | }
|
109 | 123 |
|
| 124 | + enum HashStructure { |
| 125 | + |
| 126 | + DEFAULT, FLAT; |
| 127 | + |
| 128 | + @Override |
| 129 | + public String toString() { |
| 130 | + return name().toLowerCase(); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + |
110 | 135 | }
|
0 commit comments