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.KeyboardCharacterRespVO;
|
||||||
import com.yolo.keyborad.model.vo.character.KeyboardUserCharacterVO;
|
import com.yolo.keyborad.model.vo.character.KeyboardUserCharacterVO;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.yolo.keyborad.model.entity.KeyboardCharacter;
|
import com.yolo.keyborad.model.entity.KeyboardCharacter;
|
||||||
@@ -27,6 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
/*
|
/*
|
||||||
* @author: ziin
|
* @author: ziin
|
||||||
@@ -48,7 +50,7 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
private KeyboardUserSortMapper keyboardUserSortMapper;
|
private KeyboardUserSortMapper keyboardUserSortMapper;
|
||||||
|
|
||||||
@Resource(name = "objectRedisTemplate")
|
@Resource(name = "objectRedisTemplate")
|
||||||
private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate;
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KeyboardCharacter getById(java.io.Serializable id) {
|
public KeyboardCharacter getById(java.io.Serializable id) {
|
||||||
@@ -57,7 +59,7 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
if (character == null) {
|
if (character == null) {
|
||||||
character = super.getById(id);
|
character = super.getById(id);
|
||||||
if (character != null) {
|
if (character != null) {
|
||||||
redisTemplate.opsForValue().set(key, character, 7, java.util.concurrent.TimeUnit.DAYS);
|
redisTemplate.opsForValue().set(key, character, 7, TimeUnit.DAYS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return character;
|
return character;
|
||||||
@@ -66,9 +68,21 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
@Override
|
@Override
|
||||||
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
||||||
long userId = StpUtil.getLoginIdAsLong();
|
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);
|
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user