feat(character): 为键盘人设服务集成Redis缓存
引入RedisTemplate,新增缓存刷新与删除方法,在增删改操作后同步更新缓存,提升读取性能并避免脏数据
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -53,3 +53,4 @@ application-my.yaml
|
|||||||
/yolo-ui-app/unpackage/
|
/yolo-ui-app/unpackage/
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
/.omc/
|
/.omc/
|
||||||
|
/AGENTS.md
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
package com.yolo.keyboard.service.character;
|
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.*;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import com.yolo.keyboard.controller.admin.character.vo.*;
|
import com.yolo.keyboard.controller.admin.character.vo.*;
|
||||||
import com.yolo.keyboard.dal.dataobject.character.KeyboardCharacterDO;
|
import com.yolo.keyboard.dal.dataobject.character.KeyboardCharacterDO;
|
||||||
import com.yolo.keyboard.framework.common.pojo.PageResult;
|
import com.yolo.keyboard.framework.common.pojo.PageResult;
|
||||||
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
|
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
|
||||||
|
|
||||||
import com.yolo.keyboard.dal.mysql.character.KeyboardCharacterMapper;
|
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.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.CHARACTER_NOT_EXISTS;
|
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
|
@Validated
|
||||||
public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
|
public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
|
||||||
|
|
||||||
|
private static final String CHARACTER_CACHE_KEY_PREFIX = "character:";
|
||||||
|
private static final long CHARACTER_CACHE_TTL_MINUTES = 5L;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private KeyboardCharacterMapper characterMapper;
|
private KeyboardCharacterMapper characterMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisTemplate<String, Object> redisTemplate;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Long createCharacter(KeyboardCharacterSaveReqVO createReqVO) {
|
public Long createCharacter(KeyboardCharacterSaveReqVO createReqVO) {
|
||||||
// 插入
|
// 插入
|
||||||
KeyboardCharacterDO character = BeanUtils.toBean(createReqVO, KeyboardCharacterDO.class);
|
KeyboardCharacterDO character = BeanUtils.toBean(createReqVO, KeyboardCharacterDO.class);
|
||||||
characterMapper.insert(character);
|
characterMapper.insert(character);
|
||||||
|
|
||||||
|
// 同步刷新 Redis 缓存,保证缓存内容与数据库最终落库数据一致
|
||||||
|
refreshCharacterCache(character.getId());
|
||||||
|
|
||||||
// 返回
|
// 返回
|
||||||
return character.getId();
|
return character.getId();
|
||||||
}
|
}
|
||||||
@@ -45,6 +55,9 @@ public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
|
|||||||
// 更新
|
// 更新
|
||||||
KeyboardCharacterDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardCharacterDO.class);
|
KeyboardCharacterDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardCharacterDO.class);
|
||||||
characterMapper.updateById(updateObj);
|
characterMapper.updateById(updateObj);
|
||||||
|
|
||||||
|
// 同步刷新 Redis 缓存,避免读取到旧数据
|
||||||
|
refreshCharacterCache(updateReqVO.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -53,12 +66,18 @@ public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
|
|||||||
validateCharacterExists(id);
|
validateCharacterExists(id);
|
||||||
// 删除
|
// 删除
|
||||||
characterMapper.deleteById(id);
|
characterMapper.deleteById(id);
|
||||||
|
|
||||||
|
// 同步删除 Redis 缓存,避免读取到已删除数据
|
||||||
|
deleteCharacterCache(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void deleteCharacterListByIds(List<Long> ids) {
|
public void deleteCharacterListByIds(List<Long> ids) {
|
||||||
// 删除
|
// 删除
|
||||||
characterMapper.deleteByIds(ids);
|
characterMapper.deleteByIds(ids);
|
||||||
|
|
||||||
|
// 同步删除 Redis 缓存,避免批量删除后残留脏数据
|
||||||
|
deleteCharacterCaches(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -78,4 +97,43 @@ public class KeyboardCharacterServiceImpl implements KeyboardCharacterService {
|
|||||||
return characterMapper.selectPage(pageReqVO);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user