Authored by 胡古飞

fix SearchKeyWordService

1 package com.yoho.search.service.service; 1 package com.yoho.search.service.service;
2 2
  3 +import java.util.ArrayList;
  4 +import java.util.Date;
  5 +import java.util.HashMap;
  6 +import java.util.List;
  7 +import java.util.Map;
  8 +import java.util.Set;
  9 +import java.util.concurrent.ExecutionException;
  10 +import java.util.concurrent.ExecutorService;
  11 +import java.util.concurrent.Executors;
  12 +import java.util.concurrent.TimeUnit;
  13 +
  14 +import javax.annotation.Resource;
  15 +
  16 +import org.apache.commons.lang.StringUtils;
  17 +import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;
  18 +import org.slf4j.Logger;
  19 +import org.slf4j.LoggerFactory;
  20 +import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.data.redis.core.ZSetOperations;
  22 +import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
  23 +import org.springframework.stereotype.Service;
  24 +import org.springframework.util.Assert;
  25 +
3 import com.google.common.cache.CacheBuilder; 26 import com.google.common.cache.CacheBuilder;
4 import com.google.common.cache.CacheLoader; 27 import com.google.common.cache.CacheLoader;
5 import com.yoho.core.redis.YHRedisTemplate; 28 import com.yoho.core.redis.YHRedisTemplate;
@@ -11,22 +34,6 @@ import com.yoho.search.base.utils.RedisKeys; @@ -11,22 +34,6 @@ import com.yoho.search.base.utils.RedisKeys;
11 import com.yoho.search.core.es.IElasticsearchClient; 34 import com.yoho.search.core.es.IElasticsearchClient;
12 import com.yoho.search.core.es.impl.YohoIndexHelper; 35 import com.yoho.search.core.es.impl.YohoIndexHelper;
13 import com.yoho.search.service.vo.KeyWordWithCount; 36 import com.yoho.search.service.vo.KeyWordWithCount;
14 -import org.apache.commons.lang.StringUtils;  
15 -import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse.AnalyzeToken;  
16 -import org.slf4j.Logger;  
17 -import org.slf4j.LoggerFactory;  
18 -import org.springframework.beans.factory.annotation.Autowired;  
19 -import org.springframework.data.redis.core.ZSetOperations;  
20 -import org.springframework.data.redis.core.ZSetOperations.TypedTuple;  
21 -import org.springframework.stereotype.Service;  
22 -import org.springframework.util.Assert;  
23 -  
24 -import javax.annotation.Resource;  
25 -import java.util.*;  
26 -import java.util.concurrent.ExecutionException;  
27 -import java.util.concurrent.ExecutorService;  
28 -import java.util.concurrent.Executors;  
29 -import java.util.concurrent.TimeUnit;  
30 37
31 /** 38 /**
32 * 将关键字结果存进redis中 39 * 将关键字结果存进redis中
@@ -41,14 +48,10 @@ public class SearchKeyWordService { @@ -41,14 +48,10 @@ public class SearchKeyWordService {
41 48
42 @Resource(name = "yhNoSyncZSetOperations") 49 @Resource(name = "yhNoSyncZSetOperations")
43 private YHZSetOperations<String, String> yhNoSyncZSetOperations; 50 private YHZSetOperations<String, String> yhNoSyncZSetOperations;
44 -  
45 -  
46 @Resource(name = "yhNoSyncRedisTemplate") 51 @Resource(name = "yhNoSyncRedisTemplate")
47 private YHRedisTemplate<String, String> yhNoSyncRedisTemplate; 52 private YHRedisTemplate<String, String> yhNoSyncRedisTemplate;
48 -  
49 @Autowired 53 @Autowired
50 private ESClientMgr esClientMgr; 54 private ESClientMgr esClientMgr;
51 -  
52 @Autowired 55 @Autowired
53 private YohoIndexHelper yohoIndexHelper; 56 private YohoIndexHelper yohoIndexHelper;
54 57
@@ -78,14 +81,10 @@ public class SearchKeyWordService { @@ -78,14 +81,10 @@ public class SearchKeyWordService {
78 if (!useCache) { 81 if (!useCache) {
79 return getAnalyzeTermsDirect(keyWord, analyzer); 82 return getAnalyzeTermsDirect(keyWord, analyzer);
80 } 83 }
81 -  
82 try { 84 try {
83 - return CacheBuilder.newBuilder()  
84 - .maximumSize(10000)  
85 - .expireAfterWrite(10, TimeUnit.MINUTES)  
86 - .build(new CacheLoader<String, List<String>>() { 85 + return CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.MINUTES).build(new CacheLoader<String, List<String>>() {
87 @Override 86 @Override
88 - public List<String> load(String cacheKey) throws Exception { 87 + public List<String> load(String cacheKey){
89 String[] arrays = cacheKey.split("@", 2); 88 String[] arrays = cacheKey.split("@", 2);
90 Assert.isTrue(arrays != null && arrays.length == 2); 89 Assert.isTrue(arrays != null && arrays.length == 2);
91 return getAnalyzeTermsDirect(arrays[1], arrays[0]); 90 return getAnalyzeTermsDirect(arrays[1], arrays[0]);