Skip to content

Commit

Permalink
Fix 更新缓存的时候连带更新常量工具类中使用的本地(TimeCacheMap)缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
enilu committed Apr 26, 2020
1 parent d892347 commit eb4761e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
15 changes: 15 additions & 0 deletions flash-core/src/main/java/cn/enilu/flash/cache/BaseCache.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -71,7 +72,7 @@ public void del(String key, String val) {

@Override
public void cache() {
logger.info("reset config cache");
super.cache();
List<Cfg> list = cfgRepository.findAll();
if (list != null && !list.isEmpty()) {
for (Cfg cfg : list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,48 +18,49 @@
* @version 2018/12/23 0023
*/
@Component
public class DictCacheImpl implements DictCache {
public class DictCacheImpl extends BaseCache implements DictCache {
@Autowired
private DictRepository dictRepository;
@Autowired
private CacheDao cacheDao;

@Override
public List<Dict> getDictsByPname(String dictName) {
return (List<Dict>) cacheDao.hget(CacheDao.CONSTANT,CacheKey.DICT+dictName,List.class);
return (List<Dict>) 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<Dict> list = dictRepository.findByPid(0L);
for(Dict dict:list){
List<Dict> 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<Dict> list = dictRepository.findByPid(0L);
for (Dict dict : list) {
List<Dict> 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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,5 @@ public interface IConstantFactory {
Menu getMenu(Long id) ;

Notice getNotice(Long id);
void cleanLocalCache();
}
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,8 @@ public Notice getNotice(Long id) {
return null;
}

@Override
public void cleanLocalCache() {
cache.neverCleanup();
}
}

0 comments on commit eb4761e

Please sign in to comment.