1.配置ehcache.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ehcache updateCheck="false" dynamicConfig="false"> 3 <diskStore path="java.io.tmpdir"/> 4 5 <cache name="authorizationCache" 6 maxEntriesLocalHeap="2000" 7 eternal="false" 8 timeToIdleSeconds="1800" 9 timeToLiveSeconds="1800" 10 overflowToDisk="false" 11 statistics="true"> 12 </cache> 13 14 <cache name="authenticationCache" 15 maxEntriesLocalHeap="2000" 16 eternal="false" 17 timeToIdleSeconds="1800" 18 timeToLiveSeconds="1800" 19 overflowToDisk="false" 20 statistics="true"> 21 </cache> 22 23 <cache name="activeSessionCache" 24 maxEntriesLocalHeap="2000" 25 eternal="false" 26 timeToIdleSeconds="1800" 27 timeToLiveSeconds="1800" 28 overflowToDisk="false" 29 statistics="true"> 30 </cache> 31 32 <!-- 缓存半小时 --> 33 <cache name="halfHour" 34 maxElementsInMemory="10000" 35 maxElementsOnDisk="100000" 36 eternal="false" 37 timeToIdleSeconds="1800" 38 timeToLiveSeconds="1800" 39 overflowToDisk="false" 40 diskPersistent="false" /> 41 42 <!-- 缓存一小时 --> 43 <cache name="hour" 44 maxElementsInMemory="10000" 45 maxElementsOnDisk="100000" 46 eternal="false" 47 timeToIdleSeconds="10" 48 timeToLiveSeconds="10" 49 overflowToDisk="false" 50 diskPersistent="false" /> 51 52 <!-- 缓存一天 --> 53 <cache name="oneDay" 54 maxElementsInMemory="10000" 55 maxElementsOnDisk="100000" 56 eternal="false" 57 timeToIdleSeconds="86400" 58 timeToLiveSeconds="86400" 59 overflowToDisk="false" 60 diskPersistent="false" /> 61 62 <!-- 63 name:缓存名称。 64 maxElementsInMemory:缓存最大个数。 65 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 66 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 67 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 68 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 69 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 70 maxElementsOnDisk:硬盘最大缓存个数。 71 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. 72 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 73 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 74 clearOnFlush:内存数量最大时是否清除。 75 --> 76 <defaultCache name="defaultCache" 77 maxElementsInMemory="10000" 78 eternal="false" 79 timeToIdleSeconds="120" 80 timeToLiveSeconds="120" 81 overflowToDisk="false" 82 maxElementsOnDisk="100000" 83 diskPersistent="false" 84 diskExpiryThreadIntervalSeconds="120" 85 memoryStoreEvictionPolicy="LRU"/> 86 87 </ehcache>
2.配置spring-ehcache.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:cache="http://www.springframework.org/schema/cache" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/cache 8 http://www.springframework.org/schema/cache/spring-cache.xsd"> 9 <!-- Spring提供的基于的Ehcache实现的缓存管理器 --> 10 11 <!-- 如果有多个ehcacheManager要在bean加上p:shared="true" --> 12 <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 13 <property name="configLocation" value="classpath:xml/ehcache.xml"/> 14 </bean> 15 16 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 17 <property name="cacheManager" ref="ehcacheManager"/> 18 <property name="transactionAware" value="true"/> 19 </bean> 20 21 <!-- cache注解,和spring-redis.xml中的只能使用一个 --> 22 <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/> 23 </beans>
3、测试
1 package com.wangzhixuan.service.impl; 2 3 import java.io.Serializable; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.cache.annotation.Cacheable; 9 import org.springframework.stereotype.Service; 10 11 import com.wangzhixuan.mapper.UserMapper; 12 import com.wangzhixuan.model.User; 13 14 @Service 15 public class TestService { 16 @Autowired 17 private UserMapper userMapper; 18 19 @Cacheable(value = "hour", key = "#id") 20 public User selectById(Serializable id) { 21 return userMapper.selectById(id); 22 } 23 24 @Cacheable(value = "hour") 25 public String get(){ 26 Date d=new Date(); 27 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 28 return s.format(d); 29 } 30 }