|
1 | 1 | package com.ipman.springboot.redisson.sample.examples; |
2 | 2 |
|
3 | 3 | import lombok.SneakyThrows; |
4 | | -import org.redisson.api.RAtomicLong; |
5 | | -import org.redisson.api.RList; |
6 | | -import org.redisson.api.RLock; |
7 | | -import org.redisson.api.RedissonClient; |
| 4 | +import org.redisson.api.*; |
8 | 5 | import org.springframework.stereotype.Component; |
9 | 6 |
|
10 | 7 | import javax.annotation.Resource; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
11 | 10 |
|
12 | 11 | /** |
13 | 12 | * Created by ipipman on 2021/1/28. |
@@ -87,4 +86,28 @@ public void list() { |
87 | 86 | System.out.println("Is list contains name 'ipman': " + contains); |
88 | 87 | list.forEach(System.out::println); |
89 | 88 | } |
| 89 | + |
| 90 | + /** |
| 91 | + * 基于 Redis 的 Redisson 的分布式映射结构的 RMap |
| 92 | + * Java对象实现了 java.util.concurrent.ConcurrentMap 接口和 java.util.Map 接口 |
| 93 | + * 与HashMap不同的是,RMap保持了元素的插入顺序,该对象的最大容量是 4294967295 个 |
| 94 | + */ |
| 95 | + @SneakyThrows |
| 96 | + public void map() { |
| 97 | + RMap<String, String> map = redissonClient.getMap("map"); |
| 98 | + String prevVal = map.put("man", "2"); |
| 99 | + // key 空闲时写入 |
| 100 | + String currentVal = map.putIfAbsent("ipman", "3"); |
| 101 | + // 在 Map 头部写入key |
| 102 | + map.fastPut("ip", "1"); |
| 103 | + printMap(map); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + public static void printMap(Map<String, String> map) { |
| 108 | + Set<Map.Entry<String, String>> ms = map.entrySet(); |
| 109 | + for (Map.Entry<String, String> entry : ms) { |
| 110 | + System.out.println("Map key:" + entry.getKey() + " val:" + entry.getValue()); |
| 111 | + } |
| 112 | + } |
90 | 113 | } |
0 commit comments