mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 12:18:42 +08:00 
			
		
		
		
	fileConfig 缓存,使用 guava 替代 job 扫描,目的:提升启动速度,加快缓存失效
This commit is contained in:
		@@ -6,9 +6,6 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
 | 
			
		||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigPageReqVO;
 | 
			
		||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
 | 
			
		||||
import org.apache.ibatis.annotations.Mapper;
 | 
			
		||||
import org.apache.ibatis.annotations.Select;
 | 
			
		||||
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
 | 
			
		||||
@Mapper
 | 
			
		||||
public interface FileConfigMapper extends BaseMapperX<FileConfigDO> {
 | 
			
		||||
@@ -21,7 +18,8 @@ public interface FileConfigMapper extends BaseMapperX<FileConfigDO> {
 | 
			
		||||
                .orderByDesc(FileConfigDO::getId));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Select("SELECT COUNT(*) FROM infra_file_config WHERE update_time > #{maxUpdateTime}")
 | 
			
		||||
    Long selectCountByUpdateTimeGt(LocalDateTime maxUpdateTime);
 | 
			
		||||
    default FileConfigDO selectByMaster() {
 | 
			
		||||
        return selectOne(FileConfigDO::getMaster, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,10 +1,8 @@
 | 
			
		||||
package cn.iocoder.yudao.module.infra.service.file;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollUtil;
 | 
			
		||||
import cn.hutool.core.io.resource.ResourceUtil;
 | 
			
		||||
import cn.hutool.core.util.IdUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
 | 
			
		||||
@@ -17,22 +15,22 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigU
 | 
			
		||||
import cn.iocoder.yudao.module.infra.convert.file.FileConfigConvert;
 | 
			
		||||
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileConfigDO;
 | 
			
		||||
import cn.iocoder.yudao.module.infra.dal.mysql.file.FileConfigMapper;
 | 
			
		||||
import com.google.common.cache.CacheLoader;
 | 
			
		||||
import com.google.common.cache.LoadingCache;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.scheduling.annotation.Scheduled;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.transaction.annotation.Transactional;
 | 
			
		||||
import org.springframework.validation.annotation.Validated;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.PostConstruct;
 | 
			
		||||
import javax.annotation.Resource;
 | 
			
		||||
import javax.validation.Validator;
 | 
			
		||||
import java.time.LocalDateTime;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.time.Duration;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache;
 | 
			
		||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG_DELETE_FAIL_MASTER;
 | 
			
		||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG_NOT_EXISTS;
 | 
			
		||||
 | 
			
		||||
@@ -46,19 +44,29 @@ import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class FileConfigServiceImpl implements FileConfigService {
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private FileClientFactory fileClientFactory;
 | 
			
		||||
    private static final Long CACHE_MASTER_ID = 0L;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 文件配置的缓存
 | 
			
		||||
     * {@link FileClient} 缓存,通过它异步刷新 fileClientFactory
 | 
			
		||||
     */
 | 
			
		||||
    @Getter
 | 
			
		||||
    private List<FileConfigDO> fileConfigCache;
 | 
			
		||||
    /**
 | 
			
		||||
     * Master FileClient 对象,有且仅有一个,即 {@link FileConfigDO#getMaster()} 对应的
 | 
			
		||||
     */
 | 
			
		||||
    @Getter
 | 
			
		||||
    private FileClient masterFileClient;
 | 
			
		||||
    private final LoadingCache<Long, FileClient> clientCache = buildAsyncReloadingCache(Duration.ofSeconds(10L),
 | 
			
		||||
            new CacheLoader<Long, FileClient>() {
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public FileClient load(Long id) {
 | 
			
		||||
                    FileConfigDO config = Objects.equals(CACHE_MASTER_ID, id) ?
 | 
			
		||||
                            fileConfigMapper.selectByMaster() : fileConfigMapper.selectById(id);
 | 
			
		||||
                    if (config != null) {
 | 
			
		||||
                        fileClientFactory.createOrUpdateFileClient(id, config.getStorage(), config.getConfig());
 | 
			
		||||
                    }
 | 
			
		||||
                    return fileClientFactory.getFileClient(id);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
             });
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private FileClientFactory fileClientFactory;
 | 
			
		||||
 | 
			
		||||
    @Resource
 | 
			
		||||
    private FileConfigMapper fileConfigMapper;
 | 
			
		||||
@@ -66,53 +74,12 @@ public class FileConfigServiceImpl implements FileConfigService {
 | 
			
		||||
    @Resource
 | 
			
		||||
    private Validator validator;
 | 
			
		||||
 | 
			
		||||
    @PostConstruct
 | 
			
		||||
    public void initLocalCache() {
 | 
			
		||||
        // 第一步:查询数据
 | 
			
		||||
        List<FileConfigDO> configs = fileConfigMapper.selectList();
 | 
			
		||||
        log.info("[initLocalCache][缓存文件配置,数量为:{}]", configs.size());
 | 
			
		||||
 | 
			
		||||
        // 第二步:构建缓存:创建或更新文件 Client
 | 
			
		||||
        configs.forEach(config -> {
 | 
			
		||||
            fileClientFactory.createOrUpdateFileClient(config.getId(), config.getStorage(), config.getConfig());
 | 
			
		||||
            // 如果是 master,进行设置
 | 
			
		||||
            if (Boolean.TRUE.equals(config.getMaster())) {
 | 
			
		||||
                masterFileClient = fileClientFactory.getFileClient(config.getId());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        this.fileConfigCache = configs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 通过定时任务轮询,刷新缓存
 | 
			
		||||
     *
 | 
			
		||||
     * 目的:多节点部署时,通过轮询”通知“所有节点,进行刷新
 | 
			
		||||
     */
 | 
			
		||||
    @Scheduled(initialDelay = 60, fixedRate = 60, timeUnit = TimeUnit.SECONDS)
 | 
			
		||||
    public void refreshLocalCache() {
 | 
			
		||||
        // 情况一:如果缓存里没有数据,则直接刷新缓存
 | 
			
		||||
        if (CollUtil.isEmpty(fileConfigCache)) {
 | 
			
		||||
            initLocalCache();
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // 情况二,如果缓存里数据,则通过 updateTime 判断是否有数据变更,有变更则刷新缓存
 | 
			
		||||
        LocalDateTime maxTime = CollectionUtils.getMaxValue(fileConfigCache, FileConfigDO::getUpdateTime);
 | 
			
		||||
        if (fileConfigMapper.selectCountByUpdateTimeGt(maxTime) > 0) {
 | 
			
		||||
            initLocalCache();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Long createFileConfig(FileConfigCreateReqVO createReqVO) {
 | 
			
		||||
        // 插入
 | 
			
		||||
        FileConfigDO fileConfig = FileConfigConvert.INSTANCE.convert(createReqVO)
 | 
			
		||||
                .setConfig(parseClientConfig(createReqVO.getStorage(), createReqVO.getConfig()))
 | 
			
		||||
                .setMaster(false); // 默认非 master
 | 
			
		||||
        fileConfigMapper.insert(fileConfig);
 | 
			
		||||
 | 
			
		||||
        // 刷新缓存
 | 
			
		||||
        initLocalCache();
 | 
			
		||||
        return fileConfig.getId();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -125,8 +92,8 @@ public class FileConfigServiceImpl implements FileConfigService {
 | 
			
		||||
                .setConfig(parseClientConfig(config.getStorage(), updateReqVO.getConfig()));
 | 
			
		||||
        fileConfigMapper.updateById(updateObj);
 | 
			
		||||
 | 
			
		||||
        // 刷新缓存
 | 
			
		||||
        initLocalCache();
 | 
			
		||||
        // 清空缓存
 | 
			
		||||
        clearCache(config.getId(), null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -139,8 +106,8 @@ public class FileConfigServiceImpl implements FileConfigService {
 | 
			
		||||
        // 更新
 | 
			
		||||
        fileConfigMapper.updateById(new FileConfigDO().setId(id).setMaster(true));
 | 
			
		||||
 | 
			
		||||
        // 刷新缓存
 | 
			
		||||
        initLocalCache();
 | 
			
		||||
        // 清空缓存
 | 
			
		||||
        clearCache(null, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private FileClientConfig parseClientConfig(Integer storage, Map<String, Object> config) {
 | 
			
		||||
@@ -164,8 +131,23 @@ public class FileConfigServiceImpl implements FileConfigService {
 | 
			
		||||
        // 删除
 | 
			
		||||
        fileConfigMapper.deleteById(id);
 | 
			
		||||
 | 
			
		||||
        // 刷新缓存
 | 
			
		||||
        initLocalCache();
 | 
			
		||||
        // 清空缓存
 | 
			
		||||
        clearCache(id, null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 清空指定文件配置
 | 
			
		||||
     *
 | 
			
		||||
     * @param id 配置编号
 | 
			
		||||
     * @param master 是否主配置
 | 
			
		||||
     */
 | 
			
		||||
    private void clearCache(Long id, Boolean master) {
 | 
			
		||||
        if (id != null) {
 | 
			
		||||
            clientCache.invalidate(id);
 | 
			
		||||
        }
 | 
			
		||||
        if (Boolean.TRUE.equals(master)) {
 | 
			
		||||
            clientCache.invalidate(CACHE_MASTER_ID);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private FileConfigDO validateFileConfigExists(Long id) {
 | 
			
		||||
@@ -192,12 +174,17 @@ public class FileConfigServiceImpl implements FileConfigService {
 | 
			
		||||
        validateFileConfigExists(id);
 | 
			
		||||
        // 上传文件
 | 
			
		||||
        byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
 | 
			
		||||
        return fileClientFactory.getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
 | 
			
		||||
        return getFileClient(id).upload(content, IdUtil.fastSimpleUUID() + ".jpg", "image/jpeg");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public FileClient getFileClient(Long id) {
 | 
			
		||||
        return fileClientFactory.getFileClient(id);
 | 
			
		||||
        return clientCache.getUnchecked(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public FileClient getMasterFileClient() {
 | 
			
		||||
        return clientCache.getUnchecked(CACHE_MASTER_ID);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user