Authored by foxxy

支持返回结果压缩

... ... @@ -6,8 +6,6 @@ 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);
public void delete(String key);
... ...
... ... @@ -67,10 +67,4 @@ public class EhCache implements CacheInterface {
public boolean exist(String key) {
return this.get(key) == null ? false : true;
}
@Override
public String addAndReturn(String key, CacheObject value, int expiredTimeInMinute) {
return null;
}
}
... ...
... ... @@ -61,14 +61,4 @@ 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;
}
}
... ...
... ... @@ -67,14 +67,4 @@ 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;
}
}
... ...
... ... @@ -29,17 +29,6 @@ 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);
}
... ...
... ... @@ -3,10 +3,14 @@ package com.yoho.search.common.utils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xerial.snappy.Snappy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class SnappyUtils {
private static final Logger logger = LoggerFactory.getLogger(SnappyUtils.class);
... ... @@ -26,6 +30,33 @@ public class SnappyUtils {
return null;
}
}
/**
* 压缩
* @param obj
* @return
*/
public static String compress(Object obj){
if(null==obj){
return StringUtils.EMPTY;
}
//序列化使用json,并压缩缓存对象
String value=null;
if(obj instanceof String){
value=(String)obj;
}else{
value=JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
}
try {
byte[] compressVal = Snappy.compress(value.getBytes(StandardCharsets.UTF_8));
String compressStr = new String( compressVal, StandardCharsets.ISO_8859_1);
return compressStr;
} catch (Exception e) {
//do nothing
logger.warn("compress the object failed",e);
return StringUtils.EMPTY;
}
}
/**
* 解压缩
... ...
... ... @@ -36,26 +36,6 @@ public class SearchCacheService {
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、如果不适用缓存,则直接返回
... ... @@ -159,10 +139,6 @@ 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);
... ...
... ... @@ -19,6 +19,7 @@ import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.common.cache.model.SearchCache;
import com.yoho.search.common.utils.SnappyUtils;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.base.SearchDynamicConfigService;
... ... @@ -69,7 +70,7 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
String redisCacheKey = searchCacheKeyHelper.getSearchLikeNotInShopKeyCache(productSkn, pageSize);
JSONObject cacheObject = searchCacheService.getJSONObjectFromCache(this.searchCache, redisCacheKey);
if (cacheObject != null) {
return new SearchApiResult().setData(cacheObject);
return new SearchApiResult().setData(SnappyUtils.compress(cacheObject));
}
// 4、获取当前查询的SKN的基本信息
... ... @@ -104,8 +105,8 @@ public class SearchLikeNotInShopService extends AbstractCacheAbleService {
result.put("product_list", productListResults);
// 8、结果加入缓存
String compressedReturnValue=searchCacheService.addJSONObjectToCacheAndReturn(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(compressedReturnValue).setCompress(true);
searchCacheService.addJSONObjectToCache(this.searchCache, redisCacheKey, result);
return new SearchApiResult().setData(SnappyUtils.compress(result)).setCompress(true);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
... ...