JsonUtil.java
1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.genersoft.iot.vmp.utils;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Objects;
/**
* JsonUtil
*
* @author KunLong-Luo
* @version 1.0.0
* @since 2023/2/2 15:24
*/
public final class JsonUtil {
private JsonUtil() {
}
/**
* safe json type conversion
*
* @param key redis key
* @param clazz cast type
* @param <T>
* @return result type
*/
public static <T> T redisJsonToObject(RedisTemplate<Object, Object> redisTemplate, String key, Class<T> clazz) {
Object jsonObject = redisTemplate.opsForValue().get(key);
if (Objects.isNull(jsonObject)) {
return null;
}
return clazz.cast(jsonObject);
}
/**
* 存入redis
* @param key redis key
* @param object redis value
*/
public static void redisJsonToObject(RedisTemplate<Object, Object> redisTemplate, String key, Object object) {
redisTemplate.opsForValue().set(key, object);
}
}