ehcache配置

1、maven pom文件增加依赖

  <!-- ehcache -->
      <dependency>
          <groupId>com.googlecode.ehcache-spring-annotations</groupId>
          <artifactId>ehcache-spring-annotations</artifactId>
          <version>1.2.0</version>
          <exclusions>
              <exclusion>
                  <artifactId>spring-aop</artifactId>
                  <groupId>org.springframework</groupId>
              </exclusion>
              <exclusion>
                  <artifactId>spring-expression</artifactId>
                  <groupId>org.springframework</groupId>
              </exclusion>
              <exclusion>
                  <artifactId>spring-core</artifactId>
                  <groupId>org.springframework</groupId>
              </exclusion>
              <exclusion>
                  <artifactId>spring-beans</artifactId>
                  <groupId>org.springframework</groupId>
              </exclusion>
              <exclusion>
                  <artifactId>spring-context</artifactId>
                  <groupId>org.springframework</groupId>
              </exclusion>
              <exclusion>
                  <artifactId>guava</artifactId>
                  <groupId>com.google.guava</groupId>
              </exclusion>
              <exclusion>
                  <artifactId>slf4j-api</artifactId>
                  <groupId>org.slf4j</groupId>
              </exclusion>
          </exclusions>
      </dependency>

2、配置spring 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"
       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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
    <ehcache:annotation-driven />
    <ehcache:config cache-manager="cacheManager">
        <ehcache:evict-expired-elements interval="60" />
    </ehcache:config>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation"  value="classpath:ehcache-config.xml"/>
    </bean>
</beans>

 

3、配置ehcache-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 updateCheck="false">
 <diskStore path="user.home/ehcacheDiskStore" />
 <defaultCache eternal="false" maxElementsInMemory="10000" overflowToDisk="false" diskPersistent="false"
  timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
 
 <!-- 一个小时过期的缓存 -->
 <cache name="LRUCache-1h" eternal="false" maxElementsInMemory="4000" overflowToDisk="false" maxElementsOnDisk="1000"
  diskPersistent="false" diskExpiryThreadIntervalSeconds="100" timeToIdleSeconds="0" timeToLiveSeconds="3600"
  memoryStoreEvictionPolicy="LRU" />
 <!-- 一天过期的缓存 -->
 <cache name="LRUCache-1d" copyOnRead="true" eternal="false" maxElementsInMemory="4000" overflowToDisk="false" maxElementsOnDisk="1000"
  diskPersistent="false" diskExpiryThreadIntervalSeconds="100" timeToIdleSeconds="0" timeToLiveSeconds="86400"
  memoryStoreEvictionPolicy="LRU" />
 <!-- 十天过期的缓存 -->
 <cache name="LRUCache-10d" copyOnRead="true" eternal="false" maxElementsInMemory="4000" overflowToDisk="false" maxElementsOnDisk="1000"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="100" timeToIdleSeconds="0" timeToLiveSeconds="864000"
        memoryStoreEvictionPolicy="LRU" />
    <!-- 5分钟过期的缓存 -->
 <cache name="LRUCache-5m" eternal="false" maxElementsInMemory="4000" overflowToDisk="false" maxElementsOnDisk="1000"
  diskPersistent="false" diskExpiryThreadIntervalSeconds="100" timeToIdleSeconds="0" timeToLiveSeconds="300"
  memoryStoreEvictionPolicy="LRU" />
</ehcache>

 

4、使用注解

    @Cacheable(cacheName="LRUCache-5m") 

    @TriggersRemove(cacheName="LRUCache-5m",removeAll=true)


5、关键参数解读

    

name - cache的标识符,在一个CacheManager中必须唯一
maxElementsInMemory - 在内存中缓存的element的最大数目
maxElementsOnDisk - 在磁盘上缓存的element的最大数目
eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上

以下属性是可选的:
timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.
timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期
diskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒.
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO

evict-expired-elements interval="60" - interval is in minutes,失效的缓存,ehcache并没有立即清除,要么在下次访问的时候清除,要么在triggersRemove调用的时候清除,要么就是在Ehcache#evictExpiredElements()被调用的时候清除,这个配置在xml里头,时间单位是分钟,每隔多少分钟定时清理一次。

6、ehcache-spring的key生成策略

   默认采用的是 HashCodeCacheKeyGenerator 

   基于方法参数的hashkey,类+方法名+参数hashCode

转载于:https://my.oschina.net/scipio/blog/224037

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值