缓存信息在启动时注入

类名后面加上 implements CommandLineRunner 表示启动时调用方法

@Override

public void run(String... args) throws Exception
启动时调用的方法

package com.lunz.amp.translator.commom;

import com.alibaba.fastjson.JSON;
import com.lunz.amp.core.util.LogUtil;
import com.lunz.amp.translator.commom.cache.RedisCache;
import com.lunz.amp.translator.service.CategoryWordsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class WordsType /*implements CommandLineRunner*/ {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private CategoryWordsService categoryWordsService;

    //====================敏感词===================

    /**
     * 一般敏感词
     */
    public void setCommomWords() {
        try {
            List<String> getCommom = categoryWordsService.getCategoryWords(1);
            if (getCommom.size() > 0) {
                String str = JSON.toJSONString(getCommom);
                String key = RedisConstants.RedisKeyPrefix.CREATE_WORDS_COMMON;
                redisCache.set(key, str, -1, TimeUnit.MINUTES);
            } else {
                log.error("缓存->存储一般敏感词出错,获取失败!");
            }
        } catch (Exception e) {
            log.error("缓存->存储省一般敏感词出错,错误信息【{}】,堆栈信息【{}】", e.getMessage(), LogUtil.convertStackInfoToStr(e));
        }
    }

    /**
     * 严重敏感词
     */
    public void setSeriousWords() {
        try {
            List<String> getSerious = categoryWordsService.getCategoryWords(2);
            if (getSerious.size() > 0) {
                String str = JSON.toJSONString(getSerious);
                String key = RedisConstants.RedisKeyPrefix.CREATE_WORDS_SERIOUS;
                redisCache.set(key, str, -1, TimeUnit.MINUTES);
            } else {
                log.error("缓存->存储严重敏感词出错,获取失败!");
            }
        } catch (Exception e) {
            log.error("缓存->存储严重敏感词出错,错误信息【{}】,堆栈信息【{}】", e.getMessage(), LogUtil.convertStackInfoToStr(e));
        }
    }

    /**
     * 致命敏感词
     */
    public void setFatalWords() {
        try {
            List<String> getFatal = categoryWordsService.getCategoryWords(3);
            if (getFatal.size() > 0) {
                String str = JSON.toJSONString(getFatal);
                String key = RedisConstants.RedisKeyPrefix.CREATE_WORDS_FATAL;
                redisCache.set(key, str, -1, TimeUnit.MINUTES);
            } else {
                log.error("缓存->存储致命敏感词出错,获取失败!");
            }
        } catch (Exception e) {
            log.error("缓存->存储致命敏感词出错,错误信息【{}】,堆栈信息【{}】", e.getMessage(), LogUtil.convertStackInfoToStr(e));
        }
    }

    /**
     * 刷新敏感词
     */
    public void updateWords(Integer id) {
        switch (id) {
            case 1:
                redisCache.delete(RedisConstants.RedisKeyPrefix.CREATE_WORDS_COMMON);
                setCommomWords();
                break;
            case 2:
                redisCache.delete(RedisConstants.RedisKeyPrefix.CREATE_WORDS_SERIOUS);
                setSeriousWords();
                break;
            case 3:
                redisCache.delete(RedisConstants.RedisKeyPrefix.CREATE_WORDS_FATAL);
                setFatalWords();
                break;
        }
    }

   /* @Override
    public void run(String... args) throws Exception {
        setCommomWords();
        setSeriousWords();
        setFatalWords();
    }
*/
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值