feature: 收藏功能.

This commit is contained in:
lijiahang
2023-09-05 11:50:53 +08:00
parent fb42e31f6e
commit 8035396995
15 changed files with 206 additions and 46 deletions

View File

@@ -1,6 +1,5 @@
package com.orion.ops.module.infra.api.impl;
import com.orion.lang.utils.Strings;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.constant.Const;
import com.orion.ops.framework.redis.core.utils.RedisUtils;
@@ -52,43 +51,36 @@ public class FavoriteApiImpl implements FavoriteApi {
favoriteService.addFavorite(request);
// 获取缓存
String key = FavoriteCacheKeyDefine.FAVORITE.format(typeName, userId);
String cache = redisTemplate.opsForValue().get(key);
List<Long> relIdList;
if (Strings.isBlank(cache)) {
relIdList = Lists.newList();
} else {
relIdList = Arrays.stream(cache.split(Const.COMMA))
.map(Long::valueOf)
.collect(Collectors.toList());
}
// 插入缓存
relIdList.add(relId);
RedisUtils.set(key, FavoriteCacheKeyDefine.FAVORITE, Lists.join(relIdList));
RedisUtils.listPushAll(key, Lists.singleton(relId), String::valueOf);
// 设置过期时间
RedisUtils.setExpire(key, FavoriteCacheKeyDefine.FAVORITE);
}
@Override
@Async("asyncExecutor")
public Future<List<Long>> getFavoriteRelIdList(FavoriteTypeEnum type, Long userId) {
// 获取缓存
String typeName = type.name();
String key = FavoriteCacheKeyDefine.FAVORITE.format(typeName, userId);
String cache = redisTemplate.opsForValue().get(key);
List<Long> relIdList;
if (cache != null) {
// 不为 null 则获取缓存
relIdList = Arrays.stream(cache.split(Const.COMMA))
.map(Long::valueOf)
.collect(Collectors.toList());
} else {
// 为 null 从数据库获取
// 获取缓存
List<Long> cacheRelIdList = RedisUtils.listRange(key, Long::valueOf);
if (cacheRelIdList.isEmpty()) {
// 查询数据库
FavoriteQueryRequest request = new FavoriteQueryRequest();
request.setUserId(userId);
request.setType(typeName);
relIdList = favoriteService.getFavoriteRelIdList(request);
cacheRelIdList = favoriteService.getFavoriteRelIdList(request);
// 设置 -1 到缓存防止穿透
if (cacheRelIdList.isEmpty()) {
cacheRelIdList.add(Const.L_N_1);
}
// 设置缓存
redisTemplate.opsForValue().set(key, Lists.join(relIdList));
RedisUtils.listPushAll(key, cacheRelIdList, String::valueOf);
// 设置过期时间
RedisUtils.setExpire(key, FavoriteCacheKeyDefine.FAVORITE);
}
return CompletableFuture.completedFuture(relIdList);
// 尝试删除防止穿透的 key
cacheRelIdList.remove(Const.L_N_1);
return CompletableFuture.completedFuture(cacheRelIdList);
}
@Override
@@ -141,6 +133,7 @@ public class FavoriteApiImpl implements FavoriteApi {
}
@Override
@Async("asyncExecutor")
public void deleteFavoriteByRelIdList(List<Long> relIdList) {
if (Lists.isEmpty(relIdList)) {
return;

View File

@@ -1,10 +1,11 @@
package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.define.wrapper.IPageRequest;
import com.orion.ops.framework.common.annotation.IgnoreLog;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.framework.common.constant.IgnoreLogMode;
import com.orion.ops.framework.common.valid.group.Id;
import com.orion.ops.framework.common.valid.group.Page;
import com.orion.ops.module.infra.entity.request.menu.SystemRoleBindMenuRequest;
import com.orion.ops.module.infra.entity.request.role.SystemRoleCreateRequest;
import com.orion.ops.module.infra.entity.request.role.SystemRoleQueryRequest;
@@ -56,14 +57,14 @@ public class SystemRoleController {
@PutMapping("/update")
@Operation(summary = "通过 id 更新角色")
@PreAuthorize("@ss.hasPermission('infra:system-role:update')")
public Integer updateSystemRole(@Validated @RequestBody SystemRoleUpdateRequest request) {
public Integer updateSystemRole(@Validated(Id.class) @RequestBody SystemRoleUpdateRequest request) {
return systemRoleService.updateSystemRoleById(request);
}
@PutMapping("/update-status")
@Operation(summary = "通过 id 更新角色状态")
@PreAuthorize("@ss.hasPermission('infra:system-role:update-status')")
public Integer updateRoleStatus(@Validated @RequestBody SystemRoleStatusRequest request) {
public Integer updateRoleStatus(@Validated(Id.class) @RequestBody SystemRoleStatusRequest request) {
return systemRoleService.updateRoleStatus(request);
}
@@ -88,7 +89,7 @@ public class SystemRoleController {
@PostMapping("/query")
@Operation(summary = "分页查询角色")
@PreAuthorize("@ss.hasPermission('infra:system-role:query')")
public DataGrid<SystemRoleVO> getSystemRolePage(@Validated(IPageRequest.class) @RequestBody SystemRoleQueryRequest request) {
public DataGrid<SystemRoleVO> getSystemRolePage(@Validated(Page.class) @RequestBody SystemRoleQueryRequest request) {
return systemRoleService.getSystemRolePage(request);
}

View File

@@ -2,11 +2,12 @@ package com.orion.ops.module.infra.controller;
import com.orion.lang.define.wrapper.DataGrid;
import com.orion.lang.define.wrapper.HttpWrapper;
import com.orion.lang.define.wrapper.IPageRequest;
import com.orion.lang.utils.collect.Lists;
import com.orion.ops.framework.common.annotation.IgnoreLog;
import com.orion.ops.framework.common.annotation.RestWrapper;
import com.orion.ops.framework.common.constant.IgnoreLogMode;
import com.orion.ops.framework.common.valid.group.Id;
import com.orion.ops.framework.common.valid.group.Page;
import com.orion.ops.module.infra.entity.request.user.*;
import com.orion.ops.module.infra.entity.vo.SystemUserVO;
import com.orion.ops.module.infra.service.SystemUserRoleService;
@@ -54,7 +55,7 @@ public class SystemUserController {
@PutMapping("/update")
@Operation(summary = "通过 id 更新用户")
@PreAuthorize("@ss.hasPermission('infra:system-user:update')")
public Integer updateSystemUser(@Validated @RequestBody SystemUserUpdateRequest request) {
public Integer updateSystemUser(@Validated(Id.class) @RequestBody SystemUserUpdateRequest request) {
return systemUserService.updateSystemUserById(request);
}
@@ -117,7 +118,7 @@ public class SystemUserController {
@PostMapping("/query")
@Operation(summary = "分页查询用户")
@PreAuthorize("@ss.hasPermission('infra:system-user:query')")
public DataGrid<SystemUserVO> getSystemUserPage(@Validated(IPageRequest.class) @RequestBody SystemUserQueryRequest request) {
public DataGrid<SystemUserVO> getSystemUserPage(@Validated(Page.class) @RequestBody SystemUserQueryRequest request) {
return systemUserService.getSystemUserPage(request);
}