From eb4761e0b893922f5778163a0c8a1de1f104cc7e Mon Sep 17 00:00:00 2001 From: enilu Date: Sun, 26 Apr 2020 19:20:14 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20=E6=9B=B4=E6=96=B0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E8=BF=9E=E5=B8=A6=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=B8=B8=E9=87=8F=E5=B7=A5=E5=85=B7=E7=B1=BB=E4=B8=AD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=9C=AC=E5=9C=B0(TimeCacheMap)=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/enilu/flash/cache/BaseCache.java | 15 ++++++++ .../flash/cache/impl/ConfigCacheImpl.java | 7 ++-- .../enilu/flash/cache/impl/DictCacheImpl.java | 36 ++++++++++--------- .../service/system/IConstantFactory.java | 1 + .../service/system/impl/ConstantFactory.java | 4 +++ 5 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 flash-core/src/main/java/cn/enilu/flash/cache/BaseCache.java diff --git a/flash-core/src/main/java/cn/enilu/flash/cache/BaseCache.java b/flash-core/src/main/java/cn/enilu/flash/cache/BaseCache.java new file mode 100644 index 00000000..ad42f822 --- /dev/null +++ b/flash-core/src/main/java/cn/enilu/flash/cache/BaseCache.java @@ -0,0 +1,15 @@ +package cn.enilu.flash.cache; + +import cn.enilu.flash.bean.vo.SpringContextHolder; +import cn.enilu.flash.service.system.impl.ConstantFactory; + +/** + * @author :enilu + * @date :Created in 2020/4/26 19:07 + */ +public abstract class BaseCache implements Cache { + @Override + public void cache() { + SpringContextHolder.getBean(ConstantFactory.class).cleanLocalCache(); + } +} diff --git a/flash-core/src/main/java/cn/enilu/flash/cache/impl/ConfigCacheImpl.java b/flash-core/src/main/java/cn/enilu/flash/cache/impl/ConfigCacheImpl.java index 83b29672..20897575 100644 --- a/flash-core/src/main/java/cn/enilu/flash/cache/impl/ConfigCacheImpl.java +++ b/flash-core/src/main/java/cn/enilu/flash/cache/impl/ConfigCacheImpl.java @@ -2,6 +2,7 @@ import cn.enilu.flash.bean.entity.system.Cfg; import cn.enilu.flash.bean.enumeration.ConfigKeyEnum; +import cn.enilu.flash.cache.BaseCache; import cn.enilu.flash.cache.CacheDao; import cn.enilu.flash.cache.ConfigCache; import cn.enilu.flash.dao.system.CfgRepository; @@ -20,7 +21,7 @@ * @version 2018/12/20 0020 */ @Service -public class ConfigCacheImpl implements ConfigCache { +public class ConfigCacheImpl extends BaseCache implements ConfigCache { private static final Logger logger = LoggerFactory.getLogger(ConfigCacheImpl.class); @Autowired private CfgRepository cfgRepository; @@ -29,7 +30,7 @@ public class ConfigCacheImpl implements ConfigCache { @Override public Object get(String key) { - return (String) cacheDao.hget(CacheDao.CONSTANT,key); + return cacheDao.hget(CacheDao.CONSTANT,key); } @Override @@ -71,7 +72,7 @@ public void del(String key, String val) { @Override public void cache() { - logger.info("reset config cache"); + super.cache(); List list = cfgRepository.findAll(); if (list != null && !list.isEmpty()) { for (Cfg cfg : list) { diff --git a/flash-core/src/main/java/cn/enilu/flash/cache/impl/DictCacheImpl.java b/flash-core/src/main/java/cn/enilu/flash/cache/impl/DictCacheImpl.java index eb871929..a19ce26a 100644 --- a/flash-core/src/main/java/cn/enilu/flash/cache/impl/DictCacheImpl.java +++ b/flash-core/src/main/java/cn/enilu/flash/cache/impl/DictCacheImpl.java @@ -2,6 +2,7 @@ import cn.enilu.flash.bean.constant.cache.CacheKey; import cn.enilu.flash.bean.entity.system.Dict; +import cn.enilu.flash.cache.BaseCache; import cn.enilu.flash.cache.CacheDao; import cn.enilu.flash.cache.DictCache; import cn.enilu.flash.dao.system.DictRepository; @@ -17,7 +18,7 @@ * @version 2018/12/23 0023 */ @Component -public class DictCacheImpl implements DictCache { +public class DictCacheImpl extends BaseCache implements DictCache { @Autowired private DictRepository dictRepository; @Autowired @@ -25,40 +26,41 @@ public class DictCacheImpl implements DictCache { @Override public List getDictsByPname(String dictName) { - return (List) cacheDao.hget(CacheDao.CONSTANT,CacheKey.DICT+dictName,List.class); + return (List) cacheDao.hget(CacheDao.CONSTANT, CacheKey.DICT + dictName, List.class); } @Override public String getDict(Long dictId) { - return (String) get(CacheKey.DICT_NAME+String.valueOf(dictId)); + return (String) get(CacheKey.DICT_NAME +dictId); } @Override public void cache() { - List list = dictRepository.findByPid(0L); - for(Dict dict:list){ - List children = dictRepository.findByPid(dict.getId()); - if(children.isEmpty()) { - continue; - } - set(String.valueOf(dict.getId()), children); - set(dict.getName(), children); - for(Dict child:children){ - set(CacheKey.DICT_NAME+child.getId(),child.getName()); - } + super.cache(); + List list = dictRepository.findByPid(0L); + for (Dict dict : list) { + List children = dictRepository.findByPid(dict.getId()); + if (children.isEmpty()) { + continue; + } + set(String.valueOf(dict.getId()), children); + set(dict.getName(), children); + for (Dict child : children) { + set(CacheKey.DICT_NAME + child.getId(), child.getName()); + } - } + } } @Override public Object get(String key) { - return cacheDao.hget(CacheDao.CONSTANT,CacheKey.DICT+key); + return cacheDao.hget(CacheDao.CONSTANT, CacheKey.DICT + key); } @Override public void set(String key, Object val) { - cacheDao.hset(CacheDao.CONSTANT,CacheKey.DICT+key,val); + cacheDao.hset(CacheDao.CONSTANT, CacheKey.DICT + key, val); } } diff --git a/flash-core/src/main/java/cn/enilu/flash/service/system/IConstantFactory.java b/flash-core/src/main/java/cn/enilu/flash/service/system/IConstantFactory.java index 3c697ab6..c983ed0e 100644 --- a/flash-core/src/main/java/cn/enilu/flash/service/system/IConstantFactory.java +++ b/flash-core/src/main/java/cn/enilu/flash/service/system/IConstantFactory.java @@ -160,4 +160,5 @@ public interface IConstantFactory { Menu getMenu(Long id) ; Notice getNotice(Long id); + void cleanLocalCache(); } diff --git a/flash-core/src/main/java/cn/enilu/flash/service/system/impl/ConstantFactory.java b/flash-core/src/main/java/cn/enilu/flash/service/system/impl/ConstantFactory.java index 68f02585..7a648030 100644 --- a/flash-core/src/main/java/cn/enilu/flash/service/system/impl/ConstantFactory.java +++ b/flash-core/src/main/java/cn/enilu/flash/service/system/impl/ConstantFactory.java @@ -429,4 +429,8 @@ public Notice getNotice(Long id) { return null; } + @Override + public void cleanLocalCache() { + cache.neverCleanup(); + } }