spring guava cache


@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) ;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值