Merge branch 'develop' of https://gitee.com/likeadmin/likeadmin_java into develop

This commit is contained in:
TinyAnts 2023-03-23 11:41:31 +08:00
commit a2982000a6
2 changed files with 142 additions and 1 deletions

View File

@ -0,0 +1,110 @@
package com.mdd.common.cache;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.entity.system.SystemConfig;
import com.mdd.common.mapper.system.SystemConfigMapper;
import com.mdd.common.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 系统配置缓存
*/
public class ConfigCache {
private static final String cacheKey = "config";
/**
* 设置缓存配置
*
*/
public static void set() {
SystemConfigMapper model = SpringUtils.getBean(SystemConfigMapper.class);
// 查询全部配置项设置缓存
List<SystemConfig> configs = model.selectList(
new QueryWrapper<SystemConfig>()
.select("id", "type", "name", "value")
);
List<String> typeList = configs.stream().map(SystemConfig::getType)
.collect(Collectors.toList())
.stream().distinct()
.collect(Collectors.toList());
Map<String, Object> map = new LinkedHashMap<>();
Map<String, Object> subMap = new LinkedHashMap<>();
for (String typeItem : typeList) {
subMap.clear();
for (SystemConfig configItem : configs) {
if (configItem.getType().equals(typeItem)) {
subMap.put(configItem.getName(), configItem.getValue());
}
}
map.put(typeItem, JSON.toJSONString(subMap));
}
RedisUtils.set(cacheKey, JSON.toJSONString(map));
}
/**
* 获取配置缓存 返回字符串
*
* @param type 类型
* @param name 名称
* @return String
*/
public static String get(String type, String name) {
Object configs = RedisUtils.get(cacheKey);
if (!StringUtils.isNull(configs) && !StringUtils.isEmpty(configs.toString())) {
Map<String, String> configType = MapUtils.jsonToMap(configs.toString());
if (!StringUtils.isEmpty(configType)) {
Map<String, String> configData = MapUtils.jsonToMap(configType.get(type));
return configData.get(name);
}
}
return null;
}
/**
* 根据type获取配置缓存 返回map
*
* @param type 类型
* @return Map
*/
public static Map<String, String> get(String type) {
// 读取缓存
Map<String, Object> cacheConfig = getConfigCache();
if (!cacheConfig.isEmpty()) {
Object cacheConfigObj = cacheConfig.get(type);
Map<String, String> cacheConfigMap = MapUtils.jsonToMap(cacheConfigObj.toString());
if (!cacheConfigMap.isEmpty()) {
return cacheConfigMap;
}
}
return new LinkedHashMap<>();
}
/**
* 获取配置
*
* @return Map
*/
public static Map<String, Object> getConfigCache() {
// 获取缓存
Object config = RedisUtils.get(cacheKey);
if (!StringUtils.isNull(config) && !StringUtils.isEmpty(config.toString())) {
return MapUtils.jsonToMapAsObj(config.toString());
}
return new LinkedHashMap<>();
}
/**
* 清除缓存
*/
public static void clear() {
RedisUtils.del(cacheKey);
}
}

View File

@ -1,6 +1,7 @@
package com.mdd.common.util;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.cache.ConfigCache;
import com.mdd.common.entity.system.SystemConfig;
import com.mdd.common.mapper.system.SystemConfigMapper;
@ -21,6 +22,13 @@ public class ConfigUtils {
* @return Map<String, String>
*/
public static Map<String, String> get(String type) {
// 读取缓存
Map<String, String> cache = ConfigCache.get(type);
if (!cache.isEmpty()) {
return cache;
}
SystemConfigMapper model = SpringUtils.getBean(SystemConfigMapper.class);
List<SystemConfig> configs = model.selectList(
@ -45,6 +53,12 @@ public class ConfigUtils {
* @return String
*/
public static String get(String type, String name) {
// 获取缓存配置
String cache = ConfigCache.get(type, name);
if (!StringUtils.isNull(cache) && !StringUtils.isEmpty(cache)) {
return cache;
}
SystemConfigMapper model = SpringUtils.getBean(SystemConfigMapper.class);
SystemConfig config = model.selectOne(
@ -65,6 +79,13 @@ public class ConfigUtils {
* @return String
*/
public static String get(String type, String name, String defaults) {
// 获取缓存配置
String cache = ConfigCache.get(type, name);
if (!StringUtils.isNull(cache) && !StringUtils.isEmpty(cache)) {
return cache;
}
SystemConfigMapper model = SpringUtils.getBean(SystemConfigMapper.class);
SystemConfig config = model.selectOne(
@ -89,6 +110,14 @@ public class ConfigUtils {
* @return String
*/
public static Map<String, String> getMap(String type, String name) {
// 获取缓存
String cache = ConfigCache.get(type, name);
if (!StringUtils.isNull(cache) && !StringUtils.isEmpty(cache)) {
System.out.println("获取到缓存了");
return MapUtils.jsonToMap(cache);
}
SystemConfigMapper model = SpringUtils.getBean(SystemConfigMapper.class);
SystemConfig config = model.selectOne(
@ -137,6 +166,8 @@ public class ConfigUtils {
systemConfig.setUpdateTime(System.currentTimeMillis() / 1000);
model.insert(systemConfig);
}
}
// 设置缓存
ConfigCache.set();
}
}