SSM架构下ehcatch缓存的配置

1.先在内存中开辟一块缓存区

配置如下:
SSM架构下ehcatch缓存的配置_第1张图片


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">

    <diskStore path="java.io.tmpdir" />

    <defaultCache eternal="false" 
        maxElementsInMemory="1000"
        overflowToDisk="true" 
        diskPersistent="false" 
        timeToIdleSeconds="0"
        timeToLiveSeconds="600" 
        memoryStoreEvictionPolicy="LFU" />

    <cache name="baseCache" 
        eternal="false" 
        maxElementsInMemory="3000"
        overflowToDisk="true" 
        diskPersistent="false" 
        timeToIdleSeconds="0"
        timeToLiveSeconds="300" 
        memoryStoreEvictionPolicy="LFU" />


 
ehcache>


注意事项:
SSM架构下ehcatch缓存的配置_第2张图片

2.集成到spring容器中

配置如下:
SSM架构下ehcatch缓存的配置_第3张图片

  
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:oxm="http://www.springframework.org/schema/oxm"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/mvc   
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd    
        http://www.springframework.org/schema/beans   
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd    
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-3.2.xsd  
        http://www.springframework.org/schema/cache   
        http://www.springframework.org/schema/cache/spring-cache.xsd">   



      
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
        <property name="configLocation"  value="classpath:ehcache.xml"/>   
    bean>   

      
    <cache:annotation-driven cache-manager="cacheManager" />  

      
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    
        <property name="cacheManager"  ref="cacheManagerFactory"/>    
    bean>    
beans>   

3.开启spring扫描

配置在最下面
SSM架构下ehcatch缓存的配置_第4张图片



<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                        http://www.springframework.org/schema/task  
                        http://www.springframework.org/schema/task/spring-task-3.0.xsd
                        http://www.springframework.org/schema/aop  
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config.propertiesvalue>
                <value>classpath:jdbc.propertiesvalue>
            list>
        property>
    bean>

    <context:component-scan base-package="com.hjcrm">
        
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    context:component-scan>


    <task:annotation-driven/>  

    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      <property name="driverClassName" value="${driver}" />
      <property name="url" value="${url}" />
      <property name="username" value="${username}" />
      <property name="password" value="${password}" />
      
      <property name="initialSize" value="30" />
      
      <property name="maxActive" value="1000" />
      
      <property name="maxIdle" value="50" />
      
      <property name="minIdle" value="30" />
      
      <property name="validationQuery" value="select 1" />
      
      <property name="testOnBorrow" value="true" />
      <property name="removeAbandonedTimeout" value="120" />
      <property name="removeAbandoned" value="true" />
      
      <property name="timeBetweenEvictionRunsMillis" value="3600000" />
      
      <property name="minEvictableIdleTimeMillis" value="3600000" />  
    bean>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    
    <bean id="cfgSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="dataSource" />
    bean>
    <bean id="cfgDataAccess" class="com.hjcrm.publics.dao.DataAccessImpl" >
        <constructor-arg index="0" ref="cfgSqlSessionFactory" />
    bean>

    <bean id="springUtil" class="com.hjcrm.publics.util.ContextUtil" depends-on="cfgDataAccess">
        <property name="configPath" value="config.properties,jdbc.properties" />
    bean>

    
    <import resource="classpath:applicationContext-ehcache.xml"/> 
    

beans>  

SSM架构下ehcatch缓存的配置_第5张图片

@Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
@CachePut 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存,和 @Cacheable 不同的是,它每次都会触发真实方法的调用(不常用)
—-key=”#user”是从实体类或者传进来的值中“动态取值”

如果需要更新缓存中的内容操作如下:

调用更新方法保持key值一样,
在更新方法上面加上缓存注解 (跟查询的缓存注解不一样)
@CachEvict(value = "baseCache",key="#e.userid")清空baseCache 缓存

@CachEvict 的作用 主要针对方法配置,能够根据一定的条件对缓存进行清空

e 是指更新方法中形参中实体类
例:
………..update(User e){
方法体
}

清空缓存后执行下面的更新方法
在执行查询方法时把新的结果集放到value=“baseCache”中
value的值”baseCache”是ehcache.xml配置文件中标签中name属相的值
如图:1、2
SSM架构下ehcatch缓存的配置_第6张图片
图1
SSM架构下ehcatch缓存的配置_第7张图片
图2

缓存标志详解链接:(@Cacheable、@CachePut、@CacheEvict)
http://blog.csdn.net/whatlookingfor/article/details/51833378

你可能感兴趣的:(springmvc,catch,ssm)