redis increase expiration time

The code is directly as follows ( the red marked part )

package com.zx.znyd.dao.impl;

import com.alibaba.fastjson.JSONObject;
import com.zx.znyd.dao.InputAssociationDao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class InputAssociationDaoImpl implements InputAssociationDao {
    private static final String INPUT_ASSOCIATION = "input_Association_";
    private static final Logger logger = LoggerFactory
            .getLogger(InputAssociationDaoImpl.class);
    @Autowired
    @Qualifier("esClient")
    private Client client;

    @Autowired
    @Qualifier("redisTemplate1")
    private RedisTemplate<Serializable, Object> redisTemplate1;

    @SuppressWarnings("unchecked")
    @Override
    public ArrayList<String> getInputAssociation(String l, String c,
            String words) {
        logger.info( "getInputAssociation method request parameters: l:{},c:{},words:{}" , l, c,
                words);
        HashOperations<Serializable, Object, Object> operations = redisTemplate1
                .opsForHash ();
        Map<Object, Object> custsetinfoValueMap;
        custsetinfoValueMap = operations.entries("custsetinfo_" + l + "_" + c);
        ArrayList<String> inputAssociationArr = new ArrayList<>();
        if ((custsetinfoValueMap.size() > 0)
                && (null != custsetinfoValueMap.get("inputAssociation"))
                && custsetinfoValueMap.get("inputAssociation").equals("1")) {
            logger.info( "Province:" + l + "Channel" + c + "The input association is enabled, the corresponding key is "
                    + "custsetinfo_" + l + "_" + c);
            String key;
            if(null!=custsetinfoValueMap.get("inputAssociationNum")){
                key = getInputAssociationKey(l, c, words,custsetinfoValueMap.get("inputAssociationNum").toString());
            }else{
                key = getInputAssociationKey(l, c, words,"3");
            }
            
            Object reidsAnswer = operations.entries(key)
                    .get("inputAssociation");
            if (null != reidsAnswer) {
                logger.info( "Check redis, return directly according to the input associative words found by redis" );
                String [] inputAssociationStr=reidsAnswer.toString().split("\\^");
                for(String elem:inputAssociationStr){
                    inputAssociationArr.add(elem);
                }
                return inputAssociationArr;
            } else {
                logger.info( "redis did not find the corresponding input associative words, check es" );
                String index = "index_primary_whole";
                String type = "std_index";
                SearchRequestBuilder builder = client.prepareSearch(index)
                        .setTypes(type).setSearchType(SearchType.DEFAULT)
                        .setFrom(0).setSize(10);
                BoolQueryBuilder query = QueryBuilders.boolQuery()
                        .must(new QueryStringQueryBuilder(l).field("province"))
                        .must(new QueryStringQueryBuilder(c).field("channel"))
                        .must(new QueryStringQueryBuilder(words)
                                .field("squestion"));
                builder.setQuery(query);
                SearchResponse searchResponse = builder.execute().actionGet();
                SearchHits hits = searchResponse.getHits();
                SearchHit[] searchHits = hits.hits();
                int inputAssociationNum = Integer.parseInt(custsetinfoValueMap
                        .get("inputAssociationNum").toString());
                int num = inputAssociationNum < searchHits.length
                        ? inputAssociationNum : searchHits.length;
                logger.info( "The number of standard questions obtained by input association is: " + searchHits.length
                         + ", the number of associative words configured on the management platform is: " + inputAssociationNum
                         + ", and the number of associative words returned is " + num);

                for (int i = 0; i < num; i++) {
                    inputAssociationArr.add(
                            searchHits[i].getSource().get("squestion").toString());
                }
                String inputAssociationStr=StringUtils.join(inputAssociationArr, "^");
                operations.put(key, "inputAssociation" , inputAssociationStr);
                 redisTemplate1.expire(key, 24 , TimeUnit.HOURS); 
                logger.info( "Return the input associative words found in es and store them in redis" ) ;
            }

        } else {
            logger.info( "Province:" + l + "channel" + c + "input association not enabled" );
        }
        return inputAssociationArr;

    }

    private String getInputAssociationKey(String l, String c, String words,String num) {
        return new StringBuilder(INPUT_ASSOCIATION).append(l).append("_")
                .append(c).append("_").append(words).append("_").append(num).toString();

    }

}

语法:RedisTemplate.expire(key, num, TimeUnit.SECONDS); //设置超时时间10秒 第三个参数控制时间单位,详情查看TimeUnit

The first parameter key, representing the key value of redis

The second parameter, num, represents the timeout period

The third parameter TimeUnit.SECONDS represents the unit of the timeout time, which can be DAYS (days), HOURS (hours), MINUTES (minutes), SECONDS (seconds), etc.

 

Reference URL: https://my.oschina.net/mifans/blog/792446

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324925092&siteId=291194637