xiangpei
9 天以前 8065107726ad1fc13591c9bc47819207948bc45c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cn.lili.modules.search.serviceimpl;
 
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.modules.search.entity.dos.HotWordsHistory;
import cn.lili.modules.search.entity.dto.HotWordsDTO;
import cn.lili.modules.search.mapper.HotWordsHistoryMapper;
import cn.lili.modules.search.service.HotWordsService;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import cn.lili.modules.system.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * HotWordsServiceImpl
 *
 * @author Chopper
 * @version v1.0
 * 2022-04-14 09:35
 */
@Slf4j
@Service
public class HotWordsServiceImpl implements HotWordsService {
 
    /**
     * 缓存
     */
    @Autowired
    private Cache<Object> cache;
 
    @Override
    public List<String> getHotWords(Integer count) {
        if (count == null) {
            count = 0;
        }
        List<String> hotWords = new ArrayList<>();
        // redis 排序中,下标从0开始,所以这里需要 -1 处理
        count = count - 1;
        Set<ZSetOperations.TypedTuple<Object>> set = cache.reverseRangeWithScores(CachePrefix.HOT_WORD.getPrefix(), count);
        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 List<HotWordsHistory> getHotWordsVO(Integer count) {
        if (count == null) {
            count = 50;
        }
        List<HotWordsHistory> hotWords = new ArrayList<>();
        // redis 排序中,下标从0开始,所以这里需要 -1 处理
        count = count - 1;
        Set<ZSetOperations.TypedTuple<Object>> set = cache.reverseRangeWithScores(CachePrefix.HOT_WORD.getPrefix(), count);
        if (set == null || set.isEmpty()) {
            return new ArrayList<>();
        }
        for (ZSetOperations.TypedTuple<Object> defaultTypedTuple : set) {
            try {
                hotWords.add(new HotWordsHistory(defaultTypedTuple.getValue().toString(),
                        defaultTypedTuple.getScore().intValue()));
            } catch (Exception e) {
                log.error("读取热词错误", e);
            }
 
        }
 
 
        Collections.sort(hotWords);
        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);
    }
 
}