From a8a7842250d129ee7416e6af1f159a6dc0665c2c Mon Sep 17 00:00:00 2001 From: Gary Date: Sat, 9 Mar 2024 19:03:37 +0800 Subject: [PATCH] 1.cache thanks --- .../luoo/user/controller/MyController.java | 20 ++++++++++++++++--- .../luoo/user/dto/response/UserRespDTO.java | 4 +++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index 96a6940..0810c02 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -27,6 +27,8 @@ import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -64,7 +66,6 @@ import io.swagger.annotations.ApiParam; import lombok.SneakyThrows; import util.DateUtil; import util.IdWorker; -import util.ScaleFilter; import util.StringTools; @RestController @@ -72,6 +73,7 @@ import util.StringTools; @RequestMapping("/my") @Api(tags = "MyController") public class MyController extends BaseController { + private static final String REDIS_KEY_THANKS = "thanks"; @Autowired private S3Service s3Service; @Autowired @@ -95,6 +97,9 @@ public class MyController extends BaseController { @Autowired private SensitiveWordBs sensitiveWordBs; + + @Autowired + private RedisTemplate redisTemplate; public static String USER_AVATAR_DIRECTORY = "user/avatar/"; public static String USER_FEEDBACK_IMAGE_DIRECTORY = "user/feedback/"; @@ -400,8 +405,17 @@ public class MyController extends BaseController { @ApiOperation(value = "11.感谢", notes = "贡献者列表") @GetMapping("/thanks") public Result> getThanks() { - List list=userInfoService.getThanks().stream().sorted(Comparator.comparing(u->getIndex(u))).map(user->getUserRespDTO(user, false, Collections.emptySet())).collect(Collectors.toList()); - return Result.success(list); + return Result.success(getThankList()); + } + + @SuppressWarnings("unchecked") + private List getThankList() { + List thanks = (List) redisTemplate.opsForValue().get(REDIS_KEY_THANKS); + if (CollectionUtils.isEmpty(thanks)) { + thanks = userInfoService.getThanks().stream().sorted(Comparator.comparing(u->getIndex(u))).map(user->getUserRespDTO(user, false, Collections.emptySet())).collect(Collectors.toList()); + redisTemplate.opsForValue().set(REDIS_KEY_THANKS, thanks); + } + return thanks; } private Integer getIndex(UserInfo u) { diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java index a945690..353f23d 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java @@ -1,12 +1,14 @@ package com.luoo.user.dto.response; +import java.io.Serializable; import java.util.List; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data -public class UserRespDTO { +public class UserRespDTO implements Serializable { + private static final long serialVersionUID = 1L; @ApiModelProperty(value = "ID") private String id; @ApiModelProperty(value = "昵称,首次注册登录,登录用户随机为“雀乐-XXXX-XXXX”,游客为“游客-XXXX-XXXX”, XXXX 为2~4位中文昵称", example = "雀乐-蓉江晨玥^沙顺芊润")