update 优化 使用 spring 事件机制 重构 OssConfig 缓存更新

feature/model
疯狂的狮子li 2 years ago
parent b38151a0bb
commit 06cda3fb18

@ -3,6 +3,7 @@ package com.ruoyi.common.utils.spring;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
/**
@ -62,4 +63,12 @@ public final class SpringUtils extends SpringUtil {
return (T) AopContext.currentProxy();
}
/**
* spring
*/
public static ApplicationContext context() {
return getApplicationContext();
}
}

@ -16,6 +16,7 @@ import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.redis.CacheUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.oss.constant.OssConstant;
import com.ruoyi.oss.factory.OssFactory;
import com.ruoyi.system.domain.SysOssConfig;
@ -25,6 +26,7 @@ import com.ruoyi.system.mapper.SysOssConfigMapper;
import com.ruoyi.system.service.ISysOssConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -57,7 +59,7 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
if ("0".equals(config.getStatus())) {
RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, configKey);
}
setConfigCache(true, config);
SpringUtils.context().publishEvent(config);
}
// 初始化OSS工厂
OssFactory.init();
@ -88,7 +90,11 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
public Boolean insertByBo(SysOssConfigBo bo) {
SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class);
validEntityBeforeSave(config);
return setConfigCache(baseMapper.insert(config) > 0, config);
boolean flag = baseMapper.insert(config) > 0;
if (flag) {
SpringUtils.context().publishEvent(config);
}
return flag;
}
@Override
@ -101,7 +107,11 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
luw.set(ObjectUtil.isNull(config.getExt1()), SysOssConfig::getExt1, "");
luw.set(ObjectUtil.isNull(config.getRemark()), SysOssConfig::getRemark, "");
luw.eq(SysOssConfig::getOssConfigId, config.getOssConfigId());
return setConfigCache(baseMapper.update(config, luw) > 0, config);
boolean flag = baseMapper.update(config, luw) > 0;
if (flag) {
SpringUtils.context().publishEvent(config);
}
return flag;
}
/**
@ -165,19 +175,15 @@ public class SysOssConfigServiceImpl implements ISysOssConfigService {
}
/**
*
*
*
* @param flag
* @param config
* @return
*/
private boolean setConfigCache(boolean flag, SysOssConfig config) {
if (flag) {
CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config));
RedisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, config.getConfigKey(), msg -> {
log.info("发布刷新OSS配置 => " + msg);
});
}
return flag;
@EventListener
public void updateConfigCache(SysOssConfig config) {
CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), JsonUtils.toJsonString(config));
RedisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, config.getConfigKey(), msg -> {
log.info("发布刷新OSS配置 => " + msg);
});
}
}

Loading…
Cancel
Save