提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.dict.core; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.ObjectUtil; |
|
4 |
import com.iailab.framework.common.core.KeyValue; |
|
5 |
import com.iailab.framework.common.util.cache.CacheUtils; |
|
6 |
import com.iailab.module.system.api.dict.DictDataApi; |
|
7 |
import com.iailab.module.system.api.dict.dto.DictDataRespDTO; |
|
8 |
import com.google.common.cache.CacheLoader; |
|
9 |
import com.google.common.cache.LoadingCache; |
|
10 |
import lombok.SneakyThrows; |
|
11 |
import lombok.extern.slf4j.Slf4j; |
|
12 |
|
|
13 |
import java.time.Duration; |
|
14 |
import java.util.List; |
|
15 |
|
|
16 |
/** |
|
17 |
* 字典工具类 |
|
18 |
* |
|
19 |
* @author iailab |
|
20 |
*/ |
|
21 |
@Slf4j |
|
22 |
public class DictFrameworkUtils { |
|
23 |
|
|
24 |
private static DictDataApi dictDataApi; |
|
25 |
|
|
26 |
private static final DictDataRespDTO DICT_DATA_NULL = new DictDataRespDTO(); |
|
27 |
|
|
28 |
// TODO @puhui999:GET_DICT_DATA_CACHE、GET_DICT_DATA_LIST_CACHE、PARSE_DICT_DATA_CACHE 这 3 个缓存是有点重叠,可以思考下,有没可能减少 1 个。微信讨论好私聊,再具体改哈 |
|
29 |
/** |
|
30 |
* 针对 {@link #getDictDataLabel(String, String)} 的缓存 |
|
31 |
*/ |
|
32 |
private static final LoadingCache<KeyValue<String, String>, DictDataRespDTO> GET_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache( |
|
33 |
Duration.ofMinutes(1L), // 过期时间 1 分钟 |
|
34 |
new CacheLoader<KeyValue<String, String>, DictDataRespDTO>() { |
|
35 |
|
|
36 |
@Override |
|
37 |
public DictDataRespDTO load(KeyValue<String, String> key) { |
|
38 |
return ObjectUtil.defaultIfNull(dictDataApi.getDictData(key.getKey(), key.getValue()).getCheckedData(), DICT_DATA_NULL); |
|
39 |
} |
|
40 |
|
|
41 |
}); |
|
42 |
|
|
43 |
/** |
|
44 |
* 针对 {@link #getDictDataLabelList(String)} 的缓存 |
|
45 |
*/ |
|
46 |
private static final LoadingCache<String, List<String>> GET_DICT_DATA_LIST_CACHE = CacheUtils.buildAsyncReloadingCache( |
|
47 |
Duration.ofMinutes(1L), // 过期时间 1 分钟 |
|
48 |
new CacheLoader<String, List<String>>() { |
|
49 |
|
|
50 |
@Override |
|
51 |
public List<String> load(String dictType) { |
|
52 |
return dictDataApi.getDictDataLabelList(dictType); |
|
53 |
} |
|
54 |
|
|
55 |
}); |
|
56 |
|
|
57 |
/** |
|
58 |
* 针对 {@link #parseDictDataValue(String, String)} 的缓存 |
|
59 |
*/ |
|
60 |
private static final LoadingCache<KeyValue<String, String>, DictDataRespDTO> PARSE_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache( |
|
61 |
Duration.ofMinutes(1L), // 过期时间 1 分钟 |
|
62 |
new CacheLoader<KeyValue<String, String>, DictDataRespDTO>() { |
|
63 |
|
|
64 |
@Override |
|
65 |
public DictDataRespDTO load(KeyValue<String, String> key) { |
|
66 |
return ObjectUtil.defaultIfNull(dictDataApi.parseDictData(key.getKey(), key.getValue()).getCheckedData(), DICT_DATA_NULL); |
|
67 |
} |
|
68 |
|
|
69 |
}); |
|
70 |
|
|
71 |
public static void init(DictDataApi dictDataApi) { |
|
72 |
DictFrameworkUtils.dictDataApi = dictDataApi; |
|
73 |
log.info("[init][初始化 DictFrameworkUtils 成功]"); |
|
74 |
} |
|
75 |
|
|
76 |
@SneakyThrows |
|
77 |
public static String getDictDataLabel(String dictType, Integer value) { |
|
78 |
return GET_DICT_DATA_CACHE.get(new KeyValue<>(dictType, String.valueOf(value))).getLabel(); |
|
79 |
} |
|
80 |
|
|
81 |
@SneakyThrows |
|
82 |
public static String getDictDataLabel(String dictType, String value) { |
|
83 |
return GET_DICT_DATA_CACHE.get(new KeyValue<>(dictType, value)).getLabel(); |
|
84 |
} |
|
85 |
|
|
86 |
@SneakyThrows |
|
87 |
public static List<String> getDictDataLabelList(String dictType) { |
|
88 |
return GET_DICT_DATA_LIST_CACHE.get(dictType); |
|
89 |
} |
|
90 |
|
|
91 |
@SneakyThrows |
|
92 |
public static String parseDictDataValue(String dictType, String label) { |
|
93 |
return PARSE_DICT_DATA_CACHE.get(new KeyValue<>(dictType, label)).getValue(); |
|
94 |
} |
|
95 |
|
|
96 |
} |