import java.io.Serializable;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.beans.factory.InitializingBean;
/**
* 拦截器,用于缓存方法返回结果.
*
* @version $Id: MethodCacheInterceptor.java v 1.0 2004-11-28 14:57:00 Znjq Exp $
* @author <a href="mailto:znjq1980@etang.com">Znjq </a>
*/
public class MethodCacheInterceptor implements MethodInterceptor,
InitializingBean {
private Cache cache;
/**
* sets cache name to be used
*/
public void setCache(Cache cache) {
this.cache = cache;
}
/*
* (non-Javadoc)
*
* @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
*/
public Object invoke(MethodInvocation invocation) throws Throwable {
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
Object result;
String cacheKey = getCacheKey(targetName, methodName, arguments);
Element element = cache.get(cacheKey);
if (element == null) {
//call target/sub-interceptor
result = invocation.proceed();
//cache method result
element = new Element(cacheKey, (Serializable) result);
cache.put(element);
}
return element.getValue();
}
/**
* creates cache key: targetName.methodName.argument0.argument1...
*/
private String getCacheKey(String targetName, String methodName,
Object[] arguments) {
StringBuffer sb = new StringBuffer();
sb.append(targetName).append(".").append(methodName);
if ((arguments != null) && (arguments.length != 0)) {
for (int i = 0; i < arguments.length; i++) {
sb.append(".").append(arguments[i]);
}
}
return sb.toString();
}
/*
* (non-Javadoc)
*
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
}
}
|
相关推荐
spring aop 自定义缓存实现的一个小实例,地址:http://blog.csdn.net/maoyeqiu/article/details/50260357
本代码通过使用spring aop+ehcache的技术,实现了方法级别的查询缓存,主要原理是 方法的完整路径+方法参数值,作为key,放入cache中,下次访问时先判断cache中是否有该key.
需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法...
NULL 博文链接:https://honda418.iteye.com/blog/354945
主要介绍了Spring AOP实现Redis缓存数据库查询的相关内容,源码部分还是不错的,需要的朋友可以参考下。
Context-support模块:提供了对第三方库嵌入Spring应用的集成支持,比如缓存(EhCache、Guava、JCache)、邮件服务(JavaMail)、任务调度(CommonJ、Quartz)和模板引擎(FreeMarker、JasperReports、速率)。 SpEL模块:...
主要给大家介绍了关于Spring AOP如何整合redis(注解方式)实现缓存统一管理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
主要是利用Spring AOP实现动态数据源,和数据缓存操作。
spring-**cntext**-4.3.6.RELEASE.jar:spring提供了基础IOC功能上的扩展服务,提供了很多企业级服务的支持,如邮件服务,任务调度,JNDI定位,EJB集成,远程访问,缓存以及各种试图层框架的封装等。 spring-...
NULL 博文链接:https://leiwuluan.iteye.com/blog/1176120
缘起需求:需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或者DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/...
还有严重的问题,有时候不想使用缓存服务了,还得每个方法里面判断下。为了在接下来的项目中能够方便的控制和使用 redis,所以也粗略的模仿了下membercache的aop代码,写了一份spring-redis-aop。当然方法有很多种...
今天小编就为大家分享一篇关于SpringBoot AOP控制Redis自动缓存和更新的示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
2、Spring Cache利用了AOP,实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了,做到了对代码侵入性做小。 3、由于市面上的...
主要介绍了浅谈SpringBoot集成Redis实现缓存处理(Spring AOP实现),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
答:Spring通过提前曝光机制,利用三级缓存解决循环依赖(这原理还是挺简单的,参考:三级缓存、图解循环依赖原理) 再问:Spring通过提前曝光,直接曝光到二级缓存已经可以解决循环依赖问题了,为什么一定要三级...
概述Spring Aop在Spring框架中的布局举足轻重,主要用于实现事务,缓存,安全等功能。主要介绍以下三个方面: Spring AOP多种代理机制相关核心类介绍。 Spring Boot中AOP注解方式源码分析。 Spring Boot 1.x版本和2...
spring aop实例,常常通过 AOP 来处理一些具有横切性质的系统性服务,如事物管理、安全检查、缓存、对象池管理等
通用的报表缓存设计项目背景现状态方案代码实现Redis配置注解AOP缓存(关键)业务调用(部分)测试结果:第一次第二次测试结果总结 项目背景 1:用户群体大, 2:业务计算量大,计算逻辑复杂。 现状态 1:主面页面,...