feat(service): 为标签人设列表增加Redis缓存

This commit is contained in:
2025-12-17 16:46:50 +08:00
parent 65cd9d9fae
commit 35c45abf73

View File

@@ -97,10 +97,21 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
@Override @Override
public List<KeyboardCharacterRespVO> selectListByTag(Long tagId) { public List<KeyboardCharacterRespVO> selectListByTag(Long tagId) {
List<KeyboardCharacter> keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>() // 先从缓存获取指定标签的人设列表
.eq(KeyboardCharacter::getDeleted, false) String cacheKey = "character:list:tag:" + tagId;
.eq(KeyboardCharacter::getTag, tagId) List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
.orderByDesc(KeyboardCharacter::getRank));
if (keyboardCharacters == null) {
// 缓存未命中,从数据库查询
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
.eq(KeyboardCharacter::getDeleted, false)
.eq(KeyboardCharacter::getTag, tagId)
.orderByDesc(KeyboardCharacter::getRank));
// 缓存到Redis7天过期
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
}
}
long userId = StpUtil.getLoginIdAsLong(); long userId = StpUtil.getLoginIdAsLong();
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId); List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
@@ -181,18 +192,40 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
@Override @Override
public List<KeyboardCharacterRespVO> selectListWithNotLoginRank() { public List<KeyboardCharacterRespVO> selectListWithNotLoginRank() {
List<KeyboardCharacter> keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>() // 先从缓存获取所有人设列表
.eq(KeyboardCharacter::getDeleted, false) String cacheKey = "character:list:all";
.orderByAsc(KeyboardCharacter::getRank)); List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
if (keyboardCharacters == null) {
// 缓存未命中,从数据库查询
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
.eq(KeyboardCharacter::getDeleted, false)
.orderByAsc(KeyboardCharacter::getRank));
// 缓存到Redis7天过期
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
}
}
return BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class); return BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
} }
@Override @Override
public List<KeyboardCharacterRespVO> selectListByTagWithNotLogin(Long tagId) { public List<KeyboardCharacterRespVO> selectListByTagWithNotLogin(Long tagId) {
List<KeyboardCharacter> keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>() // 先从缓存获取指定标签的人设列表
.eq(KeyboardCharacter::getDeleted, false) String cacheKey = "character:list:tag:" + tagId;
.eq(KeyboardCharacter::getTag, tagId) List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
.orderByDesc(KeyboardCharacter::getRank));
if (keyboardCharacters == null) {
// 缓存未命中,从数据库查询
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
.eq(KeyboardCharacter::getDeleted, false)
.eq(KeyboardCharacter::getTag, tagId)
.orderByDesc(KeyboardCharacter::getRank));
// 缓存到Redis7天过期
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
}
}
return BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class); return BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
} }