RecallConfigCommonService.java 4.32 KB
package com.yoho.search.recall.config;

import com.yoho.search.common.utils.MapSizeUtils;
import com.yoho.search.dal.model.CsRecallConfigCommon;
import com.yoho.search.service.base.index.CsRecallConfigCommonIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component
class RecallConfigCommonService {

    private static final Logger RECALL_NEW_LOGGER = LoggerFactory.getLogger("RECALL");

    @Autowired
    private CsRecallConfigCommonIndexBaseService csRecallConfigCommonIndexBaseService;

    private ScheduledExecutorService schedule = Executors.newSingleThreadScheduledExecutor();

    private Map<String, Map<Integer, ConfigSizeInterval>> recallConfigCommonCache = new HashMap<>();

    @PostConstruct
    void init() {
        schedule.scheduleAtFixedRate(() -> loadRecallConfigCommonCache(), 0, 1, TimeUnit.MINUTES);
    }

    private void loadRecallConfigCommonCache() {
        try {
            RECALL_NEW_LOGGER.info("loadRecallConfigCommonCache ........");
            long begin = System.currentTimeMillis();
            List<CsRecallConfigCommon> configList = csRecallConfigCommonIndexBaseService.queryAll();
            Map<String, Map<Integer, ConfigSizeInterval>> tempCache = new HashMap<>();
            for (CsRecallConfigCommon csRecallConfigCommon : configList) {
                //1、类型判断
                String configKey = csRecallConfigCommon.getConfigType();
                if (!tempCache.containsKey(configKey)) {
                    tempCache.put(configKey, new HashMap<>());
                }
                //2、生成页面结果
                int pageId = csRecallConfigCommon.getConfigPage();
                int size = csRecallConfigCommon.getSize();
                int interval = csRecallConfigCommon.getInterval();
                ConfigSizeInterval configSizeInterval = new ConfigSizeInterval(size, interval);
                tempCache.get(configKey).put(pageId, configSizeInterval);
            }
            recallConfigCommonCache = tempCache;
            RECALL_NEW_LOGGER.info("loadRecallConfigCommonCache success,recallConfigCommonCache size is[{}],cost is [{}] ms", MapSizeUtils.getMapElementMapCount(recallConfigCommonCache),System.currentTimeMillis()-begin);
        } catch (Exception e) {
            RECALL_NEW_LOGGER.error("loadRecallConfigCommonCache error,exception is:" + e.getMessage(), e);
        }
    }

    private ConfigSizeInterval queryCommonConfig(String configKey, int configPage) {
        Map<Integer, ConfigSizeInterval> pageConfigMap = recallConfigCommonCache.get(configKey);
        if (pageConfigMap == null) {
            return null;
        }
        ConfigSizeInterval pageConfig = pageConfigMap.get(configPage);
        if (pageConfig == null) {
            pageConfig = pageConfigMap.get(RecallConfigConstants.DEFAULT_PAGE_ID);
        }
        if (pageConfigMap == null) {
            return null;
        } else {
            return pageConfig;
        }
    }

    /**
     * 获取配置的大小
     *
     * @param pageId
     * @param configKey
     * @param defaultSize
     * @return
     */
    public int queryConfigSize(int pageId, String configKey, int defaultSize) {
        if (StringUtils.isBlank(configKey)) {
            return defaultSize;
        }
        ConfigSizeInterval config = queryCommonConfig(configKey, pageId);
        return config == null ? defaultSize : config.getSize();
    }

    /**
     * 获取配置的间隔
     *
     * @param pageId
     * @param configKey
     * @param defaultInterval
     * @return
     */
    public int queryConfigInterval(int pageId, String configKey, int defaultInterval) {
        if (StringUtils.isBlank(configKey)) {
            return defaultInterval;
        }
        ConfigSizeInterval config = queryCommonConfig(configKey, pageId);
        return config == null ? defaultInterval : config.getInterval();
    }

}