feat(character): 添加Redis缓存支持人设查询
This commit is contained in:
@@ -24,7 +24,7 @@ public class RedisConfig {
|
|||||||
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
|
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
StringRedisTemplate template = new StringRedisTemplate();
|
StringRedisTemplate template = new StringRedisTemplate();
|
||||||
template.setConnectionFactory(connectionFactory);
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
|
||||||
// 设置key序列化方式
|
// 设置key序列化方式
|
||||||
template.setKeySerializer(new StringRedisSerializer());
|
template.setKeySerializer(new StringRedisSerializer());
|
||||||
// 设置value序列化方式
|
// 设置value序列化方式
|
||||||
@@ -33,7 +33,30 @@ public class RedisConfig {
|
|||||||
template.setHashKeySerializer(new StringRedisSerializer());
|
template.setHashKeySerializer(new StringRedisSerializer());
|
||||||
// 设置hash value序列化方式
|
// 设置hash value序列化方式
|
||||||
template.setHashValueSerializer(new StringRedisSerializer());
|
template.setHashValueSerializer(new StringRedisSerializer());
|
||||||
|
|
||||||
|
template.afterPropertiesSet();
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置对象序列化的RedisTemplate
|
||||||
|
* @param connectionFactory Redis连接工厂
|
||||||
|
* @return RedisTemplate实例
|
||||||
|
*/
|
||||||
|
@Bean("objectRedisTemplate")
|
||||||
|
public org.springframework.data.redis.core.RedisTemplate<String, Object> objectRedisTemplate(RedisConnectionFactory connectionFactory) {
|
||||||
|
org.springframework.data.redis.core.RedisTemplate<String, Object> template = new org.springframework.data.redis.core.RedisTemplate<>();
|
||||||
|
template.setConnectionFactory(connectionFactory);
|
||||||
|
|
||||||
|
// 设置key序列化方式
|
||||||
|
template.setKeySerializer(new StringRedisSerializer());
|
||||||
|
// 设置value序列化方式(使用JSON序列化)
|
||||||
|
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||||
|
// 设置hash key序列化方式
|
||||||
|
template.setHashKeySerializer(new StringRedisSerializer());
|
||||||
|
// 设置hash value序列化方式
|
||||||
|
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
|
||||||
|
|
||||||
template.afterPropertiesSet();
|
template.afterPropertiesSet();
|
||||||
return template;
|
return template;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package com.yolo.keyborad.listener;
|
||||||
|
|
||||||
|
import com.yolo.keyborad.model.entity.KeyboardCharacter;
|
||||||
|
import com.yolo.keyborad.service.KeyboardCharacterService;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 人设缓存初始化器
|
||||||
|
* 在应用启动时将所有人设缓存到Redis
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class CharacterCacheInitializer implements ApplicationRunner {
|
||||||
|
|
||||||
|
private static final String CHARACTER_CACHE_KEY = "character:";
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private KeyboardCharacterService characterService;
|
||||||
|
|
||||||
|
@Resource(name = "objectRedisTemplate")
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) {
|
||||||
|
try {
|
||||||
|
log.info("开始缓存人设列表到Redis...");
|
||||||
|
List<KeyboardCharacter> characters = characterService.list();
|
||||||
|
for (KeyboardCharacter character : characters) {
|
||||||
|
String key = CHARACTER_CACHE_KEY + character.getId();
|
||||||
|
redisTemplate.opsForValue().set(key, character, 7, TimeUnit.DAYS);
|
||||||
|
}
|
||||||
|
log.info("人设列表缓存完成,共缓存 {} 条记录", characters.size());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("缓存人设列表失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,6 +36,8 @@ import java.util.stream.Stream;
|
|||||||
@Service
|
@Service
|
||||||
public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterMapper, KeyboardCharacter> implements KeyboardCharacterService{
|
public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterMapper, KeyboardCharacter> implements KeyboardCharacterService{
|
||||||
|
|
||||||
|
private static final String CHARACTER_CACHE_KEY = "character:";
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private KeyboardCharacterMapper keyboardCharacterMapper;
|
private KeyboardCharacterMapper keyboardCharacterMapper;
|
||||||
|
|
||||||
@@ -45,6 +47,22 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
@Resource
|
@Resource
|
||||||
private KeyboardUserSortMapper keyboardUserSortMapper;
|
private KeyboardUserSortMapper keyboardUserSortMapper;
|
||||||
|
|
||||||
|
@Resource(name = "objectRedisTemplate")
|
||||||
|
private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public KeyboardCharacter getById(java.io.Serializable id) {
|
||||||
|
String key = CHARACTER_CACHE_KEY + id;
|
||||||
|
KeyboardCharacter character = (KeyboardCharacter) redisTemplate.opsForValue().get(key);
|
||||||
|
if (character == null) {
|
||||||
|
character = super.getById(id);
|
||||||
|
if (character != null) {
|
||||||
|
redisTemplate.opsForValue().set(key, character, 7, java.util.concurrent.TimeUnit.DAYS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return character;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
||||||
long userId = StpUtil.getLoginIdAsLong();
|
long userId = StpUtil.getLoginIdAsLong();
|
||||||
@@ -61,9 +79,6 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
userCharacter.getCharacterId().equals(character.getId())));
|
userCharacter.getCharacterId().equals(character.getId())));
|
||||||
});
|
});
|
||||||
return keyboardCharacterRespVOS;
|
return keyboardCharacterRespVOS;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user