refactor: 移除 Spring Cache,初步适配 JetCache

1.移除 ContiNew Starter Spring Cache
2.初步适配 ContiNew Starter JetCache
3.优化缓存键前缀常量的使用
This commit is contained in:
2024-01-14 14:39:41 +08:00
parent 754de79200
commit d4bb39d9b4
15 changed files with 187 additions and 154 deletions

View File

@@ -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;
@@ -43,11 +40,9 @@ import java.util.List;
*/
@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);
@@ -55,7 +50,7 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
}
@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);
@@ -79,7 +74,7 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
}
@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();
}

View File

@@ -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;
@@ -39,6 +33,9 @@ import top.charles7c.continew.admin.system.service.MenuService;
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;
/**
* 菜单业务实现
*
@@ -47,11 +44,10 @@ import top.charles7c.continew.starter.extension.crud.base.BaseServiceImpl;
*/
@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);
@@ -60,7 +56,7 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
}
@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);
@@ -68,7 +64,7 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
}
@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();
@@ -81,7 +77,7 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
}
@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);
@@ -90,7 +86,7 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
}
@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());

View File

@@ -16,16 +16,10 @@
package top.charles7c.continew.admin.system.service.impl;
import java.util.List;
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 com.alicp.jetcache.anno.CacheInvalidate;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
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;
@@ -36,6 +30,8 @@ import top.charles7c.continew.admin.system.model.resp.OptionResp;
import top.charles7c.continew.admin.system.service.OptionService;
import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper;
import java.util.List;
/**
* 参数业务实现
*
@@ -44,7 +40,6 @@ import top.charles7c.continew.starter.data.mybatis.plus.query.QueryHelper;
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.OPTION_KEY_PREFIX)
public class OptionServiceImpl implements OptionService {
private final OptionMapper baseMapper;
@@ -55,13 +50,13 @@ public class OptionServiceImpl implements OptionService {
}
@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();
}

View File

@@ -19,8 +19,8 @@ package top.charles7c.continew.admin.system.service.impl;
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;
@@ -78,7 +78,7 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
}
@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();

View File

@@ -20,12 +20,12 @@ import cn.hutool.core.collection.CollUtil;
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;
@@ -63,7 +63,6 @@ import java.util.Optional;
*/
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = CacheConstants.USER_KEY_PREFIX)
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserResp, UserDetailResp, UserQuery, UserReq> implements UserService, CommonUserService {
@Resource
@@ -264,7 +263,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
}
@Override
@Cacheable(key = "#id")
@Cached(key = "#id", cacheType = CacheType.BOTH, name = CacheConstants.USER_KEY_PREFIX)
public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}