Skip to content

Commit c54b1f7

Browse files
committed
完善cacheManager操作集和测试用例
1 parent 49ca81a commit c54b1f7

File tree

3 files changed

+198
-24
lines changed

3 files changed

+198
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package iot.technology.mqtt.server;
22

33
import iot.technology.mqtt.server.domain.Book;
4-
import iot.technology.mqtt.storage.session.cache.RedissonClientService;
4+
import iot.technology.mqtt.storage.session.cache.CacheManager;
55
import lombok.extern.slf4j.Slf4j;
66
import org.junit.Test;
77
import org.junit.runner.RunWith;
88
import org.springframework.boot.test.context.SpringBootTest;
99
import org.springframework.test.context.junit4.SpringRunner;
1010

1111
import javax.annotation.Resource;
12+
import java.util.Arrays;
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.Set;
1216

1317
/**
1418
* @author mushuwei
@@ -19,15 +23,39 @@
1923
public class MQTTRedissonApplicationTests {
2024

2125
@Resource
22-
private RedissonClientService clientService;
26+
private CacheManager cacheManager;
2327

2428
@Test
2529
public void redisStringOperations() {
2630
Book book = new Book().setName("Harry Potter").setAuthor("J.K.Rowling").setPrice(120.00);
27-
clientService.put("harry-potter", book, null);
28-
Book bookCache = (Book) clientService.get("harry-potter");
31+
cacheManager.putStringCache("harry-potter", book, null);
32+
Book bookCache = (Book) cacheManager.getStringCache("harry-potter");
2933
log.info("book:{}", bookCache);
34+
}
3035

36+
@Test
37+
public void redisHashOperations() {
38+
cacheManager.putHashCache("books", "java", "think in java");
39+
Map<String, Object> maps = new HashMap<>();
40+
maps.put("golang", "concurrency in go");
41+
maps.put("python", "python cookbook");
42+
cacheManager.putAllHashCache("books", maps);
43+
String javaBookName = (String) cacheManager.getHashCache("books", "java");
44+
Map<String, Object> cacheMap = cacheManager.getAllHashCache("books");
45+
log.info("java bookName:{}", javaBookName);
46+
log.info("books cacheMap:{}", cacheMap);
47+
}
48+
49+
@Test
50+
public void redisSetOperations() {
51+
cacheManager.addSetCache("book", "python");
52+
cacheManager.addAllSetCache("book", Arrays.asList("java", "golang"));
53+
Set<Object> setCache = cacheManager.getAllSetCache("book");
54+
Boolean javaExist = cacheManager.existsSetCache("book", "java");
55+
Boolean rustExist = cacheManager.existsSetCache("book", "rust");
56+
log.info("book:{}", setCache);
57+
log.info("java is exist: {}", javaExist);
58+
log.info("rust is exist: {}", rustExist);
3159
}
3260

3361
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,130 @@
11
package iot.technology.mqtt.storage.session.cache;
22

3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.Set;
6+
37
/**
48
* @author mushuwei
59
*/
610
public interface CacheManager {
711

8-
Boolean delete(String key);
12+
//******string字符串操作 start
13+
14+
/**
15+
* 删除string字符串
16+
*
17+
* @param key 键
18+
* @return true/false
19+
*/
20+
Boolean deleteStringCache(String key);
21+
22+
/**
23+
* 增加string/object类型 键值对
24+
*
25+
* @param key 键
26+
* @param value 值
27+
* @param expireTime 失效时间
28+
* @return
29+
*/
30+
Boolean putStringCache(String key, Object value, Integer expireTime);
31+
32+
/**
33+
* 通过键获取对象值
34+
*
35+
* @param key 键
36+
* @return 对象值
37+
*/
38+
Object getStringCache(String key);
39+
40+
/**
41+
* 检查是否存在某个键的键值对
42+
*
43+
* @param key 键
44+
* @return true/false
45+
*/
46+
Boolean existsStringCache(String key);
47+
48+
//******string字符串操作 end
49+
50+
51+
//******hash(字典) 操作 start
52+
53+
/**
54+
* 获取字典值
55+
*
56+
* @param key 键
57+
* @param mapKey 字典键
58+
* @return
59+
*/
60+
Object getHashCache(String key, String mapKey);
61+
62+
/**
63+
* 增加字典属性
64+
*
65+
* @param key 键
66+
* @param mapKey 字典键
67+
* @param mapValue 字典值
68+
* @return
69+
*/
70+
Boolean putHashCache(String key, String mapKey, Object mapValue);
71+
72+
/**
73+
* 批量增加字典
74+
*
75+
* @param key 键
76+
* @param maps 字典键值集合
77+
* @return true/false
78+
*/
79+
Boolean putAllHashCache(String key, Map<String, Object> maps);
980

81+
/**
82+
* 通过键批量获取字典键值集合
83+
*
84+
* @param key 键
85+
* @return 字典键值集合
86+
*/
87+
Map<String, Object> getAllHashCache(String key);
88+
//******hash(字典) 操作 end
1089

11-
Boolean put(String key, Object value, Integer expireTime);
1290

91+
//******list(列表) 操作 start
1392

14-
Object get(String key);
93+
/**
94+
* 增加set缓存
95+
*
96+
* @param key 键
97+
* @param value 值
98+
* @return
99+
*/
100+
Boolean addSetCache(String key, Object value);
15101

102+
/**
103+
* 批量增加set缓存
104+
*
105+
* @param key 键
106+
* @param values set列表
107+
* @return true/false
108+
*/
109+
Boolean addAllSetCache(String key, List<Object> values);
16110

17-
Boolean existsKey(String key);
111+
/**
112+
* 检查set缓存是否存在
113+
*
114+
* @param key 键
115+
* @param value 值
116+
* @return true/false
117+
*/
118+
Boolean existsSetCache(String key, Object value);
18119

120+
/**
121+
* 批量获取set列表
122+
*
123+
* @param key 键
124+
* @return 值集合
125+
*/
126+
Set<Object> getAllSetCache(String key);
127+
//******list(列表) 操作 end
19128

20-
Object getMap(String key, String mapKey);
21129

22-
Boolean putMap(String key, String mapKey, Object mapValue);
23130
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package iot.technology.mqtt.storage.session.cache;
22

3-
import org.redisson.api.RBucket;
4-
import org.redisson.api.RKeys;
5-
import org.redisson.api.RMap;
6-
import org.redisson.api.RedissonClient;
3+
import org.redisson.api.*;
74
import org.springframework.stereotype.Service;
85

96
import javax.annotation.Resource;
7+
import java.util.List;
8+
import java.util.Map;
109
import java.util.Objects;
10+
import java.util.Set;
1111
import java.util.concurrent.TimeUnit;
1212

1313
/**
@@ -20,45 +20,84 @@ public class RedissonClientService implements CacheManager {
2020
private RedissonClient redissonClient;
2121

2222
@Override
23-
public Boolean delete(String key) {
23+
public Boolean deleteStringCache(String key) {
2424
RKeys keys = redissonClient.getKeys();
2525
keys.delete(key);
2626
return Boolean.TRUE;
2727
}
2828

2929

3030
@Override
31-
public Boolean put(String key, Object value, Integer expireTime) {
31+
public Boolean putStringCache(String key, Object value, Integer expireTime) {
3232
RBucket<Object> bucket = redissonClient.getBucket(key);
3333
int cacheTime = (Objects.nonNull(expireTime) && expireTime > 0) ? expireTime : 300;
3434
bucket.set(value, cacheTime, TimeUnit.SECONDS);
3535
return Boolean.TRUE;
3636
}
3737

3838
@Override
39-
public Object get(String key) {
39+
public Object getStringCache(String key) {
4040
RBucket<Object> bucket = redissonClient.getBucket(key);
4141
return bucket.get();
4242
}
4343

4444
@Override
45-
public Boolean existsKey(String key) {
45+
public Boolean existsStringCache(String key) {
4646
RKeys keys = redissonClient.getKeys();
4747
Boolean result = keys.countExists(key) > 0 ? Boolean.TRUE : Boolean.FALSE;
4848
return result;
4949
}
5050

5151

5252
@Override
53-
public Object getMap(String key, String mapKey) {
54-
RMap<String, Object> map = redissonClient.getMap(key);
55-
return map.get(mapKey);
53+
public Object getHashCache(String key, String mapKey) {
54+
RMap<String, Object> cache = redissonClient.getMap(key);
55+
return cache.get(mapKey);
5656
}
5757

5858
@Override
59-
public Boolean putMap(String key, String mapKey, Object mapValue) {
60-
RMap<String, Object> map = redissonClient.getMap(key);
61-
map.put(mapKey, mapValue);
59+
public Boolean putHashCache(String key, String mapKey, Object mapValue) {
60+
RMap<String, Object> cache = redissonClient.getMap(key);
61+
cache.putIfAbsent(mapKey, mapValue);
6262
return Boolean.TRUE;
6363
}
64+
65+
@Override
66+
public Boolean putAllHashCache(String key, Map<String, Object> maps) {
67+
RMap<String, Object> cache = redissonClient.getMap(key);
68+
cache.putAll(maps);
69+
return Boolean.TRUE;
70+
}
71+
72+
@Override
73+
public Map<String, Object> getAllHashCache(String key) {
74+
RMap<String, Object> cache = redissonClient.getMap(key);
75+
Map<String, Object> hashMap = cache.readAllMap();
76+
return hashMap;
77+
}
78+
79+
@Override
80+
public Boolean addSetCache(String key, Object value) {
81+
RSet<Object> cache = redissonClient.getSet(key);
82+
cache.add(value);
83+
return Boolean.TRUE;
84+
}
85+
86+
@Override
87+
public Boolean addAllSetCache(String key, List<Object> values) {
88+
RSet<Object> cache = redissonClient.getSet(key);
89+
return cache.addAll(values);
90+
}
91+
92+
@Override
93+
public Boolean existsSetCache(String key, Object value) {
94+
RSet<Object> cache = redissonClient.getSet(key);
95+
return cache.contains(value);
96+
}
97+
98+
@Override
99+
public Set<Object> getAllSetCache(String key) {
100+
RSet<Object> cache = redissonClient.getSet(key);
101+
return cache.readAll();
102+
}
64103
}

0 commit comments

Comments
 (0)