一,选择缓存EHCache
毫无疑问ehcache.1.2.4.jar,这个发行包必须加入项目。
二,ehcache.xml 加入classpath
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
<cache name="legendapl"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="120"
timeToLiveSeconds="360"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
</ehcache>
其中Cache各各属性表示意义,可望文而知。
三,在applicationContext.xml加入缓存管理组件。
<bean>
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
</bean>
四,缓存使用演示
@Aspect
@Component
public class CacheInterceptor extends BaseInterceptor{
@Autowired
@Qualifier("ehcacheManager")
private CacheManager cm;
// @Autowired
// @Qualifier("cache")
// private Cache cache;
@Override
@Around("execution(* cn.com.legendapl.spring.dao.impl.*.findAllUsers(..))")
public Object around(ProceedingJoinPoint jp) throws Throwable {
Cache cache = cm.getCache("legendapl");
String key = "";
Object[] args = jp.getArgs();
for (Object o : args) {
key += o.toString();
}
Object result = null;
Element element = cache.get(key);
if (element != null) {
result = element.getValue();
} else {
result = jp.proceed();
cache.put(new Element(key, result));
}
return result;
}
}
分享到:
相关推荐
使用Spring AOP技术 + ehcache做的一个缓存。此代码为转载,供大家学习使用。
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方法...
spring中使用缓存的方案,根据需求,使用Spring AOP + ehCache可实现些功能,使用EHCACHE能方便地结合SPRING来实现缓存。
缘起需求:需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或者DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/...
2、Spring Cache利用了AOP,实现了基于注解的缓存功能,并且进行了合理的抽象,业务代码不用关心底层是使用了什么缓存框架,只需要简单地加一个注解,就能实现缓存功能了,做到了对代码侵入性做小。 3、由于市面上的...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...
Spring头文件的dtd 各个包 有 aop beans cache context jdbc jee jms lang mvc oxm task tool tx util
org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...
spring官方完整jar包-4.1.6版
(3) spring-aop.jar 这个jar文件包含在应用中使用Spring的AOP特性时所需的类。使用基于AOP的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。 (4) spring-...
Aspect Cache是一个针对Spring Boot,基于AOP注解方式的轻量级缓存,目前支持EHCache,Redis缓存方式。数据类型支持POJO、Map和List数据类型,同时支持自定义缓存key解析,也支持自定义的缓存处理方式,或者扩展支持...
SimpleCacheWithSpringAop##SrpingAop需要配合Spring Aop使用,功能还比较单一,只是在获取数据的时候先进行缓存的查找,找不到的话,在方法内取得数据然后放入缓存。##Annotation利用Annotation判断是否进行缓存,...
org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...
- Spring 缓存,包括redis、ehcache、spring-cache、memcached、使用redis实现session共享 等。 - spring-docs - Spring 文档生成工具,包括 Swagger - spring-bussiness - Spring 业务应用,包括 AOP、过滤...
使用基于AOP的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。 (4) spring-context.jar 这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ...
使用基于AOP的Spring特性,如声明型事务管理(Declarative Transaction Management),也要在应用里包含这个jar包。 (4) spring-context.jar 这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ...
1. spring-aop-4.1.1.RELEASE.jar Spring面向切面编程,提供AOP实现。Spring Beans之上将横切关注点模块化 2. spring-aspects-4.1.1.RELEASE.jar 提供的对AspectJ框架的整合,也是A面向切面编程。 AspectJ可用于...
spring-aop、spring-beans、spring-cache、spring-context、spring-jdbc、spring-jee、spring-jms、spring-lang、spring-mvc、spring-oxm、spring-task、spring-tool、spring-tx、spring-util、spring-websocket、