diff --git a/server/like-common/src/main/java/com/mdd/common/cache/ConfigCache.java b/server/like-common/src/main/java/com/mdd/common/cache/ConfigCache.java new file mode 100644 index 00000000..c3b474be --- /dev/null +++ b/server/like-common/src/main/java/com/mdd/common/cache/ConfigCache.java @@ -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 configs = model.selectList( + new QueryWrapper() + .select("id", "type", "name", "value") + ); + List typeList = configs.stream().map(SystemConfig::getType) + .collect(Collectors.toList()) + .stream().distinct() + .collect(Collectors.toList()); + + Map map = new LinkedHashMap<>(); + Map 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 configType = MapUtils.jsonToMap(configs.toString()); + if (!StringUtils.isEmpty(configType)) { + Map configData = MapUtils.jsonToMap(configType.get(type)); + return configData.get(name); + } + } + return null; + } + + /** + * 根据type获取配置缓存 返回map + * + * @param type 类型 + * @return Map + */ + public static Map get(String type) { + // 读取缓存 + Map cacheConfig = getConfigCache(); + if (!cacheConfig.isEmpty()) { + Object cacheConfigObj = cacheConfig.get(type); + Map cacheConfigMap = MapUtils.jsonToMap(cacheConfigObj.toString()); + if (!cacheConfigMap.isEmpty()) { + return cacheConfigMap; + } + } + return new LinkedHashMap<>(); + } + + /** + * 获取配置 + * + * @return Map + */ + public static Map 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); + } + +} diff --git a/server/like-common/src/main/java/com/mdd/common/util/ConfigUtils.java b/server/like-common/src/main/java/com/mdd/common/util/ConfigUtils.java index e8dabb3d..062c6fc3 100644 --- a/server/like-common/src/main/java/com/mdd/common/util/ConfigUtils.java +++ b/server/like-common/src/main/java/com/mdd/common/util/ConfigUtils.java @@ -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 */ public static Map get(String type) { + + // 读取缓存 + Map cache = ConfigCache.get(type); + if (!cache.isEmpty()) { + return cache; + } + SystemConfigMapper model = SpringUtils.getBean(SystemConfigMapper.class); List 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 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(); + } }