fileConfig 缓存,使用 guava 替代 job 扫描,目的:提升启动速度,加快缓存失效
parent
f0897cb435
commit
0162933ce7
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -59,31 +59,6 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|||
@MockBean
|
||||
private FileClientFactory fileClientFactory;
|
||||
|
||||
@Test
|
||||
public void testInitLocalCache() {
|
||||
// mock 数据
|
||||
FileConfigDO configDO1 = randomFileConfigDO().setId(1L).setMaster(true);
|
||||
fileConfigMapper.insert(configDO1);
|
||||
FileConfigDO configDO2 = randomFileConfigDO().setId(2L).setMaster(false);
|
||||
fileConfigMapper.insert(configDO2);
|
||||
// mock fileClientFactory 获得 master
|
||||
FileClient masterFileClient = mock(FileClient.class);
|
||||
when(fileClientFactory.getFileClient(eq(1L))).thenReturn(masterFileClient);
|
||||
|
||||
// 调用
|
||||
fileConfigService.initLocalCache();
|
||||
// 断言 fileClientFactory 调用
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(1L),
|
||||
eq(configDO1.getStorage()), eq(configDO1.getConfig()));
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(2L),
|
||||
eq(configDO2.getStorage()), eq(configDO2.getConfig()));
|
||||
assertSame(masterFileClient, fileConfigService.getMasterFileClient());
|
||||
// 断言 fileConfigCache 缓存
|
||||
assertEquals(2, fileConfigService.getFileConfigCache().size());
|
||||
assertEquals(configDO1, fileConfigService.getFileConfigCache().get(0));
|
||||
assertEquals(configDO2, fileConfigService.getFileConfigCache().get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateFileConfig_success() {
|
||||
// 准备参数
|
||||
|
@ -102,6 +77,8 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|||
assertFalse(fileConfig.getMaster());
|
||||
assertEquals("/yunai", ((LocalFileClientConfig) fileConfig.getConfig()).getBasePath());
|
||||
assertEquals("https://www.iocoder.cn", ((LocalFileClientConfig) fileConfig.getConfig()).getDomain());
|
||||
// 验证 cache
|
||||
assertNull(fileConfigService.getClientCache().getIfPresent(fileConfigId));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -125,6 +102,8 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|||
assertPojoEquals(reqVO, fileConfig, "config");
|
||||
assertEquals("/yunai2", ((LocalFileClientConfig) fileConfig.getConfig()).getBasePath());
|
||||
assertEquals("https://doc.iocoder.cn", ((LocalFileClientConfig) fileConfig.getConfig()).getDomain());
|
||||
// 验证 cache
|
||||
assertNull(fileConfigService.getClientCache().getIfPresent(fileConfig.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -149,6 +128,8 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|||
// 断言数据
|
||||
assertTrue(fileConfigMapper.selectById(dbFileConfig.getId()).getMaster());
|
||||
assertFalse(fileConfigMapper.selectById(masterFileConfig.getId()).getMaster());
|
||||
// 验证 cache
|
||||
assertNull(fileConfigService.getClientCache().getIfPresent(0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -169,6 +150,8 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|||
fileConfigService.deleteFileConfig(id);
|
||||
// 校验数据不存在了
|
||||
assertNull(fileConfigMapper.selectById(id));
|
||||
// 验证 cache
|
||||
assertNull(fileConfigService.getClientCache().getIfPresent(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -250,14 +233,38 @@ public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|||
|
||||
@Test
|
||||
public void testGetFileClient() {
|
||||
// mock 数据
|
||||
FileConfigDO fileConfig = randomFileConfigDO().setMaster(false);
|
||||
fileConfigMapper.insert(fileConfig);
|
||||
// 准备参数
|
||||
Long id = randomLongId();
|
||||
Long id = fileConfig.getId();
|
||||
// mock 获得 Client
|
||||
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
|
||||
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
|
||||
|
||||
// 调用,并断言
|
||||
assertSame(fileClient, fileConfigService.getFileClient(id));
|
||||
// 断言缓存
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(id), eq(fileConfig.getStorage()),
|
||||
eq(fileConfig.getConfig()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMasterFileClient() {
|
||||
// mock 数据
|
||||
FileConfigDO fileConfig = randomFileConfigDO().setMaster(true);
|
||||
fileConfigMapper.insert(fileConfig);
|
||||
// 准备参数
|
||||
Long id = fileConfig.getId();
|
||||
// mock 获得 Client
|
||||
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
|
||||
when(fileClientFactory.getFileClient(eq(0L))).thenReturn(fileClient);
|
||||
|
||||
// 调用,并断言
|
||||
assertSame(fileClient, fileConfigService.getMasterFileClient());
|
||||
// 断言缓存
|
||||
verify(fileClientFactory).createOrUpdateFileClient(eq(0L), eq(fileConfig.getStorage()),
|
||||
eq(fileConfig.getConfig()));
|
||||
}
|
||||
|
||||
private FileConfigDO randomFileConfigDO() {
|
||||
|
|
|
@ -14,6 +14,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
||||
|
@ -138,4 +139,5 @@ public class FileServiceImplTest extends BaseDbUnitTest {
|
|||
// 断言
|
||||
assertSame(result, content);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue