feat(character): 为键盘人设服务集成Redis缓存

引入RedisTemplate,新增缓存刷新与删除方法,在增删改操作后同步更新缓存,提升读取性能并避免脏数据
This commit is contained in:
2026-03-16 10:09:15 +08:00
parent 9da3f3c0c5
commit 7c1b515e6a
2 changed files with 67 additions and 8 deletions

1
.gitignore vendored
View File

@@ -53,3 +53,4 @@ application-my.yaml
/yolo-ui-app/unpackage/
**/.DS_Store
/.omc/
/AGENTS.md

View File

@@ -1,16 +1,17 @@
package com.yolo.keyboard.service.character;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import java.util.concurrent.TimeUnit;
import com.yolo.keyboard.controller.admin.character.vo.*;
import com.yolo.keyboard.dal.dataobject.character.KeyboardCharacterDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import com.yolo.keyboard.dal.mysql.character.KeyboardCharacterMapper;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.CHARACTER_NOT_EXISTS;
@@ -25,15 +26,24 @@ import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.CHARACTER_
@Validated
public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
private static final String CHARACTER_CACHE_KEY_PREFIX = "character:";
private static final long CHARACTER_CACHE_TTL_MINUTES = 5L;
@Resource
private KeyboardCharacterMapper characterMapper;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Override
public Long createCharacter(KeyboardCharacterSaveReqVO createReqVO) {
// 插入
KeyboardCharacterDO character = BeanUtils.toBean(createReqVO, KeyboardCharacterDO.class);
characterMapper.insert(character);
// 同步刷新 Redis 缓存,保证缓存内容与数据库最终落库数据一致
refreshCharacterCache(character.getId());
// 返回
return character.getId();
}
@@ -45,6 +55,9 @@ public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
// 更新
KeyboardCharacterDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardCharacterDO.class);
characterMapper.updateById(updateObj);
// 同步刷新 Redis 缓存,避免读取到旧数据
refreshCharacterCache(updateReqVO.getId());
}
@Override
@@ -53,13 +66,19 @@ public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
validateCharacterExists(id);
// 删除
characterMapper.deleteById(id);
// 同步删除 Redis 缓存,避免读取到已删除数据
deleteCharacterCache(id);
}
@Override
public void deleteCharacterListByIds(List<Long> ids) {
public void deleteCharacterListByIds(List<Long> ids) {
// 删除
characterMapper.deleteByIds(ids);
}
// 同步删除 Redis 缓存,避免批量删除后残留脏数据
deleteCharacterCaches(ids);
}
private void validateCharacterExists(Long id) {
@@ -78,4 +97,43 @@ public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
return characterMapper.selectPage(pageReqVO);
}
private void refreshCharacterCache(Long characterId) {
if (characterId == null) {
return;
}
String cacheKey = CHARACTER_CACHE_KEY_PREFIX + characterId;
KeyboardCharacterDO latestCharacter = characterMapper.selectById(characterId);
if (latestCharacter == null) {
redisTemplate.delete(cacheKey);
return;
}
redisTemplate.opsForValue().set(cacheKey, latestCharacter, CHARACTER_CACHE_TTL_MINUTES, TimeUnit.MINUTES);
}
private void deleteCharacterCache(Long characterId) {
if (characterId == null) {
return;
}
redisTemplate.delete(buildCharacterCacheKey(characterId));
}
private void deleteCharacterCaches(List<Long> characterIds) {
if (characterIds == null || characterIds.isEmpty()) {
return;
}
Set<String> cacheKeys = new HashSet<>();
for (Long characterId : characterIds) {
if (characterId != null) {
cacheKeys.add(buildCharacterCacheKey(characterId));
}
}
if (!cacheKeys.isEmpty()) {
redisTemplate.delete(cacheKeys);
}
}
private String buildCharacterCacheKey(Long characterId) {
return CHARACTER_CACHE_KEY_PREFIX + characterId;
}
}