Authored by foxxy

支持结果压缩返回

@@ -5,6 +5,8 @@ import com.yoho.search.common.cache.model.CacheObject; @@ -5,6 +5,8 @@ import com.yoho.search.common.cache.model.CacheObject;
5 public interface CacheInterface { 5 public interface CacheInterface {
6 6
7 public void addOrUpdate(String key, CacheObject value, int expiredTimeInMinute); 7 public void addOrUpdate(String key, CacheObject value, int expiredTimeInMinute);
  8 +
  9 + public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute);
8 10
9 public boolean exist(String key); 11 public boolean exist(String key);
10 12
@@ -2,16 +2,16 @@ package com.yoho.search.common.cache.impls; @@ -2,16 +2,16 @@ package com.yoho.search.common.cache.impls;
2 2
3 import javax.annotation.PostConstruct; 3 import javax.annotation.PostConstruct;
4 4
5 -import net.sf.ehcache.Cache;  
6 -import net.sf.ehcache.CacheManager;  
7 -import net.sf.ehcache.Element;  
8 -  
9 import org.slf4j.Logger; 5 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
11 import org.springframework.stereotype.Service; 7 import org.springframework.stereotype.Service;
12 8
13 import com.yoho.search.common.cache.model.CacheObject; 9 import com.yoho.search.common.cache.model.CacheObject;
14 10
  11 +import net.sf.ehcache.Cache;
  12 +import net.sf.ehcache.CacheManager;
  13 +import net.sf.ehcache.Element;
  14 +
15 @Service("ehCache") 15 @Service("ehCache")
16 public class EhCache implements CacheInterface { 16 public class EhCache implements CacheInterface {
17 17
@@ -68,4 +68,9 @@ public class EhCache implements CacheInterface { @@ -68,4 +68,9 @@ public class EhCache implements CacheInterface {
68 return this.get(key) == null ? false : true; 68 return this.get(key) == null ? false : true;
69 } 69 }
70 70
  71 + @Override
  72 + public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
  73 + return null;
  74 + }
  75 +
71 } 76 }
@@ -61,4 +61,14 @@ public class SearchRedis implements CacheInterface { @@ -61,4 +61,14 @@ public class SearchRedis implements CacheInterface {
61 } 61 }
62 } 62 }
63 63
  64 + @Override
  65 + public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
  66 + try {
  67 + return RedisCacheUtils.addAndReturnCompresse(searchValueOperations, key, value, expiredTimeInMinute);
  68 + } catch (Exception e) {
  69 + logger.error(e.getMessage(), e);
  70 + }
  71 + return null;
  72 + }
  73 +
64 } 74 }
@@ -46,6 +46,7 @@ public class YohoRedis implements CacheInterface { @@ -46,6 +46,7 @@ public class YohoRedis implements CacheInterface {
46 logger.error(e.getMessage(), e); 46 logger.error(e.getMessage(), e);
47 } 47 }
48 } 48 }
  49 +
49 50
50 @Override 51 @Override
51 public void delete(String key) { 52 public void delete(String key) {
@@ -66,4 +67,14 @@ public class YohoRedis implements CacheInterface { @@ -66,4 +67,14 @@ public class YohoRedis implements CacheInterface {
66 } 67 }
67 } 68 }
68 69
  70 + @Override
  71 + public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
  72 + try {
  73 + RedisCacheUtils.addAndReturnCompresse(yhNoSyncValueOperations, key, value, expiredTimeInMinute);
  74 + } catch (Exception e) {
  75 + logger.error(e.getMessage(), e);
  76 + }
  77 + return null;
  78 + }
  79 +
69 } 80 }
@@ -28,6 +28,17 @@ public class RedisCacheUtils { @@ -28,6 +28,17 @@ public class RedisCacheUtils {
28 } 28 }
29 valueOperations.set(key, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES); 29 valueOperations.set(key, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES);
30 } 30 }
  31 +
  32 +
  33 + public static <T> String addAndReturnCompresse(YHValueOperations<String, String> valueOperations, String key, T t, int expiredTimeInMinute) {
  34 + String uncompressStr = serializeToString(t);
  35 + String compressedVal = SnappyUtils.compress(uncompressStr);
  36 + if (StringUtils.isBlank(compressedVal)) {
  37 + return null;
  38 + }
  39 + valueOperations.set(key, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES);
  40 + return compressedVal;
  41 + }
