From 0760c72ef8d03d762bd4833bea33cdfa3681222a Mon Sep 17 00:00:00 2001 From: Chopper Date: Fri, 30 Jul 2021 14:27:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=83=AD=E8=AF=8D=E8=8E=B7=E5=8F=96=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E6=A0=B9=E6=8D=AE=E6=95=B0=E9=87=8F=EF=BC=8C=E4=BC=A0?= =?UTF-8?q?=E9=80=92count=E8=8E=B7=E5=8F=96=E7=B3=BB=E7=BB=9F=E7=83=AD?= =?UTF-8?q?=E6=90=9C=E8=AF=8D=E8=AF=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/goods/GoodsBuyerController.java | 4 ++-- framework/src/main/java/cn/lili/cache/Cache.java | 11 +++++++++++ .../main/java/cn/lili/cache/impl/RedisCache.java | 16 +++++++++++++++- .../search/service/EsGoodsSearchService.java | 8 ++++---- .../serviceimpl/EsGoodsSearchServiceImpl.java | 4 ++-- .../setting/HotWordsManagerController.java | 2 +- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java b/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java index b299596d..95523e59 100644 --- a/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java +++ b/buyer-api/src/main/java/cn/lili/controller/goods/GoodsBuyerController.java @@ -115,8 +115,8 @@ public class GoodsBuyerController { @ApiOperation(value = "获取搜索热词") @GetMapping("/hot-words") - public ResultMessage> getGoodsHotWords(Integer start, Integer end) { - List hotWords = goodsSearchService.getHotWords(start, end); + public ResultMessage> getGoodsHotWords(Integer count) { + List hotWords = goodsSearchService.getHotWords(count); return ResultUtil.data(hotWords); } diff --git a/framework/src/main/java/cn/lili/cache/Cache.java b/framework/src/main/java/cn/lili/cache/Cache.java index cd475d4d..d71bc646 100644 --- a/framework/src/main/java/cn/lili/cache/Cache.java +++ b/framework/src/main/java/cn/lili/cache/Cache.java @@ -244,6 +244,17 @@ public interface Cache { */ Set> reverseRangeWithScores(String sortedSetName, Integer start, Integer end); + /** + * zrevrange命令, 查询Sorted Set中指定范围的值 + * 返回的有序集合中,score大的在前面 + * zrevrange方法无需担心用于指定范围的start和end出现越界报错问题 + * + * @param sortedSetName sortedSetName + * @param count 查询数量 + * @return 获取满足条件的集合 + */ + Set> reverseRangeWithScores(String sortedSetName, Integer count); + /** * 向Zset里添加成员 diff --git a/framework/src/main/java/cn/lili/cache/impl/RedisCache.java b/framework/src/main/java/cn/lili/cache/impl/RedisCache.java index f34515e5..dc4e8754 100644 --- a/framework/src/main/java/cn/lili/cache/impl/RedisCache.java +++ b/framework/src/main/java/cn/lili/cache/impl/RedisCache.java @@ -224,7 +224,7 @@ public class RedisCache implements Cache { @Override public void incrementScore(String sortedSetName, String keyword) { //指向key名为KEY的zset元素 - redisTemplate.opsForZSet().incrementScore(sortedSetName,keyword, 1); + redisTemplate.opsForZSet().incrementScore(sortedSetName, keyword, 1); } @Override @@ -247,6 +247,20 @@ public class RedisCache implements Cache { return this.redisTemplate.opsForZSet().reverseRangeWithScores(sortedSetName, start, end); } + /** + * zrevrange命令, 查询Sorted Set中指定范围的值 + * 返回的有序集合中,score大的在前面 + * zrevrange方法无需担心用于指定范围的start和end出现越界报错问题 + * + * @param sortedSetName sortedSetName + * @param count 获取数量 + * @return 符合排序的集合 + */ + @Override + public Set> reverseRangeWithScores(String sortedSetName, Integer count) { + return this.redisTemplate.opsForZSet().reverseRangeWithScores(sortedSetName, 0, count); + } + /** * 向Zset里添加成员 diff --git a/framework/src/main/java/cn/lili/modules/search/service/EsGoodsSearchService.java b/framework/src/main/java/cn/lili/modules/search/service/EsGoodsSearchService.java index c4cfbebb..d8531110 100644 --- a/framework/src/main/java/cn/lili/modules/search/service/EsGoodsSearchService.java +++ b/framework/src/main/java/cn/lili/modules/search/service/EsGoodsSearchService.java @@ -29,14 +29,14 @@ public interface EsGoodsSearchService { /** * 获取热门关键词 * - * @param start 查询范围开始位置 - * @param end 查询范围结束位置 - * @return + * @param count 热词数量 + * @return 热词集合 */ - List getHotWords(Integer start, Integer end); + List getHotWords(Integer count); /** * 设置热门关键词 + * * @param hotWords 热词分数 */ void setHotWords(HotWordsDTO hotWords); diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java index 55233623..9102af75 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsSearchServiceImpl.java @@ -102,9 +102,9 @@ public class EsGoodsSearchServiceImpl implements EsGoodsSearchService { } @Override - public List getHotWords(Integer start, Integer end) { + public List getHotWords(Integer count) { List hotWords = new ArrayList<>(); - Set set = cache.reverseRangeWithScores(CachePrefix.HOT_WORD.getPrefix(), start, end); + Set set = cache.reverseRangeWithScores(CachePrefix.HOT_WORD.getPrefix(), count); for (DefaultTypedTuple defaultTypedTuple : set) { hotWords.add(Objects.requireNonNull(defaultTypedTuple.getValue()).toString()); } diff --git a/manager-api/src/main/java/cn/lili/controller/setting/HotWordsManagerController.java b/manager-api/src/main/java/cn/lili/controller/setting/HotWordsManagerController.java index 6722c921..8d59150b 100755 --- a/manager-api/src/main/java/cn/lili/controller/setting/HotWordsManagerController.java +++ b/manager-api/src/main/java/cn/lili/controller/setting/HotWordsManagerController.java @@ -30,7 +30,7 @@ public class HotWordsManagerController { @ApiOperation(value = "获取热词") @GetMapping public ResultMessage getHotWords() { - return ResultUtil.data(esGoodsSearchService.getHotWords(0, 99)); + return ResultUtil.data(esGoodsSearchService.getHotWords(99)); } @ApiOperation(value = "设置热词")