以EhCache为例说明缓存的配置方法:
第一种方法,配置Spring配置文件,使用AOP处理缓存
1)添加Provider,初始化CacheManager
Xml代码
<!--
Thecreatedcachemanagerisaninstanceofnet.sf.ehcache.CacheManager
-->
<beanidbeanid="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
</bean>
<beanidbeanid="cacheProviderFacade"
class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
<propertynamepropertyname="cacheManager"ref="cacheManager"/>
</bean>
<!--
Thecreatedcachemanagerisaninstanceofnet.sf.ehcache.CacheManager
-->
<beanid="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
</bean>
<beanid="cacheProviderFacade"
class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
<propertyname="cacheManager"ref="cacheManager"/>
</bean> 2)在ehcache.xml增加一个缓存
增加一个name为testCache的缓存。
Xml代码
<cache name="testCache"
maxElementsInMemory="20000"
maxElementsOnDisk="1000"
eternal="true"
overflowToDisk="true"
memoryStoreEvictionPolicy="LFU"
/>
<cache name="testCache"
maxElementsInMemory="20000"
maxElementsOnDisk="1000"
eternal="true"
overflowToDisk="true"
memoryStoreEvictionPolicy="LFU"
/>3)配置缓存
Xml代码
<bean id="testService"
class="org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean">
<property name="cacheProviderFacade" ref="cacheProviderFacade" />
<property name="cachingModels">
<props>
<prop key="find*">cacheName=<STRONG>testCache(a)</STRONG></prop>
<prop key="is*">cacheName=testCache</prop>
</props>
</property>
<property name="flushingModels">
<props>
<prop key="insert*">cacheNames=testCache</prop>
<prop key="delete*">cacheNames=testCache</prop>
<prop key="update*">cacheNames=testCache</prop>
</props>
</property>
<property name="target" ref="<STRONG>testServiceTarget(b)</STRONG>" />
</bean>
<bean id="testService"
class="org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean">
<property name="cacheProviderFacade" ref="cacheProviderFacade" />
<property name="cachingModels">
<props>
<prop key="find*">cacheName=testCache(a)</prop>
<prop key="is*">cacheName=testCache</prop>
</props>
</property>
<property name="flushingModels">
<props>
<prop key="insert*">cacheNames=testCache</prop>
<prop key="delete*">cacheNames=testCache</prop>
<prop key="update*">cacheNames=testCache</prop>
</props>
</property>
<property name="target" ref="testServiceTarget(b)" />
</bean>
其中(a)为第2步定义的cache的名字,(b)为需要缓存的bean的id。
这样,第一种方式就配置好了。
第二种方法,使用Annotations的配置方式。
1)同第一种方法1)。
2)同第二种方法2)。
3)在需要处理缓存的方法前面配置Annotion
@Cacheable(modelId="testCache")//写在方法前面就会缓存方法的返回结果
@CacheFlush(modelId="testFlushing")//写在方法前面,就会在执行该函数时清除缓存里面的数据。
这样第二种方法也完成了。
第一种方法在flush的时候可以同时flush多个缓存。第二种方法只能flush一个缓存,而且第一种方法只需要修改Spring配置文件即可。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengchua/archive/2009/08/02/4401136.aspx
分享到:
相关推荐
利用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-cache对方法进行缓存,同时验证如何通过key更新指定的缓存!
传播智客,为Spring集成的Hibernate配置二级缓存、cache.xml
在接入Spring-Cloud-Gateway时,可能有需求进行缓存Json-Body数据或者Form-Urlencoded数据的情况。这篇文章主要介绍了SpringCloud Finchley Gateway 缓存请求Body和Form表单的实现,感兴趣的小伙伴们可以参考一下
一个关于spring二级缓存的例子和一点讲解
介绍spring与ehcache缓存结合,使用spring注解访问,使用guava包中的缓存,封装guava缓存,为应用程序使用。类似于自定义的方式
基于spring注解小型的web应用,本例使用EHCache来作为缓存方案,写好了配置步骤
spring+redis作为缓存,带springTest配置,导入resource下的数据库文件,配置数据库地址和redis地址,点击test文件夹下的测试方法即可运行,也可完善页面运行。
@CacheEvict:清除指定方法的缓存数据,可以配置在方法执行前或执行后进行缓存清除。 @CachePut:更新缓存数据,即使缓存已存在也会执行方法并更新缓存。 通过以上步骤,可以在Spring Boot中使用缓存管理器来提高...
spring自动加载缓存
使用maven简单搭建Spring mvc + redis缓存 个人实验成功
spring3.0两种事务管理配置
如何通过spring自己的缓存机制缓存方法和数据,使系统性能更高!
本文是springboot 使用spring cache缓存 和 缓存数据落地到redis的研究笔记,为方便下载查看,特意分享了springboot 使用spring cache缓存 和 缓存数据落地到redis.pdf的文件。希望搭建对springboot 使用spring ...
spring + ehcache + redis两级缓存
NULL 博文链接:https://hanqunfeng.iteye.com/blog/605123
Spring+EhCache缓存实例
JAVA编程之spring cache本机缓存应用 spring cache简单实用,简介: ...它不是具体的缓存实现,它只提供一整套的接口和代码规范、配置、注解等,用于整合各种缓存方案,比如Caffeine、Guava Cache、Ehcache。
NULL 博文链接:https://topmanopensource.iteye.com/blog/752948