feat(service): 为所有人设列表添加Redis缓存
在 selectListWithRank 中先读缓存,未命中再查库并写入7天过期缓存,减少数据库压力。
This commit is contained in:
@@ -18,6 +18,7 @@ import com.yolo.keyborad.model.entity.KeyboardUserCharacter;
|
||||
import com.yolo.keyborad.model.vo.character.KeyboardCharacterRespVO;
|
||||
import com.yolo.keyborad.model.vo.character.KeyboardUserCharacterVO;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yolo.keyborad.model.entity.KeyboardCharacter;
|
||||
@@ -27,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Stream;
|
||||
/*
|
||||
* @author: ziin
|
||||
@@ -48,7 +50,7 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
||||
private KeyboardUserSortMapper keyboardUserSortMapper;
|
||||
|
||||
@Resource(name = "objectRedisTemplate")
|
||||
private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate;
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Override
|
||||
public KeyboardCharacter getById(java.io.Serializable id) {
|
||||
@@ -57,7 +59,7 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
||||
if (character == null) {
|
||||
character = super.getById(id);
|
||||
if (character != null) {
|
||||
redisTemplate.opsForValue().set(key, character, 7, java.util.concurrent.TimeUnit.DAYS);
|
||||
redisTemplate.opsForValue().set(key, character, 7, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
return character;
|
||||
@@ -66,9 +68,21 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
||||
@Override
|
||||
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
||||
long userId = StpUtil.getLoginIdAsLong();
|
||||
List<KeyboardCharacter> keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
|
||||
.eq(KeyboardCharacter::getDeleted, false)
|
||||
.orderByAsc(KeyboardCharacter::getRank));
|
||||
|
||||
// 先从缓存获取所有人设列表
|
||||
String cacheKey = "character:list:all";
|
||||
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));
|
||||
// 缓存到Redis,7天过期
|
||||
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
|
||||
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
|
||||
}
|
||||
}
|
||||
|
||||
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user