Просмотр исходного кода

【功能新增】增加一个定时刷新常量缓存的定时器
【修改】修改常量查询的排序,按创建时间倒序

fengshuonan 4 лет назад
Родитель
Сommit
324de61206

+ 1 - 1
_sql/guns-separation.sql

@ -775,7 +775,7 @@ CREATE TABLE `sys_timers`  (
-- ----------------------------
-- Records of sys_timers
-- ----------------------------
INSERT INTO `sys_timers` VALUES (1277952546828189698, '定时删除缓存', 'cn.stylefeng.guns.sys.modular.timer.tasks.SystemOutTaskRunner', '*/2 * * * * *', 2, '删除的是redis没用的东西', '2020-06-30 21:10:05', 1265476890672672808, '2020-07-07 16:01:57', 1265476890672672808);
INSERT INTO `sys_timers` VALUES (1277952546828189698, '定时同步缓存常量', 'cn.stylefeng.guns.sys.modular.timer.tasks.RefreshConstantsTaskRunner', '0 0/1 * * * ?', 1, '定时同步sys_config表的数据到缓存常量中', '2020-7-30 16:53:13', 1265476890672672808, '2020-7-30 16:53:18', 1265476890672672808);
-- ----------------------------
-- Table structure for sys_user

+ 5 - 1
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/consts/service/impl/SysConfigServiceImpl.java

@ -31,6 +31,7 @@ import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.enums.YesOrNotEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.base.entity.BaseEntity;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.sys.modular.consts.entity.SysConfig;
import cn.stylefeng.guns.sys.modular.consts.enums.SysConfigExceptionEnum;
@ -59,7 +60,7 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
        //构造查询条件
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(sysConfigParam)) {
        if (ObjectUtil.isNotNull(sysConfigParam)) {
            //如果名称不为空,则带上名称搜素搜条件
            if (ObjectUtil.isNotEmpty(sysConfigParam.getName())) {
                queryWrapper.like(SysConfig::getName, sysConfigParam.getName());
@ -77,6 +78,9 @@ public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig
        //查询未删除的
        queryWrapper.ne(SysConfig::getStatus, CommonStatusEnum.DELETED.getCode());
        //按时间倒序排列
        queryWrapper.orderByDesc(BaseEntity::getCreateTime);
        //查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

+ 72 - 0
guns-base-support/guns-system/src/main/java/cn/stylefeng/guns/sys/modular/timer/tasks/RefreshConstantsTaskRunner.java

@ -0,0 +1,72 @@
/*
Copyright [2020] [https://www.stylefeng.cn]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Guns采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点:
1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Guns源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处,作者声明等。
4.分发源码时候,请注明软件出处 https://gitee.com/stylefeng/guns-separation
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/stylefeng/guns-separation
6.若您的项目无法满足以上几点,可申请商业授权,获取Guns商业授权许可,请在官网购买授权,地址为 https://www.stylefeng.cn
 */
package cn.stylefeng.guns.sys.modular.timer.tasks;
import cn.hutool.log.Log;
import cn.stylefeng.guns.core.context.constant.ConstantContext;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.timer.TimerTaskRunner;
import cn.stylefeng.guns.sys.modular.consts.entity.SysConfig;
import cn.stylefeng.guns.sys.modular.consts.service.SysConfigService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
 * 定时器:用来根据sys_config表中的配置,刷新ConstantContextHolder中的缓存
 * <p>
 * 防止由于直接改动数据库,而调用缓存常量时,产生数据不一致问题
 *
 * @author fengshuonan
 * @date 2020/7/30 16:41
 */
@Component
public class RefreshConstantsTaskRunner implements TimerTaskRunner {
    private static final Log log = Log.get();
    @Resource
    private SysConfigService sysConfigService;
    @Override
    public void action() {
        // 查询库中的所有配置
        LambdaQueryWrapper<SysConfig> sysConfigLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysConfigLambdaQueryWrapper.eq(SysConfig::getStatus, CommonStatusEnum.ENABLE.getCode());
        sysConfigLambdaQueryWrapper.select(SysConfig::getCode, SysConfig::getValue);
        List<SysConfig> list = sysConfigService.list(sysConfigLambdaQueryWrapper);
        // 所有配置添加到缓存中,覆盖已有配置
        list.forEach(sysConfig -> ConstantContext.putConstant(sysConfig.getCode(), sysConfig.getValue()));
        log.info("定时任务:同步sys_config表中的数据到缓存常量,条数:{}", list.size());
    }
}