springMvc整合ehcache

pom文件

    <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>${ehcache.core}</version>
        </dependency>

    <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-jgroupsreplication</artifactId>
            <version>1.7</version>
        </dependency>

 

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>${hibernate.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>net.sf.ehcache</groupId>
                    <artifactId>ehcache-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

1.在web文件下配置spring-cache.xml的路径

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:config/spring/spring-application.xml,
            classpath:config/spring/spring-support.xml,
            classpath:config/spring/spring-mybatis.xml,
            classpath:config/spring/spring-cache.xml,
            classpath:config/spring/spring-quartz.xml
        </param-value>
    </context-param>

2.配置spring-cache.xm文件 

注意:spring-cache.xml 要放在resources文件下不能有子文件

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

  <description>ehcache缓存配置管理文件</description>
 <!-- 启用缓存注解开关 -->
 <cache:annotation-driven cache-manager="cacheManager"/>
  <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>

   <!-- 这里不加上或者改成false,项目会启动不起来,还没找到问题 -->
     <property name="shared" value="true"></property>  
  </bean>

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

2.配置ehcache.xml文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd">
     <diskStore path="D:/tempCase1" />
     <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="18000" timeToLiveSeconds="36000" overflowToDisk="true"
        maxElementsOnDisk="10000000" diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

    <cache name="plateform" maxElementsInMemory="200000" eternal="false"
        timeToIdleSeconds="18000" timeToLiveSeconds="36000" overflowToDisk="true" />
</ehcache>

3.(1)在serviceIpml上使用注解

  @Override
  @Cacheable(value = "plateform", key = "'house_'+#cmutyId+'_'+ #uid")
  public List<User> find
User(long cmutyId, long uid) {
    System.err.println("运行数据库。。。。。");
  }

 (2)@CacheEvict的方法和@Cache的方法放到一个java文件中写,他俩在两个java文件的话,会导致@CacheEvict失效@CacheEvict注解的方法必须是controller层直接调用,service里间接调用不生效

参考文章:http://www.cnblogs.com/jingmoxukong/p/5975994.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值