Authored by sailing-PC\sailing

add cache clean

support ufo in sms
package com.yohoufo.order.controller;
import com.yohoufo.common.ApiResponse;
import com.yohoufo.order.service.cache.CacheCleaner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by chenchao on 2018/10/20.
*/
@RestController
@RequestMapping("/cacheRest")
public class CacheRest {
final private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private CacheCleaner cacheCleaner;
@RequestMapping("/cleanList")
public ApiResponse cleanList(@RequestParam("uid")int uid,
@RequestParam("tabType")String tabType){
logger.info("in cleanList uid {} tabType {}", uid, tabType);
cacheCleaner.cleanList(uid, tabType);
return new ApiResponse.ApiResponseBuilder().code(200).message("clean列表成功").build();
}
@RequestMapping("/cleanDetail")
public ApiResponse cleanDetail(@RequestParam("uid")int uid,
@RequestParam("orderCode")long orderCode){
logger.info("in cleanList uid {} orderCode {}", uid, orderCode);
cacheCleaner.cleanDetail(uid, orderCode);
return new ApiResponse.ApiResponseBuilder().code(200).message("clean order 成功").build();
}
}
... ...
... ... @@ -4,7 +4,6 @@ import com.google.common.collect.Lists;
import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
... ... @@ -23,13 +22,16 @@ public class CacheCleaner {
public void cleanList(int uid, String tabType){
RedisKeyBuilder orderListKey = buildOrderList(uid, tabType);
List<RedisKeyBuilder> keyBuilders = Lists.newArrayList(orderListKey);
delete(keyBuilders);
}
public void cleanOrder(int uid, long orderCode){
public void cleanDetail(int uid, long orderCode){
RedisKeyBuilder detailKB = CacheKeyBuilder.orderDetailKey(uid, orderCode);
List<RedisKeyBuilder> keyBuilders = Lists.newArrayList(detailKB);
delete(keyBuilders);
}
/**
... ...
... ... @@ -38,7 +38,10 @@ public class CacheKeyBuilder {
}
public enum KeyTemp{
ORDER_LIST("order:orderList:","uid:{}:tabType:{}");
ORDER_LIST("ufo:order:orderList:","uid:{}:tabType:{}"),
ORDER_DETAIL("ufo:order:orderDetail:", "uid:{}:orderCode:{}");
private String fix;
... ... @@ -63,10 +66,6 @@ public class CacheKeyBuilder {
}
}
public static void main(String[] args) {
RedisKeyBuilder kb = orderListKey(1, TabType.BUY.name());
System.out.println(kb.getKey() + "\n" + kb.getKeyTemplate());
}
public static RedisKeyBuilder orderListKey(int uid, String tabType){
return KeyTemp.ORDER_LIST.builderKey(new Object[]{uid, tabType});
... ... @@ -77,6 +76,13 @@ public class CacheKeyBuilder {
}
public static String orderListCntHashKey(){
return PatternKeyTemplate.ORDER_LIST_COUNT.getKeyPattern();
}
public static RedisKeyBuilder orderDetailKey(int uid, long orderCode){
return KeyTemp.ORDER_DETAIL.builderKey(new Object[]{uid, orderCode});
}
}
... ...
package com.yohoufo.order.service.cache;
/**
* 单位为秒
* Created by chenchao on 2018/10/20.
*/
public interface ExpiredTime {
int ORDER_LIST = 300;
int ORDER_DETAIL = 300;
int ORDER_SUMMARY = 300;
}
... ...
package com.yohoufo.order.service.cache;
import com.alibaba.fastjson.JSON;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yohoufo.common.cache.CacheClient;
import com.yohoufo.common.cache.SnappyZipUtils;
import com.yohoufo.order.common.TabType;
import com.yohoufo.order.model.request.OrderListRequest;
import com.yohoufo.order.model.response.OrderDetailInfo;
import com.yohoufo.order.model.response.OrderListInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
... ... @@ -25,6 +28,23 @@ public class OrderCacheService {
@Autowired
private CacheClient cacheClient;
public void cacheOrderSummary(int uid, TabType actor, Integer cnt){
String hashKey = CacheKeyBuilder.orderListCntHashKey();
String key = CacheKeyBuilder.orderListKey(uid, actor.getValue()).getKey();
try{
cacheClient.hashPut(key, hashKey, cnt, ExpiredTime.ORDER_LIST);
}catch (Exception ex){
logger.warn("in cacheOrderSummary fail ,uid {} actor {} cnt {}", uid, actor, cnt);
}
}
public Integer getOrderSummary(int uid, TabType actor){
String hashKey = CacheKeyBuilder.orderListCntHashKey();
String key = CacheKeyBuilder.orderListKey(uid, actor.getValue()).getKey();
return cacheClient.hashGet(key, hashKey, Integer.class);
}
public void cacheOrderList(OrderListRequest req, List<OrderListInfo> orderListInfos){
String hashKey = CacheKeyBuilder.orderListHashKey(req);
... ... @@ -32,7 +52,7 @@ public class OrderCacheService {
try{
String hashValue = JSON.toJSONString(orderListInfos);
String compressHashValue = SnappyZipUtils.compress(hashValue);
cacheClient.hashPut(key, hashKey, compressHashValue, 300);
cacheClient.hashPut(key, hashKey, compressHashValue, ExpiredTime.ORDER_LIST);
}catch (Exception ex){
logger.warn("in cacheOrderList fail ,req {}", req);
}
... ... @@ -58,4 +78,16 @@ public class OrderCacheService {
}
public void cacheOrderDetailInfo(int uid, long orderCode, OrderDetailInfo orderDetailInfo){
RedisKeyBuilder kb = CacheKeyBuilder.orderDetailKey(uid, orderCode);
cacheClient.set(kb.getKey(), ExpiredTime.ORDER_DETAIL , orderDetailInfo);
}
public OrderDetailInfo getOrderDetailInfo(int uid, long orderCode){
RedisKeyBuilder kb = CacheKeyBuilder.orderDetailKey(uid, orderCode);
OrderDetailInfo orderDetailInfo = cacheClient.get(kb.getKey(), OrderDetailInfo.class);
return orderDetailInfo;
}
}
... ...
... ... @@ -39,6 +39,8 @@ public class SendSmsService {
McSmsByMobileBO smsBo = new McSmsByMobileBO();
smsBo.setMobile(mobileList.get(i));
smsBo.setContent(content);
//"smsProviderCode" : "3"
smsBo.setSmsProviderCode("3");
smsBo.setIsNoDisturb(0);//是否免打扰, 1-是 0-否
boArray[i] = smsBo;
}
... ...