基于springmodules的缓存方案

来源:互联网 发布:公司网络不能听音乐 编辑:程序博客网 时间:2024/05/16 08:34

简介    

      通常在系统开发中,必不可少的要使用到缓存(Cache),如用户信息、字典信息都会使用缓存来提高性能;但是如何使用好缓存是个需要深入研究的话题,缓存方案没有通用性,针对不同的应用层面,缓存的设计通常也是千差万别的!这里只是介绍了一种比较轻量级、无侵入的缓存方案,该方案基于Spring+SpringModules。

目的

  1. 方法级别的缓存
  2. 声明式、无侵入
  3. 不绑定缓存框架
  4. JDK 1.4/1.5均适用

实现

基于JDK1.4

      JDK1.4中可使用方法映射、元数据(commons-attributes)两种方式声明需要缓存的方法。由于元数据需要导入额外的包,今后JDK升级后无法转换为annotation,且commons-attributes包本身有点BUG,故不在本文讨论之中。

定义缓存实现

目前支持的实现有:

  • org.springmodules.cache.provider.jboss.JbossCacheManagerFactoryBean

  • org.springmodules.cache.provider.jcs.JcsManagerFactoryBean

  • org.springmodules.cache.provider.oscache.OsCacheManagerFactoryBean

  • org.springframework.cache.ehcache.EhCacheManagerFactoryBean 

Xml代码  收藏代码
  1. <!-- 缓存实现管理器 -->  
  2. <bean id="cacheManager"  
  3.                 class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
  4. </bean>  

  

定义缓存接口

 

Xml代码  收藏代码
  1. <!-- 缓存统一接口 -->  
  2. <bean id="cacheProviderFacade"  
  3.     class="org.springmodules.cache.provider.ehcache.EhCacheFacade">  
  4.     <property name="cacheManager" ref="cacheManager" />  
  5. </bean>  

 

定义缓存拦截器

Xml代码  收藏代码
  1. <bean id="cachingInterceptor"  
  2.     class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor">  
  3.     <property name="cacheProviderFacade" ref="cacheProviderFacade" />  
  4.     <property name="cachingModels">  
  5.         <props>  
  6.             <prop key="com.cidp.system.service.IDictService.load*">cacheName=dictCache</prop>  
  7.         </props>            
  8.     </property>  
  9. </bean>  

 

定义刷新拦截器

Xml代码  收藏代码
  1. <bean id="flushingInterceptor"  
  2.     class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor">  
  3.     <property name="cacheProviderFacade" ref="cacheProviderFacade" />       
  4.     <property name="flushingModels">  
  5.         <props>  
  6.             <prop key="com.cidp.system.service.IDictService.update*">cacheNames=dictCache</prop>  
  7.         </props>  
  8.     </property>  
  9. </bean>  

 

为业务层添加缓存拦截器

Xml代码  收藏代码
  1. <!-- 注册自动代理创建,为业务Bean添加事务拦截器 -->  
  2. <bean id="ServiceAutoProxyCreator"  
  3.     class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  4.     <property name="proxyTargetClass" value="false"></property>  
  5.     <property name="beanNames">  
  6.         <list>  
  7.             <value>*Service</value>  
  8.         </list>  
  9.     </property>  
  10.     <property name="interceptorNames">  
  11.         <list>  
  12.             <value>cachingInterceptor</value>  
  13.             <value>flushingInterceptor</value>                                
  14.         </list>  
  15.     </property>  
  16. </bean>  

  

基于JDK1.5

      JDK 1.5中出现了annotation这样的好东西,使得我们可以在方法级别上做更精细的控制,将基于XML配置转入源代码中。

 

注:缓存实现和缓存接口配置同上,拦截器绑定同上

定义缓存拦截器

Xml代码  收藏代码
  1. <bean id="cachingAttributeSource"  
  2.   class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource">  
  3. </bean>  
  4.   
  5. <bean id="cachingInterceptor"  
  6.     class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor">  
  7.     <property name="cacheProviderFacade" ref="cacheProviderFacade" />  
  8.     <property name="cachingAttributeSource" ref="cachingAttributeSource" />         
  9.     <property name="cachingModels">  
  10.         <props>  
  11.             <prop key="dictCaching">cacheName=dictCache</prop>  
  12.         </props>            
  13.     </property>  
  14. </bean>  

 

定义刷新拦截器

 

Xml代码  收藏代码
  1. <bean id="flushingAttributeSource"  
  2.   class="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource">  
  3. </bean>     
  4.   
  5. <bean id="flushingInterceptor"  
  6.     class="org.springmodules.cache.interceptor.flush.MetadataCachingInterceptor">  
  7.     <property name="cacheProviderFacade" ref="cacheProviderFacade" />   
  8.     <property name="flushingAttributeSource" ref="flushingAttributeSource" />           
  9.     <property name="flushingModels">  
  10.         <props>  
  11.             <prop key="dictFlushing">cacheNames=dictCache</prop>  
  12.         </props>  
  13.     </property>  
  14. </bean>  

 

Java代码

Java代码  收藏代码
  1. @Cacheable(modelId = "dictCaching")  
  2. public Dict load(Long id);    
  3.   
  4.   
  5. @CacheFlush(modelId = "dictFlushing")  
  6. public void update(Dict dict);  

 

 

ehcache 

      springmodules支持<ehcache>标签形式的配置方式,可简化以上用<bean>定义拦截器的方式。如果项目中使用的缓存框架为ehcache,可通过该标签简化配置

 

0 0