下面简单介绍一下spring3.1.M1中的cache功能。
spring3.1.M1中负责cache的模块是org.springframework.context-3.1.0.M1.jar
与2.5时的modules模块类似,3.1的注解缓存也是在方法上声明注解,3.1同样提供了两个注解:
@Cacheable:负责将方法的返回值加入到缓存中
@CacheEvict:负责清除缓存
@Cacheable
支持如下几个参数:
value:缓存位置名称,不能为空,如果使用EHCache,就是ehcache.xml中声明的cache的name
key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL
condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL
例如:
//将缓存保存进andCache,并使用参数中的userId加上一个字符串(这里使用方法名称)作为缓存的key
@Cacheable(value="andCache",key="#userId + 'findById'")
public SystemUser findById(String userId) {
SystemUser user = (SystemUser) dao.findById(SystemUser.class, userId);
return user ;
}
//将缓存保存进andCache,并当参数userId的长度小于32时才保存进缓存,默认使用参数值及类型作为缓存的key
@Cacheable(value="andCache",condition="#userId.length < 32")
public boolean isReserved(String userId) {
System.out.println("hello andCache"+userId);
return false;
}
@CacheEvict
支持如下几个参数:
value:缓存位置名称,不能为空,同上
key:缓存的key,默认为空,同上
condition:触发条件,只有满足条件的情况才会清除缓存,默认为空,支持SpEL
allEntries:true表示清除value中的全部缓存,默认为false
例如:
//清除掉指定key的缓存
@CacheEvict(value="andCache",key="#user.userId + 'findById'")
public void modifyUserRole(SystemUser user) {
System.out.println("hello andCache delete"+user.getUserId());
}
//清除掉全部缓存
@CacheEvict(value="andCache",allEntries=true)
public final void setReservedUsers(String[] reservedUsers) {
System.out.println("hello andCache deleteall");
}
一般来说,我们的更新操作只需要刷新缓存中某一个值,所以定义缓存的key值的方式就很重要,最好是能够唯一,因为这样可以准确的清除掉特定的缓存,而不会影响到其它缓存值
,
比如我这里针对用户的操作,使用(userId+方法名称)的方式设定key值
,当然,你也可以找到更适合自己的方式去设定。
SpEL:Spring Expression Language
关于SpEL的介绍,可以参考如下地址:
http://static.springsource.org/spring/docs/3.1.0.M1/spring-framework-reference/html/expressions.html
了解了cache的注解之后,接下来说说如何使注解生效,其实就是需要在spring的配置文件中增加一些配置。
1.spring-cache
首先我们来看一下如何使用spring3.1自己的cache,
需要在命名空间中增加cache的配置
Xml代码
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
之后添加如下声明:
Xml代码
- <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
- <cache:annotation-driven cache-manager="cacheManager"/>
-
-
- <!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
- <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
- <property name="caches">
- <set>
- <bean
- class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
- p:name="default" />
- <bean
- class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
- p:name="andCache" />
- </set>
- </property>
- </bean>
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- spring自己的换管理器,这里定义了两个缓存位置名称 ,既注解中的value -->
<bean id="cacheManager">
<property name="caches">
<set>
<bean
class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
p:name="default" />
<bean
class="org.springframework.cache.concurrent.ConcurrentCacheFactoryBean"
p:name="andCache" />
</set>
</property>
</bean>
2.spring-ehcache
接下来说说对ehcache的支持,其实只需要把cacheManager换成EHCache的cacheManager即可,如下:
Java代码
- <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
- <cache:annotation-driven cache-manager="cacheManager"/>
-
- <!-- cacheManager工厂类,指定ehcache.xml的位置 -->
- <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
- p:configLocation="classpath:/config/ehcache.xml" />
-
- <!-- 声明cacheManager -->
- <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
- p:cacheManager-ref="cacheManagerFactory" />
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
<cache:annotation-driven cache-manager="cacheManager"/>
<!-- cacheManager工厂类,指定ehcache.xml的位置 -->
<bean id="cacheManagerFactory"
p:configLocation="classpath:/config/ehcache.xml" />
<!-- 声明cacheManager -->
<bean id="cacheManager"
p:cacheManager-ref="cacheManagerFactory" />
ehcache.xml
Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
- monitoring="autodetect">
- <!--
- <diskStore path="java.io.tmpdir" /> -->
- <diskStore path="E:/cachetmpdir"/>
- <defaultCache maxElementsInMemory="10000" eternal="false"
- timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
- maxElementsOnDisk="10000000" diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
-
- <cache name="andCache" maxElementsInMemory="10000"
- maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
- diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
- memoryStoreEvictionPolicy="LFU" />
- </ehcache>
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect">
<!--
<diskStore path="java.io.tmpdir" /> -->
<diskStore path="E:/cachetmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<cache name="andCache" maxElementsInMemory="10000"
maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
ok,这样注解缓存就生效了。
分享到:
相关推荐
利用spring实现的简单的缓存的例子,代码解释:http://blog.csdn.net/maoyeqiu/article/details/50238035
springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot 使用spring cache缓存 和 使用fastjson配置redis系列化,springboot ...
Spring Boot缓存技术
介绍spring与ehcache缓存结合,使用spring注解访问,使用guava包中的缓存,封装guava缓存,为应用程序使用。类似于自定义的方式
通过spring-cache对方法进行缓存,同时验证如何通过key更新指定的缓存!
如何通过spring自己的缓存机制缓存方法和数据,使系统性能更高!
本文是springboot 使用spring cache缓存 和 缓存数据落地到redis的研究笔记,为方便下载查看,特意分享了springboot 使用spring cache缓存 和 缓存数据落地到redis.pdf的文件。希望搭建对springboot 使用spring ...
Spring+EhCache缓存实例
spring自动加载缓存
3.使用缓存注解: 在需要进行缓存的方法上添加缓存注解。例如,使用@Cacheable注解来指定一个方法的结果应该被缓存。 @Cacheable:将方法的结果缓存起来,下次调用该方法时,如果传入的参数相同,则直接返回缓存中的...
在接入Spring-Cloud-Gateway时,可能有需求进行缓存Json-Body数据或者Form-Urlencoded数据的情况。这篇文章主要介绍了SpringCloud Finchley Gateway 缓存请求Body和Form表单的实现,感兴趣的小伙伴们可以参考一下
spring缓存中用conditon限制放入缓存的数据。
一个关于spring二级缓存的例子和一点讲解
Spring集成Redis实现数据缓存
基于spring注解小型的web应用,本例使用EHCache来作为缓存方案,写好了配置步骤
NULL 博文链接:https://nethub2.iteye.com/blog/2348479
记录使用EHCache实现页面级的缓存以及完成集群设计的过程
Ehcache 整合Spring 使用页面、对象缓存
这是Maven项目,Spring集成Redis来实现注解缓存,代码配置方面都有详细代码,并在相关配置文件里添加了相关参考文档地址,都是博主自己一手敲出来的,如果觉得资源还可以,请五星好评哦,亲(づ ̄3 ̄)づ╭❤~
SpringCache缓存初探共5页.pdf.zip