|
|
package com.yohoufo.order.service.cache;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.yohoufo.common.cache.CacheClient;
|
|
|
import com.yohoufo.common.cache.SnappyZipUtils;
|
|
|
import com.yohoufo.order.model.request.OrderListRequest;
|
|
|
import com.yohoufo.order.model.response.OrderListInfo;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* Created by chenchao on 2018/10/16.
|
|
|
*/
|
|
|
@Service
|
|
|
public class OrderCacheService {
|
|
|
|
|
|
|
|
|
final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
|
private CacheClient cacheClient;
|
|
|
|
|
|
|
|
|
public void cacheOrderList(OrderListRequest req, List<OrderListInfo> orderListInfos){
|
|
|
String hashKey = CacheKeyBuilder.orderListHashKey(req);
|
|
|
String key = CacheKeyBuilder.orderListKey(req.getUid()).getKey();
|
|
|
try{
|
|
|
String hashValue = JSON.toJSONString(orderListInfos);
|
|
|
String compressHashValue = SnappyZipUtils.compress(hashValue);
|
|
|
cacheClient.hashPut(key, hashKey, compressHashValue, 300);
|
|
|
}catch (Exception ex){
|
|
|
logger.warn("in cacheOrderList fail ,req {}", req);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public List<OrderListInfo> getOrderListInfos(OrderListRequest req){
|
|
|
String hashKey = CacheKeyBuilder.orderListHashKey(req);
|
|
|
try{
|
|
|
String key = CacheKeyBuilder.orderListKey(req.getUid()).getKey();
|
|
|
String hashVal = cacheClient.hashGet(key, hashKey, String.class);
|
|
|
String uncompressHashValue = SnappyZipUtils.uncompress(hashVal);
|
|
|
|
|
|
if (StringUtils.isNotEmpty(uncompressHashValue)) {
|
|
|
return JSON.parseArray(uncompressHashValue, OrderListInfo.class);
|
|
|
} else {
|
|
|
return null;
|
|
|
}
|
|
|
}catch (Exception ex){
|
|
|
logger.warn("in cacheOrderList fail ,req {}", req);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|