Skip to content

Commit

Permalink
refactor: 优化部分 JetCache 缓存定义
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jan 14, 2024
1 parent d4bb39d commit 1b7aa9d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@

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;
import top.charles7c.continew.admin.monitor.model.resp.DashboardPopularModuleResp;
Expand Down Expand Up @@ -65,9 +60,6 @@ public DashboardTotalResp getTotal() {
}

@Override
@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 @@ -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;
Expand All @@ -29,6 +26,8 @@
import top.charles7c.continew.admin.system.model.resp.UserResp;
import top.charles7c.continew.starter.extension.crud.base.BaseService;

import java.util.List;

/**
* 用户业务接口
*
Expand Down Expand Up @@ -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);

/**
* 修改密码
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public Long add(DictItemReq req) {
}

@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);
Expand All @@ -74,7 +73,6 @@ public List<LabelValueResp> listByDictCode(String dictCode) {
}

@Override
// @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 @@ -56,16 +56,16 @@ public Long add(MenuReq req) {
}

@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);
super.update(req, id);
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public List<OptionResp> list(OptionQuery query) {
}

@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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public Long add(RoleReq req) {
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
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;
Expand Down Expand Up @@ -103,6 +105,7 @@ public Long add(UserReq req) {

@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);
Expand All @@ -129,6 +132,7 @@ public void update(UserReq req, Long id) {

@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()
Expand Down Expand Up @@ -178,11 +182,12 @@ public String uploadAvatar(MultipartFile avatarFile, Long id) {
}

@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();
}
Expand Down Expand Up @@ -263,7 +268,7 @@ public Long countByDeptIds(List<Long> deptIds) {
}

@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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import com.alicp.jetcache.anno.CachePenetrationProtect;
import com.alicp.jetcache.anno.CacheRefresh;
import com.alicp.jetcache.anno.Cached;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -103,6 +105,8 @@ public R<List<LabelValueResp<Long>>> listRoleDict(RoleQuery query, SortQuery sor
@Operation(summary = "查询字典", description = "查询字典列表")
@Parameter(name = "code", description = "字典编码", example = "announcement_type", in = ParameterIn.PATH)
@GetMapping("/dict/{code}")
@CachePenetrationProtect
@CacheRefresh(refresh = 3600, stopRefreshAfterLastAccess = 7200)
@Cached(key = "#code", name = CacheConstants.DICT_KEY_PREFIX)
public R<List<LabelValueResp>> listDict(@PathVariable String code) {
Optional<Class<?>> enumClass = this.getEnumClassByName(code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@

package top.charles7c.continew.admin.webapi.common;

import java.util.List;

import lombok.RequiredArgsConstructor;

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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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;
import top.charles7c.continew.admin.monitor.model.resp.DashboardPopularModuleResp;
Expand All @@ -41,6 +41,8 @@
import top.charles7c.continew.starter.extension.crud.model.resp.R;
import top.charles7c.continew.starter.log.common.annotation.Log;

import java.util.List;

/**
* 仪表盘 API
*
Expand All @@ -66,6 +68,9 @@ public R<DashboardTotalResp> getTotal() {
@Operation(summary = "查询访问趋势信息", description = "查询访问趋势信息")
@Parameter(name = "days", description = "日期数", example = "30", in = ParameterIn.PATH)
@GetMapping("/access/trend/{days}")
@CachePenetrationProtect
@CacheRefresh(refresh = 7200)
@Cached(key = "#days", cacheType = CacheType.BOTH, name = CacheConstants.DASHBOARD_KEY_PREFIX, syncLocal = true)
public R<List<DashboardAccessTrendResp>> listAccessTrend(@PathVariable Integer days) {
ValidationUtils.throwIf(7 != days && 30 != days, "仅支持查询近 7/30 天访问趋势信息");
return R.ok(dashboardService.listAccessTrend(days));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,21 @@

package top.charles7c.continew.admin.webapi.system;

import java.util.List;
import java.util.stream.Collectors;

import jakarta.validation.constraints.NotNull;

import lombok.RequiredArgsConstructor;

import cn.hutool.core.util.ReUtil;
import com.xkcoding.justauth.AuthRequestFactory;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.xkcoding.justauth.AuthRequestFactory;

import cn.hutool.core.util.ReUtil;

import top.charles7c.continew.admin.common.constant.CacheConstants;
import top.charles7c.continew.admin.common.constant.RegexConstants;
import top.charles7c.continew.admin.common.enums.SocialSourceEnum;
Expand All @@ -55,10 +50,8 @@
import top.charles7c.continew.starter.core.util.validate.ValidationUtils;
import top.charles7c.continew.starter.extension.crud.model.resp.R;

import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* 个人中心 API
Expand Down Expand Up @@ -87,8 +80,8 @@ public R<AvatarResp> uploadAvatar(@NotNull(message = "头像不能为空") Multi

@Operation(summary = "修改基础信息", description = "修改用户基础信息")
@PatchMapping("/basic/info")
public R updateBasicInfo(@Validated @RequestBody UserBasicInfoUpdateReq updateReq) {
userService.updateBasicInfo(updateReq, LoginHelper.getUserId());
public R updateBasicInfo(@Validated @RequestBody UserBasicInfoUpdateReq req) {
userService.updateBasicInfo(req, LoginHelper.getUserId());
return R.ok("修改成功");
}

Expand Down

0 comments on commit 1b7aa9d

Please sign in to comment.