SpringBoot结合内嵌Redis实现热词搜索功能

需求说明
需要实现一个检索功能,需要查询到最近所有的所有热词,自定需求为所有一个月内检索数量最多的10个热词;这里使用Redis的内存数据库功能,其中Redis的ZSet格式提供的功能完全贴合该需求;
后台服务使用SpringBoot实现,由于不想起多余的服务,所以从maven上找了一个可内嵌如SpringBoot的Redis服务。

SpringBoot整合内嵌Redis
pom文件添加,虽然下面这个包两三年没更新了,但亲测可放心使用

    
      com.github.kstyrc
      embedded-redis
      0.6
    

application.xml配置

spring:
  redis:
    host: localhost
    port: 6379

Redis配置类

package cn.com.casic.thinkdata.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    /**
     * 注入 RedisConnectionFactory
     */
    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    /**
     * 存储key值和评分值
     *
     * @return
     */
    @Bean
    public RedisTemplate redisKeyDb() {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
        return redisTemplate;
    }

    /**
     * 设置数据存入 redis 的序列化方式
     *
     * @param redisTemplate
     * @param factory
     */
    private void initDomainRedisTemplate(RedisTemplate redisTemplate, RedisConnectionFactory factory) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
    }

    /**
     * 实例化 HashOperations 对象,可以使用 Hash 类型操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public HashOperations hashOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForHash();
    }

    /**
     * 实例化 ValueOperations 对象,可以使用 String 操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ValueOperations valueOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    /**
     * 实例化 ListOperations 对象,可以使用 List 操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ListOperations listOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForList();
    }

    /**
     * 实例化 SetOperations 对象,可以使用 Set 操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public SetOperations setOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForSet();
    }

    /**
     * 实例化 ZSetOperations 对象,可以使用 ZSet 操作
     *
     * @param redisTemplate
     * @return
     */
    @Bean
    public ZSetOperations zSetOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForZSet();
    }
}

热词实现思路
由于热词同时需要兼顾 【检索次数】和【检索时间】两个维度,所以在Redis中同时需要维护【检索次数】和【检索时间】两种数据;
存储【检索次数】数据时,使用redis的ZSet结构,该结构可自动支持数据按分数排序;
redis的service类

package cn.com.casic.thinkdata.service.impl;

import cn.com.casic.thinkdata.service.HotWordService;
import com.google.common.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.*;

@Service
@Slf4j
@EnableScheduling
public class HotWordServiceImpl implements HotWordService {
    ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(1));

    @Resource(name = "redisKeyDb")
    private RedisTemplate redisKeyDb;

    @Override
    public void addHotWord(String hotWord) throws Exception {
        if (StringUtils.isEmpty(hotWord))
            return;
        Long now = System.currentTimeMillis(); //记录热词的日期
        redisKeyDb.opsForZSet().incrementScore("hotWord", hotWord, 1); // 加入排序set
        redisKeyDb.opsForValue().set(hotWord, now); // 记录时间
    }

    @Override
    public Set getHotWord(int topN) throws Exception {
        Set sets = redisKeyDb.opsForZSet()
                .reverseRangeByScore("hotWord", 0, Integer.MAX_VALUE, 0, topN);
        return sets;
    }

    @Override
    @Scheduled(cron = "0 0 1 * * ?")
    public Boolean clearHotWordOutTime() throws Exception {
        ListenableFuture future = executorService.submit(new Callable() {
            @Override
            public Boolean call() throws Exception {
                Long now = System.currentTimeMillis();
                ValueOperations wordTime = redisKeyDb.opsForValue();
                Set sets = redisKeyDb.opsForZSet().reverseRange("hotWord", 0, Integer.MAX_VALUE);
                for (Object set : sets) {
                    String word = String.valueOf(set);
                    Long time = Long.valueOf(String.valueOf(wordTime.get(word)));
                    if ((now - time) > 2592000000L) { // 找到1个月未操作的数据
                        redisKeyDb.opsForZSet().remove("hotWord", word);
                        redisKeyDb.opsForValue().getOperations().delete(word);
                    }
                }
                return true;
            }
        });

        Futures.addCallback(future, new FutureCallback() {
            @Override
            public void onSuccess(Boolean v) {
                log.info("@HotWord: clear redis data for hot word month ago successfully");
            }
            @Override
            public void onFailure(Throwable throwable) {
                log.info("@HotWord: fail to clear redis data for hot word, message is {}", throwable.getMessage());
            }
        });
        return true;
    }
}


为了节约内存使用,使用SpringBoot提供的定时调度功能,每天凌晨1点自动清除一个月前的数据,同时为了清理时不影响其他功能,需要通过线程后台调度,使用ListenableFuture监测执行完毕时,写日志,用于监察记录

你可能感兴趣的:(SpringBoot结合内嵌Redis实现热词搜索功能)