【8.3.3】【file】【cache】更新缓存的使用

pull/62/head
stylefeng 2025-01-14 21:07:26 +08:00
parent 20c6e51a38
commit 767158fc15
1 changed files with 50 additions and 31 deletions

View File

@ -59,6 +59,7 @@ import cn.stylefeng.roses.kernel.file.modular.mapper.SysFileInfoMapper;
import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
import cn.stylefeng.roses.kernel.file.modular.service.SysFileStorageService;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.roses.kernel.rule.util.StrFilterUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -108,36 +109,34 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
public SysFileInfoResponse getFileInfoResult(Long fileId) {
// 查询库中文件信息
SysFileInfoRequest sysFileInfoRequest = new SysFileInfoRequest();
sysFileInfoRequest.setFileId(fileId);
SysFileInfo sysFileInfo = this.querySysFileInfo(sysFileInfoRequest);
SysFileInfoResponse fileInfo = this.getFileInfoWithoutContent(fileId);
if (fileInfo == null) {
throw new ServiceException(FileExceptionEnum.FILE_NOT_FOUND);
}
// 获取文件字节码
byte[] fileBytes;
try {
// 如果是存储在数据库从数据库中获取其他的方式走FileOperatorApi
if (FileLocationEnum.DB.getCode().equals(sysFileInfo.getFileLocation())) {
if (FileLocationEnum.DB.getCode().equals(fileInfo.getFileLocation())) {
SysFileStorage storage = sysFileStorageService.getById(fileId);
// 如果在数据库中找不到这条记录,则从本地资源路径中找,因为有的文件字节较大不宜往数据库中存储
if (storage == null) {
fileBytes = ResourceUtil.readBytes("pics/" + sysFileInfo.getFileId() + "." + sysFileInfo.getFileSuffix());
fileBytes = ResourceUtil.readBytes("pics/" + fileInfo.getFileId() + "." + fileInfo.getFileSuffix());
} else {
fileBytes = storage.getFileBytes();
}
} else {
fileBytes = fileOperatorApi.getFileBytes(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName());
fileBytes = fileOperatorApi.getFileBytes(fileInfo.getFileBucket(), fileInfo.getFileObjectName());
}
} catch (Exception e) {
log.error("获取文件异常,具体信息为:{}", e.getMessage());
log.error("获取文件异常,具体信息为:{}", e.getMessage());
throw new FileException(FileExceptionEnum.FILE_STREAM_ERROR, e.getMessage());
}
// 设置文件字节码
SysFileInfoResponse sysFileInfoResult = new SysFileInfoResponse();
BeanUtil.copyProperties(sysFileInfo, sysFileInfoResult);
sysFileInfoResult.setFileBytes(fileBytes);
return sysFileInfoResult;
fileInfo.setFileBytes(fileBytes);
return fileInfo;
}
@Override
@ -186,6 +185,9 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
// 存储新版本文件信息
this.save(newFileInfo);
// 删除文件信息的缓存
fileInfoCache.remove(oldFileInfo.getFileId().toString());
// 返回文件信息体
SysFileInfoResponse fileUploadInfoResult = new SysFileInfoResponse();
BeanUtil.copyProperties(newFileInfo, fileUploadInfoResult);
@ -235,6 +237,10 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
this.fileOperatorApi.deleteFile(fileInfo.getFileBucket(), fileInfo.getFileObjectName());
}
}
// 删除文件缓存
List<String> fileIdListString = fileInfos.stream().map(SysFileInfo::getFileId).map(String::valueOf).collect(Collectors.toList());
fileInfoCache.remove(fileIdListString);
}
@Override
@ -444,15 +450,30 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
@Override
public SysFileInfoResponse getFileInfoWithoutContent(Long fileId) {
SysFileInfoRequest sysFileInfoRequest = new SysFileInfoRequest();
sysFileInfoRequest.setFileId(fileId);
if (fileId == null) {
return null;
}
// 获取文件的基本信息
SysFileInfo sysFileInfo = querySysFileInfo(sysFileInfoRequest);
// 先从缓存中获取文件信息详情
SysFileInfoResponse cachedResult = fileInfoCache.get(fileId.toString());
if (cachedResult != null) {
return cachedResult;
}
LambdaQueryWrapper<SysFileInfo> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysFileInfo::getFileId, fileId);
wrapper.select(SysFileInfo::getFileId, SysFileInfo::getFileCode, SysFileInfo::getFileVersion, SysFileInfo::getFileStatus, SysFileInfo::getFileSuffix, SysFileInfo::getFileSizeKb,
SysFileInfo::getFileSizeInfo, SysFileInfo::getSecretFlag, SysFileInfo::getFileObjectName, SysFileInfo::getFileLocation, SysFileInfo::getFileBucket, SysFileInfo::getFileOriginName,
SysFileInfo::getFilePath, SysFileInfo::getFileUrl, SysFileInfo::getFileMd5);
SysFileInfo sysFileInfo = this.getOne(wrapper, false);
if (sysFileInfo == null) {
return null;
}
// 转化实体
SysFileInfoResponse sysFileInfoResponse = new SysFileInfoResponse();
BeanUtil.copyProperties(sysFileInfo, sysFileInfoResponse);
fileInfoCache.put(fileId.toString(), sysFileInfoResponse, FileConstants.DEFAULT_FILE_TIMEOUT_SECONDS);
return sysFileInfoResponse;
}
@ -465,33 +486,34 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
@Override
public String getFileAuthUrl(Long fileId, String token) {
// 获取文件的基本信息
SysFileInfoRequest sysFileInfoRequest = new SysFileInfoRequest();
sysFileInfoRequest.setFileId(fileId);
SysFileInfo sysFileInfo = querySysFileInfo(sysFileInfoRequest);
SysFileInfoResponse fileInfo = this.getFileInfoWithoutContent(fileId);
if(fileInfo == null){
throw new ServiceException(FILE_NOT_FOUND);
}
// 如果是数据库存储则返回previewUrl
if (sysFileInfo.getFileLocation().equals(FileLocationEnum.DB.getCode())) {
if (fileInfo.getFileLocation().equals(FileLocationEnum.DB.getCode())) {
return this.sysFileStorageService.getFileAuthUrl(String.valueOf(fileId));
} else {
// 返回第三方存储文件url
return fileOperatorApi.getFileAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName(),
FileConfigExpander.getDefaultFileTimeoutSeconds());
return fileOperatorApi.getFileAuthUrl(fileInfo.getFileBucket(), fileInfo.getFileObjectName(), FileConfigExpander.getDefaultFileTimeoutSeconds());
}
}
@Override
public String getFileUnAuthUrl(Long fileId) {
// 获取文件的基本信息
SysFileInfoRequest sysFileInfoRequest = new SysFileInfoRequest();
sysFileInfoRequest.setFileId(fileId);
SysFileInfo sysFileInfo = querySysFileInfo(sysFileInfoRequest);
SysFileInfoResponse fileInfo = this.getFileInfoWithoutContent(fileId);
if(fileInfo == null){
throw new ServiceException(FILE_NOT_FOUND);
}
// 如果是数据库存储则返回previewUrl
if (sysFileInfo.getFileLocation().equals(FileLocationEnum.DB.getCode())) {
if (fileInfo.getFileLocation().equals(FileLocationEnum.DB.getCode())) {
return this.sysFileStorageService.getFileUnAuthUrl(String.valueOf(fileId));
} else {
// 返回第三方存储文件url
return fileOperatorApi.getFileUnAuthUrl(sysFileInfo.getFileBucket(), sysFileInfo.getFileObjectName());
return fileOperatorApi.getFileUnAuthUrl(fileInfo.getFileBucket(), fileInfo.getFileObjectName());
}
}
@ -555,10 +577,7 @@ public class SysFileInfoServiceImpl extends ServiceImpl<SysFileInfoMapper, SysFi
antdvFileInfo.setUid(String.valueOf(fileId));
// 设置文件名称
LambdaQueryWrapper<SysFileInfo> sysFileInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysFileInfoLambdaQueryWrapper.eq(SysFileInfo::getFileId, fileId);
sysFileInfoLambdaQueryWrapper.select(SysFileInfo::getFileOriginName, SysFileInfo::getFileSizeInfo, SysFileInfo::getFileSuffix);
SysFileInfo sysFileInfo = this.getOne(sysFileInfoLambdaQueryWrapper);
SysFileInfoResponse sysFileInfo = this.getFileInfoWithoutContent(fileId);
if (sysFileInfo == null) {
continue;
}