8.11 缓存优化bug解决

This commit is contained in:
zhangsan 2025-08-11 16:47:20 +08:00
parent 45fd2f6534
commit 61e633f88e
4 changed files with 16 additions and 6 deletions

View File

@ -215,6 +215,7 @@ public class PictureController {
// /**
// * 分页获取图片列表封装类脱敏,普通用户使用且不能看到未过审的图片
// */
// @Deprecated
// @PostMapping("/list/page/vo")
// public BaseResponse<Page<PictureVO>> listPictureVOByPage(@RequestBody PictureQueryRequest pictureQueryRequest,
// HttpServletRequest request) {
@ -251,7 +252,6 @@ public class PictureController {
/**
* 分页获取图片列表封装类有缓存
*/
@Deprecated
@PostMapping("/list/page/vo")
public BaseResponse<Page<PictureVO>> listPictureVOByPageWithCache(
@RequestBody PictureQueryRequest pictureQueryRequest,

View File

@ -36,7 +36,6 @@ public class MyCacheManager {
*/
private final StringRedisTemplate stringRedisTemplate;
// ====== 新增强类型版本 ======
public <T> T getFromCacheOrDatabase(
String cacheKey,
TypeReference<T> typeRef,

View File

@ -6,6 +6,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -618,12 +619,21 @@ public class PictureServiceImpl extends ServiceImpl<PictureMapper, Picture>
ThrowUtils.throwIf(!hasPermission, ErrorCode.NO_AUTH_ERROR);
}
// 把查询条件序列化成 JSON
String queryJson = JSONUtil.toJsonStr(Map.of(
"spaceId", spaceId == null ? "public" : spaceId,
"category", queryRequest.getCategory(),
"tags", queryRequest.getTags(),
"searchText", queryRequest.getSearchText(),
"current", current,
"size", size
));
String cacheKey = "smilepicture:listPictureVOByPage:spaceId:" + (spaceId == null ? "public" : spaceId) +
":current:" + current +
":size:" + size;
// 计算 MD5 作为哈希也可用 SHA256
String hash = DigestUtil.md5Hex(queryJson);
// String cacheKey = "smilepicture:list:v" + ver + ":" + hash;
// 拼接缓存 key
String cacheKey = "smilepicture:listPictureVOByPage:v1:" + hash;
// 随机过期300600s
int expire = 300 + RandomUtil.randomInt(0, 300);

View File

@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.whut.smilepicturebackend.exception.BusinessException;
import edu.whut.smilepicturebackend.exception.ErrorCode;
import edu.whut.smilepicturebackend.exception.ThrowUtils;
import edu.whut.smilepicturebackend.manager.sharding.DynamicShardingManager;
import edu.whut.smilepicturebackend.mapper.SpaceMapper;
import edu.whut.smilepicturebackend.model.dto.space.SpaceAddRequest;