From 2eaf9a37d5e6c046d9ddfb916bdfd7352064f5cb Mon Sep 17 00:00:00 2001 From: ziin Date: Mon, 29 Dec 2025 15:43:23 +0800 Subject: [PATCH] =?UTF-8?q?feat(service):=20=E4=B8=BA=E4=B8=BB=E9=A2=98?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=A2=9E=E5=8A=A0=20Redis=20=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=90=8C=E6=AD=A5=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在新增、更新、删除主题时,按风格维度刷新 Redis 缓存,保持缓存与数据库一致,提升读取性能。 --- .../themes/KeyboardThemesServiceImpl.java | 89 ++++++++++++++++--- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/keyboard-server/src/main/java/com/yolo/keyboard/service/themes/KeyboardThemesServiceImpl.java b/keyboard-server/src/main/java/com/yolo/keyboard/service/themes/KeyboardThemesServiceImpl.java index 55b0f8f..bf1013b 100644 --- a/keyboard-server/src/main/java/com/yolo/keyboard/service/themes/KeyboardThemesServiceImpl.java +++ b/keyboard-server/src/main/java/com/yolo/keyboard/service/themes/KeyboardThemesServiceImpl.java @@ -5,21 +5,18 @@ import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesPageReqVO; import com.yolo.keyboard.controller.admin.themes.vo.KeyboardThemesSaveReqVO; import com.yolo.keyboard.dal.dataobject.themes.KeyboardThemesDO; import com.yolo.keyboard.dal.mysql.themes.KeyboardThemesMapper; -import org.springframework.stereotype.Service; -import jakarta.annotation.Resource; -import org.springframework.validation.annotation.Validated; -import org.springframework.transaction.annotation.Transactional; - -import java.util.*; - import com.yolo.keyboard.framework.common.pojo.PageResult; -import com.yolo.keyboard.framework.common.pojo.PageParam; import com.yolo.keyboard.framework.common.util.object.BeanUtils; +import jakarta.annotation.Resource; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception; -import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList; -import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList; import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.THEMES_NOT_EXISTS; /** @@ -31,15 +28,25 @@ import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.THEMES_NOT @Validated public class KeyboardThemesServiceImpl implements KeyboardThemesService { + private static final String THEME_STYLE_KEY_PREFIX = "theme:style:"; + private static final String THEME_STYLE_ALL_KEY = "theme:style:all"; + private static final Long ALL_STYLE_ID = 9999L; + @Resource private KeyboardThemesMapper themesMapper; + @Resource + private RedisTemplate redisTemplate; + @Override public Long createThemes(KeyboardThemesSaveReqVO createReqVO) { // 插入 KeyboardThemesDO themes = BeanUtils.toBean(createReqVO, KeyboardThemesDO.class); themesMapper.insert(themes); + // 同步更新 Redis 缓存 + refreshThemeStyleCache(themes.getThemeStyle()); + // 返回 return themes.getId(); } @@ -47,25 +54,57 @@ public class KeyboardThemesServiceImpl implements KeyboardThemesService { @Override public void updateThemes(KeyboardThemesSaveReqVO updateReqVO) { // 校验存在 + KeyboardThemesDO oldTheme = themesMapper.selectById(updateReqVO.getId()); validateThemesExists(updateReqVO.getId()); + // 更新 KeyboardThemesDO updateObj = BeanUtils.toBean(updateReqVO, KeyboardThemesDO.class); themesMapper.updateById(updateObj); + + // 同步更新 Redis 缓存(如果风格变更,需要刷新新旧两个风格的缓存) + refreshThemeStyleCache(updateReqVO.getThemeStyle()); + if (oldTheme != null && oldTheme.getThemeStyle() != null + && !oldTheme.getThemeStyle().equals(updateReqVO.getThemeStyle())) { + refreshThemeStyleCache(oldTheme.getThemeStyle()); + } } @Override public void deleteThemes(Long id) { // 校验存在 + KeyboardThemesDO theme = themesMapper.selectById(id); validateThemesExists(id); + // 删除 themesMapper.deleteById(id); + + // 同步更新 Redis 缓存 + if (theme != null && theme.getThemeStyle() != null) { + refreshThemeStyleCache(theme.getThemeStyle()); + } } @Override - public void deleteThemesListByIds(List ids) { + public void deleteThemesListByIds(List ids) { + // 获取要删除的主题列表,记录其风格 + List themes = themesMapper.selectBatchIds(ids); + Set styleIds = new HashSet<>(); + if (CollUtil.isNotEmpty(themes)) { + for (KeyboardThemesDO theme : themes) { + if (theme.getThemeStyle() != null) { + styleIds.add(theme.getThemeStyle()); + } + } + } + // 删除 themesMapper.deleteByIds(ids); + + // 同步更新 Redis 缓存 + for (Long styleId : styleIds) { + refreshThemeStyleCache(styleId); } + } private void validateThemesExists(Long id) { @@ -84,4 +123,32 @@ public class KeyboardThemesServiceImpl implements KeyboardThemesService { return themesMapper.selectPage(pageReqVO); } + /** + * 刷新主题风格缓存 + * + * @param styleId 风格ID + */ + private void refreshThemeStyleCache(Long styleId) { + if (styleId == null) { + return; + } + + // 1. 刷新指定风格的缓存 + String styleKey = THEME_STYLE_KEY_PREFIX + styleId; + List themesByStyle = themesMapper.selectList(KeyboardThemesDO::getThemeStyle, styleId); + if (CollUtil.isNotEmpty(themesByStyle)) { + redisTemplate.opsForValue().set(styleKey, themesByStyle); + } else { + redisTemplate.delete(styleKey); + } + + // 2. 刷新全部主题缓存(theme:style:all) + List allThemes = themesMapper.selectList(); + if (CollUtil.isNotEmpty(allThemes)) { + redisTemplate.opsForValue().set(THEME_STYLE_ALL_KEY, allThemes); + } else { + redisTemplate.delete(THEME_STYLE_ALL_KEY); + } + } + } \ No newline at end of file