refactor: 优化部分 JetCache 缓存定义

This commit is contained in:
2024-01-14 19:54:35 +08:00
parent d4bb39d9b4
commit 1b7aa9db0c
10 changed files with 44 additions and 49 deletions

View File

@@ -16,10 +16,7 @@
package top.charles7c.continew.admin.system.service;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import top.charles7c.continew.admin.system.model.entity.UserDO;
import top.charles7c.continew.admin.system.model.query.UserQuery;
import top.charles7c.continew.admin.system.model.req.UserBasicInfoUpdateReq;
@@ -29,6 +26,8 @@ import top.charles7c.continew.admin.system.model.resp.UserDetailResp;
import top.charles7c.continew.admin.system.model.resp.UserResp;
import top.charles7c.continew.starter.extension.crud.base.BaseService;
import java.util.List;
/**
* 用户业务接口
*
@@ -57,10 +56,10 @@ public interface UserService extends BaseService<UserResp, UserDetailResp, UserQ
/**
* 修改基础信息
*
* @param updateReq 修改信息
* @param id ID
* @param req 修改信息
* @param id ID
*/
void updateBasicInfo(UserBasicInfoUpdateReq updateReq, Long id);
void updateBasicInfo(UserBasicInfoUpdateReq req, Long id);
/**
* 修改密码

View File

@@ -50,7 +50,6 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
}
@Override
// @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);
@@ -74,7 +73,6 @@ public class DictItemServiceImpl extends BaseServiceImpl<DictItemMapper, DictIte
}
@Override
// @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

@@ -56,7 +56,7 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
}
@Override
@CacheInvalidate(key = "#id", name = CacheConstants.MENU_KEY_PREFIX)
@CacheInvalidate(key = "'ALL'", 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);
@@ -64,8 +64,8 @@ public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, MenuDO, MenuRes
}
@Override
@CacheInvalidate(key = "#ids", name = CacheConstants.MENU_KEY_PREFIX, multi = true)
@Transactional(rollbackFor = Exception.class)
@CacheInvalidate(key = "'ALL'", name = CacheConstants.MENU_KEY_PREFIX)
public void delete(List<Long> ids) {
baseMapper.lambdaUpdate().in(MenuDO::getParentId, ids).remove();
super.delete(ids);

View File

@@ -50,7 +50,6 @@ public class OptionServiceImpl implements OptionService {
}
@Override
// @CacheInvalidate(key = "#req.code", name = CacheConstants.OPTION_KEY_PREFIX, multi = true)
public void update(List<OptionReq> req) {
baseMapper.updateBatchById(BeanUtil.copyToList(req, OptionDO.class));
}

View File

@@ -78,8 +78,8 @@ public class RoleServiceImpl extends BaseServiceImpl<RoleMapper, RoleDO, RoleRes
}
@Override
@CacheInvalidate(key = "#req.code == 'admin' ? 'ALL' : #req.code", name = CacheConstants.MENU_KEY_PREFIX)
@Transactional(rollbackFor = Exception.class)
@CacheInvalidate(key = "#req.code == 'admin' ? 'ALL' : #req.code", name = CacheConstants.MENU_KEY_PREFIX)
public void update(RoleReq req, Long id) {
String name = req.getName();
CheckUtils.throwIf(this.isNameExists(name, id), "修改失败,[{}] 已存在", name);

View File

@@ -20,7 +20,9 @@ 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.CacheInvalidate;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CacheUpdate;
import com.alicp.jetcache.anno.Cached;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
@@ -103,6 +105,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
@Override
@Transactional(rollbackFor = Exception.class)
@CacheUpdate(key = "#id", value = "#req.nickname", name = CacheConstants.USER_KEY_PREFIX)
public void update(UserReq req, Long id) {
String username = req.getUsername();
CheckUtils.throwIf(this.isNameExists(username, id), "修改失败,[{}] 已存在", username);
@@ -129,6 +132,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
@Override
@Transactional(rollbackFor = Exception.class)
@CacheInvalidate(key = "#ids", name = CacheConstants.USER_KEY_PREFIX, multi = true)
public void delete(List<Long> ids) {
CheckUtils.throwIf(CollUtil.contains(ids, LoginHelper.getUserId()), "不允许删除当前用户");
List<UserDO> list = baseMapper.lambdaQuery()
@@ -178,11 +182,12 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
}
@Override
public void updateBasicInfo(UserBasicInfoUpdateReq updateReq, Long id) {
@CacheUpdate(key = "#id", value = "#req.nickname", name = CacheConstants.USER_KEY_PREFIX)
public void updateBasicInfo(UserBasicInfoUpdateReq req, Long id) {
super.getById(id);
baseMapper.lambdaUpdate()
.set(UserDO::getNickname, updateReq.getNickname())
.set(UserDO::getGender, updateReq.getGender())
.set(UserDO::getNickname, req.getNickname())
.set(UserDO::getGender, req.getGender())
.eq(UserDO::getId, id)
.update();
}
@@ -263,7 +268,7 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserDO, UserRes
}
@Override
@Cached(key = "#id", cacheType = CacheType.BOTH, name = CacheConstants.USER_KEY_PREFIX)
@Cached(key = "#id", cacheType = CacheType.BOTH, name = CacheConstants.USER_KEY_PREFIX, syncLocal = true)
public String getNicknameById(Long id) {
return baseMapper.selectNicknameById(id);
}