...
|
...
|
@@ -6,6 +6,7 @@ import java.util.List; |
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
import com.yohoufo.common.utils.StringUtil;
|
|
|
import org.apache.commons.lang.ArrayUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
...
|
...
|
@@ -90,35 +91,71 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
|
|
|
//caculate cache key
|
|
|
final String level1_cache_key = this.getL1CacheKey(method, joinPoint.getArgs());
|
|
|
//final String level2_cache_key = this.getL2CacheKey(joinPoint, cacheExpire);
|
|
|
|
|
|
if (ArrayUtils.isEmpty(cacheExpire.pageArgs())) {
|
|
|
return dealSingeCache(level1_cache_key, cacheExpire, returnType, methodName, joinPoint);
|
|
|
} else {
|
|
|
return dealPageCache(level1_cache_key, cacheExpire, returnType, methodName, joinPoint);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private Object dealPageCache(String cacheKey, Cachable cacheExpire, Class<?> returnType, String methodName, ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
//从一级缓存中获取数据
|
|
|
try {
|
|
|
Object level1_obj = this.cacheClient.get(level1_cache_key, returnType);
|
|
|
if (level1_obj != null) {
|
|
|
logger.info("Cache1 hit for method:{} at key:{}.", methodName, level1_cache_key);
|
|
|
return level1_obj;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("get from level 1 cache exception.", e);
|
|
|
}
|
|
|
String pageKey = getPageCacheKey(cacheExpire.pageArgs());
|
|
|
try {
|
|
|
Object level1_obj = this.cacheClient.hashGet(cacheKey, pageKey, returnType);
|
|
|
if (level1_obj != null) {
|
|
|
logger.info("Cache1 hit for page method:{} at key:{}, page:{}.", methodName, cacheKey, pageKey);
|
|
|
return level1_obj;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("get from level 1 page cache exception.", e);
|
|
|
}
|
|
|
|
|
|
|
|
|
//cache miss at level1: 调用原来的请求,然后将结果缓存到cache1&cache2中。如果调用异常,则从cache2中查找
|
|
|
Object httpResponse;
|
|
|
Object httpResponse = null;
|
|
|
try {
|
|
|
httpResponse = joinPoint.proceed();
|
|
|
|
|
|
if (httpResponse != null) {
|
|
|
this.saveToCache(level1_cache_key, cacheExpire, httpResponse);
|
|
|
logger.info("saveToPageCache page method:{} at key:{}, page:{}.", methodName, cacheKey, pageKey);
|
|
|
this.saveToPageCache(cacheKey, pageKey, cacheExpire, httpResponse);
|
|
|
}
|
|
|
|
|
|
} catch (Throwable throwable) {
|
|
|
logger.error("Cache1 & Cache2 miss for method:{} .", methodName);
|
|
|
logger.error("Cache1 & Cache2 miss for page method:{} .", methodName);
|
|
|
throw throwable;
|
|
|
}
|
|
|
return httpResponse;
|
|
|
}
|
|
|
|
|
|
private Object dealSingeCache(String cacheKey, Cachable cacheExpire, Class<?> returnType, String methodName, ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
//从一级缓存中获取数据
|
|
|
try {
|
|
|
Object level1_obj = this.cacheClient.get(cacheKey, returnType);
|
|
|
if (level1_obj != null) {
|
|
|
logger.info("Cache1 hit for single method:{} at key:{}.", methodName, cacheKey);
|
|
|
return level1_obj;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
logger.warn("get from level 1 single cache exception.", e);
|
|
|
}
|
|
|
|
|
|
|
|
|
//cache miss at level1: 调用原来的请求,然后将结果缓存到cache1&cache2中。如果调用异常,则从cache2中查找
|
|
|
Object httpResponse = null;
|
|
|
try {
|
|
|
httpResponse = joinPoint.proceed();
|
|
|
|
|
|
if (httpResponse != null) {
|
|
|
logger.info("saveToPageCache page method:{} at key:{}.", methodName, cacheKey);
|
|
|
this.saveToCache(cacheKey, cacheExpire, httpResponse);
|
|
|
}
|
|
|
|
|
|
} catch (Throwable throwable) {
|
|
|
logger.error("Cache1 & Cache2 miss for single method:{} .", methodName);
|
|
|
throw throwable;
|
|
|
}
|
|
|
return httpResponse;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -143,6 +180,21 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
}
|
|
|
this.cacheClient.set(level1_cache_key, cacheExpireTime, httpResponse);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将结果保存在缓存中
|
|
|
* @param level1_cache_key
|
|
|
* @param cacheExpire
|
|
|
* @param httpResponse
|
|
|
*/
|
|
|
public void saveToPageCache(String level1_cache_key, String pageKey, Cachable cacheExpire, Object httpResponse) {
|
|
|
int cacheExpireTime = getCacheTime(cacheExpire);
|
|
|
// 一级缓存失效的时间,如果指定了,则使用指定的值。如果没有指定,则使用配置的值
|
|
|
if (cacheExpireTime <= 0) {
|
|
|
cacheExpireTime = 60;
|
|
|
}
|
|
|
this.cacheClient.hashPut(level1_cache_key, pageKey, httpResponse, cacheExpireTime);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 单位是s
|
...
|
...
|
@@ -159,8 +211,8 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
/**
|
|
|
* 根据拦截的方法的参数,生成cache的key. yh_gw:METHOD_NAME:METHOD_PARAM
|
|
|
*
|
|
|
* @param joinPoint 拦截点
|
|
|
* @param cacheExpire
|
|
|
* @param method 拦截点
|
|
|
* @param params
|
|
|
* @return key
|
|
|
*/
|
|
|
private String getL1CacheKey(Method method, Object[] params) {
|
...
|
...
|
@@ -169,7 +221,9 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
return StringUtils.EMPTY;
|
|
|
}
|
|
|
int[] excludeParams = cacheExpire.excludeArgs();
|
|
|
String cacheKey = this.getCacheKey(method.getName(), params, excludeParams);
|
|
|
int[] pageParams = cacheExpire.pageArgs();
|
|
|
|
|
|
String cacheKey = this.getCacheKey(method.getName(), params, excludeParams, pageParams);
|
|
|
|
|
|
String level1_cache_key = "YH:UFOGW:L1:" + cacheKey;
|
|
|
|
...
|
...
|
@@ -189,10 +243,10 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
}
|
|
|
|
|
|
|
|
|
private String getCacheKey(String methodName, Object params[], int[] excludeParams ){
|
|
|
private String getCacheKey(String methodName, Object params[], int[] excludeParams, int[] pageParams ){
|
|
|
|
|
|
//args params sign
|
|
|
String args_sign = this.getStrings(params, excludeParams);
|
|
|
String args_sign = this.getStrings(params, excludeParams, pageParams);
|
|
|
|
|
|
//method name
|
|
|
String cacheKeyPrefix = "yh_ufogw:" + methodName;
|
...
|
...
|
@@ -200,6 +254,10 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
return cacheKeyPrefix + ":" + args_sign;
|
|
|
}
|
|
|
|
|
|
private String getPageCacheKey(int[] pageParams) {
|
|
|
return "PAGE:" + StringUtils.join(pageParams, ":");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -209,7 +267,7 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
* @param excludeParams arguments to exclude
|
|
|
* @return params sign
|
|
|
*/
|
|
|
public String getStrings(Object arguments[], int[] excludeParams) {
|
|
|
public String getStrings(Object arguments[], int[] excludeParams, int[] pageParams) {
|
|
|
|
|
|
if (ArrayUtils.isEmpty(arguments)) {
|
|
|
return StringUtils.EMPTY;
|
...
|
...
|
@@ -219,7 +277,7 @@ public class ControllerCacheAop implements ApplicationContextAware{ |
|
|
|
|
|
for (int i = 0; i < arguments.length; i++) {
|
|
|
//排除掉某些参数
|
|
|
if (ArrayUtils.contains(excludeParams, i)) {
|
|
|
if (ArrayUtils.contains(excludeParams, i) || ArrayUtils.contains(pageParams, i)) {
|
|
|
continue;
|
|
|
}
|
|
|
Object arg = arguments[i];
|
...
|
...
|
|