借助Spring Module+EHCache,实现配置声明性缓存功能

前言:

本文档将讲解一下,如何借助Spring Module项目,实现配置声明性缓存功能。

说明:

本档的配置经过本人测试,都能正确运行。

运行环境: Jdk6.0,
Spring-2.5,
Spring-modules-0.9,
ehcache-1.5.0.jar,
cglib-nodep-2.1_3.jar
backport-util-concurrent.jar
oro-2.0.8.jar

首先创建一个PortalMenuService服务类,本文将对其所有的以find* 方式命令的方法,进行缓存处理。当调用update* 命令时,需要其删除缓存以更做数据的更新。


public class PortalMenuService{

private String name = "matthew";

public void findFavMenuByName(String name) {
//code
}

public void updateFavMenuByName(String name) {
//code
}
}



接下来,就是编写Spring配置文件 context.xml,以实现根据上面的要求,进行声明性缓存功能的配置


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans [url]http://www.springframework.org/schema/beans/spring-beans-2.5.xsd[/url]">

<!-- Using a EHCache cache manager -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<!--<property name="cacheManagerName" value="mainCache"/>-->
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>

<!-- 使用 Spring Modules对 EhCache的封装 -->
<bean id="cacheProviderFacade" class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
<property name="cacheManager" ref="cacheManager" />
</bean>

<!-- 配置 方法 拦截器 -->
<!-- 缓存拦截器 -->
<bean id="cachingInterceptor"
class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor">
<property name="cacheProviderFacade" ref="cacheProviderFacade" />
<property name="cachingModels"> <!-- 进行cache缓存 -->
<props> <!-- 所有PortalMenuService对象中,以find开头的方法都将进行缓存 -->
<prop key="PortalMenuService.find*">cacheName=testCache</prop>
</props>
</property>
</bean>

<!-- 缓存刷新拦截器 -->
<bean id="flushingInterceptor"
class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor">
<property name="cacheProviderFacade" ref="cacheProviderFacade" />
<property name="flushingModels"><!-- 进行cache刷新(清除) -->
<props>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.flushPortalMenuCache*">cacheNames=portalCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.updateMenuNode*">cacheNames=portalCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.updateFunction*">cacheNames=portalCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.flushUsageStaticCache*">cacheNames=usageStaticCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.flushUserFuncHitsDataForChartCache*">cacheNames=userFuncHitsDataForChartCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.flushFunctionDataForChartCache*">cacheNames=functionDataForChartCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.deActivateESAT*">cacheNames=esatCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.activateESAT*">cacheNames=esatCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.updateUserEntryPage*">cacheNames=favMenuCache,recentMenuCache,userDefaultCache,favorConfigCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.updateFavoriteMenu*">cacheNames=favMenuCache,recentMenuCache,userDefaultCache,favorConfigCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.setFavoriteMenuConfig*">cacheNames=favMenuCache,recentMenuCache,userDefaultCache,favorConfigCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.setShareFavoriteConfig*">cacheNames=favMenuCache,recentMenuCache,userDefaultCache,favorConfigCache</prop>
<prop key="com.citi.risk.portal.ui.client.util.NavigationMenuUtil.stopShareFavoriteConfig*">cacheNames=favMenuCache,recentMenuCache,userDefaultCache,favorConfigCache</prop>
</props>
</property>
</bean>

<!-- 配置 基于BeanName规则的动态代理封装 -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>portalMenuService</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>cachingInterceptor</value>
<value>flushingInterceptor</value>
</list>
</property>
</bean>

<bean id="portalMenuService" class="PortalMenuService"></bean>
</beans>


接下来,为能让EhCache能正常工作,还得编写EhCache配置文件 ehcache.xml, 内容如下:

<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="2" timeToLiveSeconds="5" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<cache name="testCache" maxElementsInMemory="10000"
maxElementsOnDisk="1000" eternal="false" overflowToDisk="false"
diskSpoolBufferSizeMB="20" timeToIdleSeconds="60" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU" />
</ehcache>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值