修改收藏代码结构.
This commit is contained in:
@@ -41,7 +41,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
|
||||
@Override
|
||||
public Long create${type}(${type}CreateRequest request) {
|
||||
log.info("${type}Service-create${type} record: {}", JSON.toJSONString(record));
|
||||
log.info("${type}Service-create${type} request: {}", JSON.toJSONString(request));
|
||||
// 转换
|
||||
${type}DO record = ${type}Convert.MAPPER.to(request);
|
||||
record.setId(null);
|
||||
@@ -55,7 +55,7 @@ public class ${table.serviceImplName} implements ${table.serviceName} {
|
||||
|
||||
@Override
|
||||
public Integer update${type}ById(${type}UpdateRequest request) {
|
||||
log.info("${type}Service-update${type}ById updateRecord: {}", JSON.toJSONString(updateRecord));
|
||||
log.info("${type}Service-update${type}ById request: {}", JSON.toJSONString(request));
|
||||
// 查询
|
||||
Long id = Valid.notNull(request.getId(), ErrorMessage.ID_MISSING);
|
||||
${type}DO record = ${typeLower}DAO.selectById(id);
|
||||
|
||||
@@ -1,26 +1,18 @@
|
||||
package com.orion.ops.module.infra.api.impl;
|
||||
|
||||
import com.orion.lang.utils.collect.Lists;
|
||||
import com.orion.ops.framework.common.constant.Const;
|
||||
import com.orion.ops.framework.redis.core.utils.RedisUtils;
|
||||
import com.orion.ops.module.infra.api.FavoriteApi;
|
||||
import com.orion.ops.module.infra.define.FavoriteCacheKeyDefine;
|
||||
import com.orion.ops.module.infra.entity.request.favorite.FavoriteCreateRequest;
|
||||
import com.orion.ops.module.infra.entity.request.favorite.FavoriteQueryRequest;
|
||||
import com.orion.ops.module.infra.enums.FavoriteTypeEnum;
|
||||
import com.orion.ops.module.infra.service.FavoriteService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 收藏 对外服务实现类
|
||||
@@ -36,112 +28,46 @@ public class FavoriteApiImpl implements FavoriteApi {
|
||||
@Resource
|
||||
private FavoriteService favoriteService;
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Override
|
||||
@Async("asyncExecutor")
|
||||
public void addFavorite(FavoriteTypeEnum type, Long userId, Long relId) {
|
||||
// 插入数据库
|
||||
String typeName = type.name();
|
||||
FavoriteCreateRequest request = new FavoriteCreateRequest();
|
||||
request.setUserId(userId);
|
||||
request.setRelId(relId);
|
||||
request.setType(typeName);
|
||||
favoriteService.addFavorite(request);
|
||||
// 获取缓存
|
||||
String key = FavoriteCacheKeyDefine.FAVORITE.format(typeName, userId);
|
||||
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);
|
||||
// 获取缓存
|
||||
List<Long> cacheRelIdList = RedisUtils.listRange(key, Long::valueOf);
|
||||
if (cacheRelIdList.isEmpty()) {
|
||||
// 查询数据库
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setUserId(userId);
|
||||
request.setType(typeName);
|
||||
cacheRelIdList = favoriteService.getFavoriteRelIdList(request);
|
||||
// 添加默认值 防止穿透
|
||||
if (cacheRelIdList.isEmpty()) {
|
||||
cacheRelIdList.add(Const.NONE_ID);
|
||||
}
|
||||
// 设置缓存
|
||||
RedisUtils.listPushAll(key, cacheRelIdList, String::valueOf);
|
||||
// 设置过期时间
|
||||
RedisUtils.setExpire(key, FavoriteCacheKeyDefine.FAVORITE);
|
||||
}
|
||||
// 删除防止穿透的 key
|
||||
cacheRelIdList.remove(Const.NONE_ID);
|
||||
return CompletableFuture.completedFuture(cacheRelIdList);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("asyncExecutor")
|
||||
public void deleteFavoriteByUserId(Long userId) {
|
||||
if (userId == null) {
|
||||
return;
|
||||
}
|
||||
// 删除缓存
|
||||
List<String> favoriteKeyList = Arrays.stream(FavoriteTypeEnum.values())
|
||||
.map(s -> FavoriteCacheKeyDefine.FAVORITE.format(s, userId))
|
||||
.collect(Collectors.toList());
|
||||
redisTemplate.delete(favoriteKeyList);
|
||||
// 删除库
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setUserId(userId);
|
||||
favoriteService.deleteFavorite(request);
|
||||
request.setType(type.name());
|
||||
// 查询
|
||||
List<Long> relIdList = favoriteService.getFavoriteRelIdList(request);
|
||||
return CompletableFuture.completedFuture(relIdList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFavoriteByUserId(Long userId) {
|
||||
favoriteService.deleteFavoriteByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("asyncExecutor")
|
||||
public void deleteFavoriteByUserIdList(List<Long> userIdList) {
|
||||
if (Lists.isEmpty(userIdList)) {
|
||||
return;
|
||||
}
|
||||
// 删除缓存
|
||||
List<String> favoriteKeyList = new ArrayList<>();
|
||||
for (Long userId : userIdList) {
|
||||
Arrays.stream(FavoriteTypeEnum.values())
|
||||
.map(s -> FavoriteCacheKeyDefine.FAVORITE.format(s, userId))
|
||||
.forEach(favoriteKeyList::add);
|
||||
}
|
||||
redisTemplate.delete(favoriteKeyList);
|
||||
// 删除库
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setUserIdList(userIdList);
|
||||
favoriteService.deleteFavorite(request);
|
||||
favoriteService.deleteFavoriteByUserIdList(userIdList);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("asyncExecutor")
|
||||
public void deleteFavoriteByRelId(Long relId) {
|
||||
if (relId == null) {
|
||||
return;
|
||||
}
|
||||
// 只删除数据库 redis 等自动失效
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setRelId(relId);
|
||||
favoriteService.deleteFavorite(request);
|
||||
favoriteService.deleteFavoriteByRelId(relId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Async("asyncExecutor")
|
||||
public void deleteFavoriteByRelIdList(List<Long> relIdList) {
|
||||
if (Lists.isEmpty(relIdList)) {
|
||||
return;
|
||||
}
|
||||
// 只删除数据库 redis 等自动失效
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setRelIdList(relIdList);
|
||||
favoriteService.deleteFavorite(request);
|
||||
favoriteService.deleteFavoriteByRelIdList(relIdList);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,10 +16,17 @@ import java.util.concurrent.TimeUnit;
|
||||
public interface TagCacheKeyDefine {
|
||||
|
||||
CacheKeyDefine TAG_NAME = new CacheKeyBuilder()
|
||||
.key("tag:{}")
|
||||
.desc("tag名称 ${type}")
|
||||
.key("tag:name:{}")
|
||||
.desc("tag 名称 ${type}")
|
||||
.type(TagCacheDTO.class)
|
||||
.timeout(3, TimeUnit.DAYS)
|
||||
.build();
|
||||
|
||||
CacheKeyDefine TAG_REL = new CacheKeyBuilder()
|
||||
.key("tag:rel:{}:{}")
|
||||
.desc("tag 引用 ${type} ${relId}")
|
||||
.type(Long.class)
|
||||
.timeout(3, TimeUnit.DAYS)
|
||||
.build();
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.orion.ops.framework.common.entity.PageRequest;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
|
||||
import javax.validation.constraints.Size;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -31,7 +30,6 @@ public class FavoriteQueryRequest extends PageRequest {
|
||||
@Schema(description = "引用id")
|
||||
private Long relId;
|
||||
|
||||
@Size(max = 12)
|
||||
@Schema(description = "收藏类型")
|
||||
private String type;
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.orion.ops.module.infra.entity.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.math.*;
|
||||
|
||||
/**
|
||||
* 标签引用 视图响应对象
|
||||
*
|
||||
* @author Jiahang Li
|
||||
* @version 1.0.0
|
||||
* @since 2023-9-5 17:39
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Schema(name = "TagRelVO", description = "标签引用 视图响应对象")
|
||||
public class TagRelVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "id")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "标签名称")
|
||||
private String name;
|
||||
|
||||
@Schema(description = "标签类型")
|
||||
private String type;
|
||||
|
||||
@Schema(description = "标签id")
|
||||
private Long tagId;
|
||||
|
||||
@Schema(description = "关联id")
|
||||
private Long relId;
|
||||
|
||||
}
|
||||
@@ -39,6 +39,34 @@ public interface FavoriteService {
|
||||
*/
|
||||
List<Long> getFavoriteRelIdList(FavoriteQueryRequest request);
|
||||
|
||||
/**
|
||||
* 通过 userId 删除收藏
|
||||
*
|
||||
* @param userId userId
|
||||
*/
|
||||
void deleteFavoriteByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 通过 userId 删除收藏
|
||||
*
|
||||
* @param userIdList userId
|
||||
*/
|
||||
void deleteFavoriteByUserIdList(List<Long> userIdList);
|
||||
|
||||
/**
|
||||
* 通过 relId 删除收藏
|
||||
*
|
||||
* @param relId relId
|
||||
*/
|
||||
void deleteFavoriteByRelId(Long relId);
|
||||
|
||||
/**
|
||||
* 通过 relId 删除收藏
|
||||
*
|
||||
* @param relIdList relIdList
|
||||
*/
|
||||
void deleteFavoriteByRelIdList(List<Long> relIdList);
|
||||
|
||||
/**
|
||||
* 删除收藏
|
||||
*
|
||||
|
||||
@@ -2,17 +2,25 @@ package com.orion.ops.module.infra.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.orion.lang.utils.collect.Lists;
|
||||
import com.orion.ops.framework.common.constant.Const;
|
||||
import com.orion.ops.framework.redis.core.utils.RedisUtils;
|
||||
import com.orion.ops.module.infra.convert.FavoriteConvert;
|
||||
import com.orion.ops.module.infra.dao.FavoriteDAO;
|
||||
import com.orion.ops.module.infra.define.FavoriteCacheKeyDefine;
|
||||
import com.orion.ops.module.infra.entity.domain.FavoriteDO;
|
||||
import com.orion.ops.module.infra.entity.request.favorite.FavoriteCreateRequest;
|
||||
import com.orion.ops.module.infra.entity.request.favorite.FavoriteQueryRequest;
|
||||
import com.orion.ops.module.infra.entity.vo.FavoriteVO;
|
||||
import com.orion.ops.module.infra.enums.FavoriteTypeEnum;
|
||||
import com.orion.ops.module.infra.service.FavoriteService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -30,6 +38,9 @@ public class FavoriteServiceImpl implements FavoriteService {
|
||||
@Resource
|
||||
private FavoriteDAO favoriteDAO;
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
@Override
|
||||
public Long addFavorite(FavoriteCreateRequest request) {
|
||||
// 转换
|
||||
@@ -38,6 +49,11 @@ public class FavoriteServiceImpl implements FavoriteService {
|
||||
// 插入
|
||||
int effect = favoriteDAO.insert(record);
|
||||
log.info("FavoriteService-addFavorite effect: {}, record: {}", effect, JSON.toJSONString(record));
|
||||
// 设置缓存
|
||||
String key = FavoriteCacheKeyDefine.FAVORITE.format(request.getType(), request.getUserId());
|
||||
RedisUtils.listPush(key, request.getRelId(), String::valueOf);
|
||||
// 设置过期时间
|
||||
RedisUtils.setExpire(key, FavoriteCacheKeyDefine.FAVORITE);
|
||||
return record.getId();
|
||||
}
|
||||
|
||||
@@ -53,15 +69,90 @@ public class FavoriteServiceImpl implements FavoriteService {
|
||||
|
||||
@Override
|
||||
public List<Long> getFavoriteRelIdList(FavoriteQueryRequest request) {
|
||||
// 条件
|
||||
LambdaQueryWrapper<FavoriteDO> wrapper = this.buildQueryWrapper(request);
|
||||
// 查询
|
||||
return favoriteDAO.of()
|
||||
.wrapper(wrapper)
|
||||
.stream()
|
||||
.map(FavoriteDO::getRelId)
|
||||
.distinct()
|
||||
String type = request.getType();
|
||||
Long userId = request.getUserId();
|
||||
String cacheKey = FavoriteCacheKeyDefine.FAVORITE.format(type, userId);
|
||||
// 获取缓存
|
||||
List<Long> cacheRelIdList = RedisUtils.listRange(cacheKey, Long::valueOf);
|
||||
if (cacheRelIdList.isEmpty()) {
|
||||
// 条件
|
||||
LambdaQueryWrapper<FavoriteDO> wrapper = this.buildQueryWrapper(request);
|
||||
// 查询数据库
|
||||
cacheRelIdList = favoriteDAO.of()
|
||||
.wrapper(wrapper)
|
||||
.stream()
|
||||
.map(FavoriteDO::getRelId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
// 添加默认值 防止穿透
|
||||
if (cacheRelIdList.isEmpty()) {
|
||||
cacheRelIdList.add(Const.NONE_ID);
|
||||
}
|
||||
// 设置缓存
|
||||
RedisUtils.listPushAll(cacheKey, cacheRelIdList, String::valueOf);
|
||||
// 设置过期时间
|
||||
RedisUtils.setExpire(cacheKey, FavoriteCacheKeyDefine.FAVORITE);
|
||||
}
|
||||
// 删除防止穿透的 key
|
||||
cacheRelIdList.remove(Const.NONE_ID);
|
||||
return cacheRelIdList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFavoriteByUserId(Long userId) {
|
||||
if (userId == null) {
|
||||
return;
|
||||
}
|
||||
// 删除缓存
|
||||
List<String> favoriteKeyList = Arrays.stream(FavoriteTypeEnum.values())
|
||||
.map(s -> FavoriteCacheKeyDefine.FAVORITE.format(s, userId))
|
||||
.collect(Collectors.toList());
|
||||
redisTemplate.delete(favoriteKeyList);
|
||||
// 删除库
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setUserId(userId);
|
||||
favoriteDAO.delete(this.buildQueryWrapper(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFavoriteByUserIdList(List<Long> userIdList) {
|
||||
if (Lists.isEmpty(userIdList)) {
|
||||
return;
|
||||
}
|
||||
// 删除缓存
|
||||
List<String> favoriteKeyList = new ArrayList<>();
|
||||
for (Long userId : userIdList) {
|
||||
Arrays.stream(FavoriteTypeEnum.values())
|
||||
.map(s -> FavoriteCacheKeyDefine.FAVORITE.format(s, userId))
|
||||
.forEach(favoriteKeyList::add);
|
||||
}
|
||||
redisTemplate.delete(favoriteKeyList);
|
||||
// 删除库
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setUserIdList(userIdList);
|
||||
favoriteDAO.delete(this.buildQueryWrapper(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFavoriteByRelId(Long relId) {
|
||||
if (relId == null) {
|
||||
return;
|
||||
}
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setRelId(relId);
|
||||
// 只删除数据库 redis 等自动失效
|
||||
favoriteDAO.delete(this.buildQueryWrapper(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteFavoriteByRelIdList(List<Long> relIdList) {
|
||||
if (Lists.isEmpty(relIdList)) {
|
||||
return;
|
||||
}
|
||||
FavoriteQueryRequest request = new FavoriteQueryRequest();
|
||||
request.setRelIdList(relIdList);
|
||||
// 只删除数据库 redis 等自动失效
|
||||
favoriteDAO.delete(this.buildQueryWrapper(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -72,7 +163,6 @@ public class FavoriteServiceImpl implements FavoriteService {
|
||||
return favoriteDAO.delete(wrapper);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建查询 wrapper
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user