31 42
32 public static void delete(YHRedisTemplate<String, String> redisTemplate, String key) { 43 public static void delete(YHRedisTemplate<String, String> redisTemplate, String key) {
33 redisTemplate.delete(key); 44 redisTemplate.delete(key);
@@ -5,6 +5,7 @@ import java.io.Serializable; @@ -5,6 +5,7 @@ import java.io.Serializable;
5 public class SearchApiResult extends AbstractApiResult implements Serializable { 5 public class SearchApiResult extends AbstractApiResult implements Serializable {
6 private static final long serialVersionUID = 654492513696448848L; 6 private static final long serialVersionUID = 654492513696448848L;
7 private Object data; 7 private Object data;
  8 + private boolean compress=false;
8 9
9 public Object getData() { 10 public Object getData() {
10 return data; 11 return data;
@@ -14,6 +15,15 @@ public class SearchApiResult extends AbstractApiResult implements Serializable { @@ -14,6 +15,15 @@ public class SearchApiResult extends AbstractApiResult implements Serializable {
14 this.data = data; 15 this.data = data;
15 return this; 16 return this;
16 } 17 }
  18 +
  19 + public boolean getCompress() {
  20 + return compress;
  21 + }
  22 +
  23 + public SearchApiResult setCompress(boolean compress) {
  24 + this.compress = compress;
  25 + return this;
  26 + }
17 27
18 public SearchApiResult setMessage(String message) { 28 public SearchApiResult setMessage(String message) {
19 this.message = message; 29 this.message = message;
@@ -26,16 +26,36 @@ public class SearchCacheService { @@ -26,16 +26,36 @@ public class SearchCacheService {
26 private void addJSONObjectToCache(String key, JSONObject jsonObject, SearchCache searchCache) { 26 private void addJSONObjectToCache(String key, JSONObject jsonObject, SearchCache searchCache) {
27 // 1、如果不适用缓存,则直接返回 27 // 1、如果不适用缓存,则直接返回
28 if (!ISearchConstants.SEARCH_USE_CACHE) { 28 if (!ISearchConstants.SEARCH_USE_CACHE) {
29 - return; 29 + return ;
30 } 30 }
31 // 2、如果缓存不存在,则直接返回 31 // 2、如果缓存不存在,则直接返回
32 if (searchCache == null || searchCache.getCache() == null) { 32 if (searchCache == null || searchCache.getCache() == null) {
33 - return; 33 + return ;
34 } 34 }
35 // 3、加入缓存 35 // 3、加入缓存
36 CacheObject cacheObject = new CacheObject(jsonObject); 36 CacheObject cacheObject = new CacheObject(jsonObject);
37 searchCache.getCache().addOrUpdate(key, cacheObject, searchCache.getCacheInMinute()); 37 searchCache.getCache().addOrUpdate(key, cacheObject, searchCache.getCacheInMinute());
38 } 38 }
  39 +
  40 + /**
  41 + * 加入对象进缓存
  42 + *
  43 + * @param key
  44 + * @param jsonObject
  45 + */
  46 + private String addJSONObjectToCacheAndReturn(String key, JSONObject jsonObject, SearchCache searchCache) {
  47 + // 1、如果不适用缓存,则直接返回
  48 + if (!ISearchConstants.SEARCH_USE_CACHE) {
  49 + return null;
  50 + }
  51 + // 2、如果缓存不存在,则直接返回
  52 + if (searchCache == null || searchCache.getCache() == null) {
  53 + return null;
  54 + }
  55 + // 3、加入缓存
  56 + CacheObject cacheObject = new CacheObject(jsonObject);
  57 + return searchCache.getCache().addAndReturn(key, cacheObject, searchCache.getCacheInMinute());
  58 + }
39 59
40 private void addJSONArrayToCache(String key, JSONArray jsonArray, SearchCache searchCache) { 60 private void addJSONArrayToCache(String key, JSONArray jsonArray, SearchCache searchCache) {
41 // 1、如果不适用缓存,则直接返回 61 // 1、如果不适用缓存,则直接返回
@@ -139,6 +159,10 @@ public class SearchCacheService { @@ -139,6 +159,10 @@ public class SearchCacheService {
139 public void addJSONObjectToCache(SearchCache searchCache, String cacheKey, JSONObject jsonObject) { 159 public void addJSONObjectToCache(SearchCache searchCache, String cacheKey, JSONObject jsonObject) {
140 this.addJSONObjectToCache(cacheKey, jsonObject, searchCache); 160 this.addJSONObjectToCache(cacheKey, jsonObject, searchCache);
141 } 161 }
  162 +
  163 + public String addJSONObjectToCacheAndReturn(SearchCache searchCache, String cacheKey, JSONObject jsonObject) {
  164 + return this.addJSONObjectToCacheAndReturn(cacheKey, jsonObject, searchCache);
  165 + }
142 166
143 public void addJSONObjectToCache(SearchCache searchCache, String indexName, SearchParam searchParam, JSONObject jsonObject) { 167 public void addJSONObjectToCache(SearchCache searchCache, String indexName, SearchParam searchParam, JSONObject jsonObject) {
144 String key = this.genSearchParamString(indexName, searchParam); 168 String key = this.genSearchParamString(indexName, searchParam);
@@ -104,8 +104,8 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService { @@ -104,8 +104,8 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
104 result.put("product_list", productListResults); 104 result.put("product_list", productListResults);
105 105
106 // 8、结果加入缓存 106 // 8、结果加入缓存
107 - searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, result);  
108 - return new SearchApiResult().setData(result); 107 + String compressedReturnValue=searchCacheService.addJSONObjectToCacheAndReturn(this.searchCache, redisCacheKey, result);
  108 + return new SearchApiResult().setData(compressedReturnValue).setCompress(true);
109 } catch (Exception e) { 109 } catch (Exception e) {
110 logger.error(e.getMessage(), e); 110 logger.error(e.getMessage(), e);
111 return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500); 111 return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);