|
@@ -0,0 +1,60 @@
|
|
|
+package com.chelvc.cloud.vehicle.server.service.impl;
|
|
|
+
|
|
|
+import com.chelvc.cloud.vehicle.api.dto.HotWordsDTO;
|
|
|
+import com.chelvc.cloud.vehicle.api.service.HotWordsService;
|
|
|
+import com.chelvc.framework.redis.cache.Cache;
|
|
|
+import com.chelvc.framework.redis.cache.CachePrefix;
|
|
|
+import org.apache.dubbo.config.annotation.DubboService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.redis.core.ZSetOperations;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 搜索热词业务操作实现
|
|
|
+ *
|
|
|
+ * @author qizai
|
|
|
+ * @date 2023/11/3
|
|
|
+ */
|
|
|
+@DubboService(interfaceClass = HotWordsService.class)
|
|
|
+public class HotWordsServiceImpl implements HotWordsService {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缓存
|
|
|
+ */
|
|
|
+ @Autowired
|
|
|
+ private Cache<Object> cache;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> listHotWords(Integer number) {
|
|
|
+ if (number == null) {
|
|
|
+ number = 0;
|
|
|
+ }
|
|
|
+ List<String> hotWords = new ArrayList<>();
|
|
|
+ // redis 排序中,下标从0开始,所以这里需要 -1 处理
|
|
|
+ number = number - 1;
|
|
|
+ Set<ZSetOperations.TypedTuple<Object>> set = cache.reverseRangeWithScores(CachePrefix.HOT_WORD.getPrefix(), number);
|
|
|
+ if (set == null || set.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ for (ZSetOperations.TypedTuple<Object> defaultTypedTuple : set) {
|
|
|
+ hotWords.add(Objects.requireNonNull(defaultTypedTuple.getValue()).toString());
|
|
|
+ }
|
|
|
+ return hotWords;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void setHotWords(HotWordsDTO hotWords) {
|
|
|
+ cache.incrementScore(CachePrefix.HOT_WORD.getPrefix(), hotWords.getKeywords(), hotWords.getPoint());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除热门关键词
|
|
|
+ *
|
|
|
+ * @param keywords 热词
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void deleteHotWords(String keywords) {
|
|
|
+ cache.zRemove(CachePrefix.HOT_WORD.getPrefix(), keywords);
|
|
|
+ }
|
|
|
+}
|