feat(character): 添加Redis缓存支持人设查询

This commit is contained in:
2025-12-17 16:25:05 +08:00
parent 27a8911b7f
commit 0156156440
3 changed files with 88 additions and 5 deletions

View File

@@ -24,7 +24,7 @@ public class RedisConfig {
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(connectionFactory);
// 设置key序列化方式
template.setKeySerializer(new StringRedisSerializer());
// 设置value序列化方式
@@ -33,7 +33,30 @@ public class RedisConfig {
template.setHashKeySerializer(new StringRedisSerializer());
// 设置hash value序列化方式
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();
return template;
}

View File

@@ -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);
}
}
}

View File

@@ -36,6 +36,8 @@ import java.util.stream.Stream;
@Service
public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterMapper, KeyboardCharacter> implements KeyboardCharacterService{
private static final String CHARACTER_CACHE_KEY = "character:";
@Resource
private KeyboardCharacterMapper keyboardCharacterMapper;
@@ -45,6 +47,22 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
@Resource
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
public List<KeyboardCharacterRespVO> selectListWithRank() {
long userId = StpUtil.getLoginIdAsLong();
@@ -61,9 +79,6 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
userCharacter.getCharacterId().equals(character.getId())));
});
return keyboardCharacterRespVOS;
}
@Override