LocalCacheKeyUtils.java 1.47 KB
package com.yoho.search.common.utils;

import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.utils.SearchParamUtils;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.List;
import java.util.Set;

/**
 * Created by henry on 2016/9/21.
 */
public class LocalCacheKeyUtils {

    public static String  guavaCacheKey(String indexName,SearchParam searchParam){

        StringBuilder sb = new StringBuilder();
        // 拼装索引名称
        sb.append("indexName:").append(indexName).append(';');
        // 拼装报文
        SearchSourceBuilder searchSourceBuilder = SearchParamUtils.genSearchSourceBuilderFromSearchParam(searchParam);
        sb.append("searchSource:").append(searchSourceBuilder.toString()).append(';');
        String cacheKey = "YOHOSEARCH:" + MD5Util.string2MD5(sb.toString());
        return cacheKey;

    }


    public  static String genMultiGetParamKey(String indexName, Set<String> idList, List<String> fields) {
        StringBuilder sb = new StringBuilder();
        // 拼装索引名称
        sb.append("indexName:").append(indexName).append(';');
        // 拼装搜索类型
        sb.append("idList:").append(idList.toString()).append(';');
        // 拼装报文
        sb.append("fields:").append(fields == null ? "" : fields.toString()).append(';');
        // 打印拼装结果
        return "YOHOSEARCH:" + MD5Util.string2MD5(sb.toString());
    }
}