基于springmodules的缓存方案

简介    

      通常在系统开发中,必不可少的要使用到缓存(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);  
@Cacheable(modelId = "dictCaching")
public Dict load(Long id);	


@CacheFlush(modelId = "dictFlushing")
public void update(Dict dict);

 

 

ehcache 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:oscache="http://www.springmodules.org/schema/oscache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 mvc:annotation-driven --> <mvc:annotation-driven/> <!-- 扫描包 --> <context:annotation-config/> <context:component-scan base-package="com.org.*" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <value>classpath:properties/jdbc.properties</value> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1"/> <property name="maxActive" value="500"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <!-- 配置sessionFactory 注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.org.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- 配置hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Spring AOP config配置切点 --> <aop:config> <aop:pointcut expression="execution(public * com.org.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> <!-- 配置那个类那个方法用到事务处理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 这个映射配置主要是用来进行静态资源的访问 --> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/resource/**" location="/resource/" /> <mvc:resources mapping="/jsp/**" location="/jsp/" /> </beans>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值