From 7c1b515e6a2e9f9c3609fc67c4bf570f8ac1d94c Mon Sep 17 00:00:00 2001 From: ziin Date: Mon, 16 Mar 2026 10:09:15 +0800 Subject: [PATCH] =?UTF-8?q?feat(character):=20=E4=B8=BA=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E4=BA=BA=E8=AE=BE=E6=9C=8D=E5=8A=A1=E9=9B=86=E6=88=90Redis?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 引入RedisTemplate,新增缓存刷新与删除方法,在增删改操作后同步更新缓存,提升读取性能并避免脏数据 --- .gitignore | 1 + .../KeyboardCharacterServiceImpl.java | 74 +++++++++++++++++-- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index d10bf43..8f25928 100644 --- a/.gitignore +++ b/.gitignore @@ -53,3 +53,4 @@ application-my.yaml /yolo-ui-app/unpackage/ **/.DS_Store /.omc/ +/AGENTS.md diff --git a/keyboard-server/src/main/java/com/yolo/keyboard/service/character/KeyboardCharacterServiceImpl.java b/keyboard-server/src/main/java/com/yolo/keyboard/service/character/KeyboardCharacterServiceImpl.java index 155e5a4..b5c54f6 100644 --- a/keyboard-server/src/main/java/com/yolo/keyboard/service/character/KeyboardCharacterServiceImpl.java +++ b/keyboard-server/src/main/java/com/yolo/keyboard/service/character/KeyboardCharacterServiceImpl.java @@ -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 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 ids) { + public void deleteCharacterListByIds(List 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); } -} \ No newline at end of file + 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 characterIds) { + if (characterIds == null || characterIds.isEmpty()) { + return; + } + Set 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; + } + +}