Authored by foxxy

支持结果压缩返回

... ... @@ -5,6 +5,8 @@ import com.yoho.search.common.cache.model.CacheObject;
public interface CacheInterface {
public void addOrUpdate(String key, CacheObject value, int expiredTimeInMinute);
public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute);
public boolean exist(String key);
... ...
... ... @@ -2,16 +2,16 @@ package com.yoho.search.common.cache.impls;
import javax.annotation.PostConstruct;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.yoho.search.common.cache.model.CacheObject;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
@Service("ehCache")
public class EhCache implements CacheInterface {
... ... @@ -68,4 +68,9 @@ public class EhCache implements CacheInterface {
return this.get(key) == null ? false : true;
}
@Override
public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
return null;
}
}
... ...
... ... @@ -61,4 +61,14 @@ public class SearchRedis implements CacheInterface {
}
}
@Override
public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
try {
return RedisCacheUtils.addAndReturnCompresse(searchValueOperations, key, value, expiredTimeInMinute);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
}
... ...
... ... @@ -46,6 +46,7 @@ public class YohoRedis implements CacheInterface {
logger.error(e.getMessage(), e);
}
}
@Override
public void delete(String key) {
... ... @@ -66,4 +67,14 @@ public class YohoRedis implements CacheInterface {
}
}
@Override
public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
try {
RedisCacheUtils.addAndReturnCompresse(yhNoSyncValueOperations, key, value, expiredTimeInMinute);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
}
... ...
... ... @@ -28,6 +28,17 @@ public class RedisCacheUtils {
}
valueOperations.set(key, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES);
}
public static <T> String addAndReturnCompresse(YHValueOperations<String, String> valueOperations, String key, T t, int expiredTimeInMinute) {
String uncompressStr = serializeToString(t);
String compressedVal = SnappyUtils.compress(uncompressStr);
if (StringUtils.isBlank(compressedVal)) {
return null;
}
valueOperations.set(key, compressedVal, expiredTimeInMinute, TimeUnit.MINUTES);
return compressedVal;
}
public static void delete(YHRedisTemplate<String, String> redisTemplate, String key) {
redisTemplate.delete(key);
... ...
... ... @@ -5,6 +5,7 @@ import java.io.Serializable;
public class SearchApiResult extends AbstractApiResult implements Serializable {
private static final long serialVersionUID = 654492513696448848L;
private Object data;
private boolean compress=false;
public Object getData() {
return data;
... ... @@ -14,6 +15,15 @@ public class SearchApiResult extends AbstractApiResult implements Serializable {
this.data = data;
return this;
}
public boolean getCompress() {
return compress;
}
public SearchApiResult setCompress(boolean compress) {
this.compress = compress;
return this;
}
public SearchApiResult setMessage(String message) {
this.message = message;
... ...
... ... @@ -26,16 +26,36 @@ public class SearchCacheService {
private void addJSONObjectToCache(String key, JSONObject jsonObject, SearchCache searchCache) {
// 1、如果不适用缓存,则直接返回
if (!ISearchConstants.SEARCH_USE_CACHE) {
return;
return ;
}
// 2、如果缓存不存在,则直接返回
if (searchCache == null || searchCache.getCache() == null) {
return;
return ;
}
// 3、加入缓存
CacheObject cacheObject = new CacheObject(jsonObject);
searchCache.getCache().addOrUpdate(key, cacheObject, searchCache.getCacheInMinute());
}
/**
* 加入对象进缓存
*
* @param key
* @param jsonObject
*/
private String addJSONObjectToCacheAndReturn(String key, JSONObject jsonObject, SearchCache searchCache) {
// 1、如果不适用缓存,则直接返回
if (!ISearchConstants.SEARCH_USE_CACHE) {
return null;
}
// 2、如果缓存不存在,则直接返回
if (searchCache == null || searchCache.getCache() == null) {
return null;
}
// 3、加入缓存
CacheObject cacheObject = new CacheObject(jsonObject);
return searchCache.getCache().addAndReturn(key, cacheObject, searchCache.getCacheInMinute());
}
private void addJSONArrayToCache(String key, JSONArray jsonArray, SearchCache searchCache) {
// 1、如果不适用缓存,则直接返回
... ... @@ -139,6 +159,10 @@ public class SearchCacheService {
public void addJSONObjectToCache(SearchCache searchCache, String cacheKey, JSONObject jsonObject) {
this.addJSONObjectToCache(cacheKey, jsonObject, searchCache);
}
public String addJSONObjectToCacheAndReturn(SearchCache searchCache, String cacheKey, JSONObject jsonObject) {
return this.addJSONObjectToCacheAndReturn(cacheKey, jsonObject, searchCache);
}
public void addJSONObjectToCache(SearchCache searchCache, String indexName, SearchParam searchParam, JSONObject jsonObject) {
String key = this.genSearchParamString(indexName, searchParam);
... ...
... ... @@ -104,8 +104,8 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
result.put("product_list", productListResults);
// 8、结果加入缓存
searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(result);
String compressedReturnValue=searchCacheService.addJSONObjectToCacheAndReturn(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(compressedReturnValue).setCompress(true);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
... ...