Skip to content

Commit

Permalink
refactor: 移除 Spring Cache,初步适配 JetCache
Browse files Browse the repository at this point in the history
1.移除 ContiNew Starter Spring Cache
2.初步适配 ContiNew Starter JetCache
3.优化缓存键前缀常量的使用
  • Loading branch information
Charles7c committed Jan 14, 2024
1 parent 754de79 commit d4bb39d
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 153 deletions.
4 changes: 2 additions & 2 deletions continew-admin-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@
<artifactId>continew-starter-data-mybatis-plus</artifactId>
</dependency>

<!-- ContiNew Starter 缓存模块 - Spring Cache -->
<!-- ContiNew Starter 缓存模块 - JetCache -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-cache-springcache</artifactId>
<artifactId>continew-starter-cache-jetcache</artifactId>
</dependency>

<!-- ContiNew Starter 消息模块 - 邮件 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import top.charles7c.continew.starter.core.constant.StringConstants;

/**
* 缓存相关常量
Expand All @@ -28,6 +29,11 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CacheConstants {

/**
* 分隔符
*/
public static final String DELIMITER = StringConstants.COLON;

/**
* 登录用户键
*/
Expand All @@ -36,35 +42,35 @@ public class CacheConstants {
/**
* 验证码键前缀
*/
public static final String CAPTCHA_KEY_PREFIX = "CAPTCHA";
public static final String CAPTCHA_KEY_PREFIX = "CAPTCHA" + DELIMITER;

/**
* 限流键前缀
*/
public static final String LIMIT_KEY_PREFIX = "LIMIT";
public static final String LIMIT_KEY_PREFIX = "LIMIT" + DELIMITER;

/**
* 用户缓存键前缀
*/
public static final String USER_KEY_PREFIX = "USER";
public static final String USER_KEY_PREFIX = "USER" + DELIMITER;

/**
* 菜单缓存键前缀
*/
public static final String MENU_KEY_PREFIX = "MENU";
public static final String MENU_KEY_PREFIX = "MENU" + DELIMITER;

/**
* 字典缓存键前缀
*/
public static final String DICT_KEY_PREFIX = "DICT";
public static final String DICT_KEY_PREFIX = "DICT" + DELIMITER;

/**
* 参数缓存键前缀
*/
public static final String OPTION_KEY_PREFIX = "OPTION";
public static final String OPTION_KEY_PREFIX = "OPTION" + DELIMITER;

/**
* 仪表盘缓存键前缀
*/
public static final String DASHBOARD_KEY_PREFIX = "DASHBOARD";
public static final String DASHBOARD_KEY_PREFIX = "DASHBOARD" + DELIMITER;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@

package top.charles7c.continew.admin.monitor.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;

import com.alicp.jetcache.anno.CachePenetrationProtect;
import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.monitor.model.resp.DashboardAccessTrendResp;
import top.charles7c.continew.admin.monitor.model.resp.DashboardGeoDistributionResp;
Expand All @@ -39,6 +34,10 @@
import top.charles7c.continew.admin.system.model.resp.DashboardAnnouncementResp;
import top.charles7c.continew.admin.system.service.AnnouncementService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
* 仪表盘业务实现
*
Expand All @@ -47,7 +46,6 @@
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.DASHBOARD_KEY_PREFIX)
public class DashboardServiceImpl implements DashboardService {

private final LogService logService;
Expand All @@ -67,7 +65,9 @@ public DashboardTotalResp getTotal() {
}

@Override
@Cacheable(key = "#days")
@CachePenetrationProtect
@CacheRefresh(refresh = 7200)
@Cached(key = "#days", cacheType = CacheType.BOTH, name = CacheConstants.DASHBOARD_KEY_PREFIX)
public List<DashboardAccessTrendResp> listAccessTrend(Integer days) {
return logService.listDashboardAccessTrend(days);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package top.charles7c.continew.admin.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.model.resp.LabelValueResp;
import top.charles7c.continew.admin.system.mapper.DictItemMapper;
import top.charles7c.continew.admin.system.model.entity.DictItemDO;
Expand All @@ -43,19 +40,17 @@
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.DICT_KEY_PREFIX)
public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictItemDO, DictItemResp, DictItemDetailResp, DictItemQuery, DictItemReq> implements DictItemService {

@Override
@CacheEvict(allEntries = true)
public Long add(DictItemReq req) {
String value = req.getValue();
CheckUtils.throwIf(this.isValueExists(value, null, req.getDictId()), "新增失败,字典值 [{}] 已存在", value);
return super.add(req);
}

@Override
@CacheEvict(allEntries = true)
// @CacheInvalidate(key = "#id", name = CacheConstants.DICT_KEY_PREFIX)
public void update(DictItemReq req, Long id) {
String value = req.getValue();
CheckUtils.throwIf(this.isValueExists(value, id, req.getDictId()), "修改失败,字典值 [{}] 已存在", value);
Expand All @@ -79,7 +74,7 @@ public List<LabelValueResp> listByDictCode(String dictCode) {
}

@Override
@CacheEvict(allEntries = true)
// @CacheInvalidate(key = "#dictIds", name = CacheConstants.DICT_KEY_PREFIX, multi = true)
public void deleteByDictIds(List<Long> dictIds) {
baseMapper.lambdaUpdate().in(DictItemDO::getDictId, dictIds).remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@

package top.charles7c.continew.admin.system.service.impl;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.alicp.jetcache.anno.CacheInvalidate;
import com.alicp.jetcache.anno.Cached;
import lombok.RequiredArgsConstructor;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.bean.BeanUtil;

import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.enums.DisEnableStatusEnum;
import top.charles7c.continew.admin.system.mapper.MenuMapper;
Expand All @@ -39,6 +33,9 @@
import top.charles7c.continew.starter.core.util.validate.CheckUtils;
import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;

import java.util.List;
import java.util.Set;

/**
* 菜单业务实现
*
Expand All @@ -47,11 +44,10 @@
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.MENU_KEY_PREFIX)
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuResp, MenuResp, MenuQuery, MenuReq> implements MenuService {

@Override
@CacheEvict(allEntries = true)
@CacheInvalidate(key = "'ALL'", name = CacheConstants.MENU_KEY_PREFIX)
public Long add(MenuReq req) {
String title = req.getTitle();
CheckUtils.throwIf(this.isNameExists(title, req.getParentId(), null), "新增失败,[{}] 已存在", title);
Expand All @@ -60,15 +56,15 @@ public Long add(MenuReq req) {
}

@Override
@CacheEvict(allEntries = true)
@CacheInvalidate(key = "#id", name = CacheConstants.MENU_KEY_PREFIX)
public void update(MenuReq req, Long id) {
String title = req.getTitle();
CheckUtils.throwIf(this.isNameExists(title, req.getParentId(), id), "修改失败,[{}] 已存在", title);
super.update(req, id);
}

@Override
@CacheEvict(allEntries = true)
@CacheInvalidate(key = "#ids", name = CacheConstants.MENU_KEY_PREFIX, multi = true)
@Transactional(rollbackFor = Exception.class)
public void delete(List<Long> ids) {
baseMapper.lambdaUpdate().in(MenuDO::getParentId, ids).remove();
Expand All @@ -81,7 +77,7 @@ public Set<String> listPermissionByUserId(Long userId) {
}

@Override
@Cacheable(key = "#roleCode")
@Cached(key = "#roleCode", name = CacheConstants.MENU_KEY_PREFIX)
public List<MenuResp> listByRoleCode(String roleCode) {
List<MenuDO> menuList = baseMapper.selectListByRoleCode(roleCode);
List<MenuResp> list = BeanUtil.copyToList(menuList, MenuResp.class);
Expand All @@ -90,7 +86,7 @@ public List<MenuResp> listByRoleCode(String roleCode) {
}

@Override
@Cacheable(key = "'ALL'")
@Cached(key = "'ALL'", name = CacheConstants.MENU_KEY_PREFIX)
public List<MenuResp> list() {
MenuQuery menuQuery = new MenuQuery();
menuQuery.setStatus(DisEnableStatusEnum.ENABLE.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@

package top.charles7c.continew.admin.system.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.alicp.jetcache.anno.CacheInvalidate;
import lombok.RequiredArgsConstructor;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import cn.hutool.core.bean.BeanUtil;

import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.system.mapper.OptionMapper;
import top.charles7c.continew.admin.system.model.entity.OptionDO;
Expand All @@ -36,6 +30,8 @@
import top.charles7c.continew.admin.system.service.OptionService;
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper;

import java.util.List;

/**
* 参数业务实现
*
Expand All @@ -44,7 +40,6 @@
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.OPTION_KEY_PREFIX)
public class OptionServiceImpl implements OptionService {

private final OptionMapper baseMapper;
Expand All @@ -55,13 +50,13 @@ public List<OptionResp> list(OptionQuery query) {
}

@Override
@CacheEvict(allEntries = true)
// @CacheInvalidate(key = "#req.code", name = CacheConstants.OPTION_KEY_PREFIX, multi = true)
public void update(List<OptionReq> req) {
baseMapper.updateBatchById(BeanUtil.copyToList(req, OptionDO.class));
}

@Override
@CacheEvict(allEntries = true)
@CacheInvalidate(key = "#req.code", name = CacheConstants.OPTION_KEY_PREFIX, multi = true)
public void resetValue(OptionResetValueReq req) {
baseMapper.lambdaUpdate().set(OptionDO::getValue, null).in(OptionDO::getCode, req.getCode()).update();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alicp.jetcache.anno.CacheInvalidate;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.charles7c.continew.admin.auth.service.OnlineUserService;
Expand Down Expand Up @@ -78,7 +78,7 @@ public Long add(RoleReq req) {
}

@Override
@CacheEvict(cacheNames = CacheConstants.MENU_KEY_PREFIX, key = "#req.code == 'admin' ? 'ALL' : #req.code")
@CacheInvalidate(key = "#req.code == 'admin' ? 'ALL' : #req.code", name = CacheConstants.MENU_KEY_PREFIX)
@Transactional(rollbackFor = Exception.class)
public void update(RoleReq req, Long id) {
String name = req.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -63,7 +63,6 @@
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.USER_KEY_PREFIX)
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserResp, UserDetailResp, UserQuery, UserReq> implements UserService, CommonUserService {

@Resource
Expand Down Expand Up @@ -264,7 +263,7 @@ public Long countByDeptIds(List<Long> deptIds) {
}

@Override
@Cacheable(key = "#id")
@Cached(key = "#id", cacheType = CacheType.BOTH, name = CacheConstants.USER_KEY_PREFIX)
public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.URLUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -28,7 +29,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties;
Expand All @@ -42,11 +42,11 @@
* @since 2022/12/8 23:15
*/
@Slf4j
@EnableCaching
@EnableFileStorage
@RestController
@EnableFileStorage
@SpringBootApplication
@RequiredArgsConstructor
@EnableMethodCache(basePackages = "top.charles7c.continew.admin")
public class ContiNewAdminApplication implements ApplicationRunner {

private final ProjectProperties projectProperties;
Expand Down
Loading

0 comments on commit d4bb39d

Please sign in to comment.