@Aspect
public class ApplicationAspect {
private static final Logger logger = Logger.getLogger(ApplicationAspect.class);
private Cache<String, Object> cache = CacheBuilder.newBuilder()
.maximumSize(10000).expireAfterWrite(12 , TimeUnit.HOURS).build();
private Object getCallableCache(final String requestPath , final ProceedingJoinPoint pjp) {
try {
return cache.get(requestPath ,
new Callable<Object>() {
@Override
public Object call() {
logger.info("from db") ;
try {
return pjp.proceed() ;
} catch (Throwable e) {
logger.error("Cache may be error!") ;
return null ;
}
}
});
} catch (ExecutionException e) {
logger.error("Cache may be error!") ;
return null ;
}
}
@Around("execution(* cn....controller..*.*(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
RequestAttributes ra = RequestContextHolder.getRequestAttributes() ;
ServletRequestAttributes sra = (ServletRequestAttributes) ra ;
HttpServletRequest request = sra.getRequest() ;
String requestPath = request.getServletPath() ;
Map<String , String[]> inputParamMap = request.getParameterMap() ;
TreeMultimap<String , String> requestSortedParam = TreeMultimap.create() ;
for(Map.Entry<String, String[]> entry : inputParamMap.entrySet()){
String[] values = entry.getValue() ;
Arrays.sort(values) ;
for(String value : values)
requestSortedParam.put(entry.getKey() , new String(value.getBytes("ISO-8859-1"), "UTF-8")) ;
}
String inputParams = "";
for(Map.Entry<String , String> entry : requestSortedParam.entries()){
inputParams += entry.getKey() + "=" + entry.getValue() + "&" ;
}
if(! Strings.isNullOrEmpty(inputParams)){
requestPath += "?" + inputParams.substring(0 , inputParams.length() - 1) ;
}
return getCallableCache(requestPath, pjp) ;
}
}
spring guava cache
最新推荐文章于 2024-03-27 14:20:47 发布