下面说明一下ehcache和spring整合配置。java
1. 须要的jar包spring
slf4j-api-1.6.1.jarapi
ehcache-core-2.1.0.jar缓存
ehcache-spring-annotations-1.1.2.jarapp
slf4j-log4j12-1.6.1.jarspa
spring-context-support-4.0.6.RELEASE.jar线程
2. ehcache.xmlxml
<?xml version="1.0" encoding="UTF-8"?>对象
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">接口
<diskStore path="java.io.tmpdir/ehcache"/>
<!-- 默认缓存 -->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"/>
<!-- 菜单缓存 -->
<cache name="menuCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
参数说明:
<diskStore>:当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口)。
<diskStore path="">:用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index。
name:缓存名称,cache的惟一标识(ehcache会把这个cache放到HashMap里)。
maxElementsOnDisk:磁盘缓存中最多能够存放的元素数量,0表示无穷大。
maxElementsInMemory:内存缓存中最多能够存放的元素数量,若放入Cache中的元素超过这个数值,则有如下两种状况。
1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中。
2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素。
Eternal:缓存中对象是否永久有效,便是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds。
timeToIdleSeconds:缓存数据在失效前的容许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除。
timeToLiveSeconds:缓存数据在失效前的容许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大,即Cache中的某元素从建立到清楚的生存时间,也就是说从建立开始计时,当超过这个时间时,此元素将从Cache中清除。
overflowToDisk:内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中),会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data。
diskPersistent:是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查 找文件名为cache名称,后缀名为index的文件,这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存, 要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法。
diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔,默认是120秒。
diskSpoolBufferSizeMB:设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB
memoryStoreEvictionPolicy:内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存,共有三种策略,分别为LRU(最近最少使用)、LFU(最经常使用的)、FIFO(先进先出)。
3. application_spring_cache.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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
<cache:annotation-driven cache-manager="cacheManager"/>
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:application/ehcache.xml" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
</beans>
4. 使用
首先在ehcache.xml中配置缓存策略,即添加一组cache。
注意事项:
<cache:annotation-driven/>还能够指定一个mode属性,可选值有proxy和aspectj。默认是使用proxy。当mode为proxy时,只有缓存方法在外部被调用的时候Spring Cache才会发生做用,也就是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的。
使用proxy时,只有public方法上的@Cacheable等标注才会起做用,若是须要非public方法上的方法也能够使用Spring Cache时把mode设置为aspectj。
4.1 添加缓存
@Cacheable(value="menuCache",key="'UserMenuKey'+#userid")
public List<MenuBean> queryMenuByUserId(String userid)
{
……
}
@Cacheable主要针对方法配置,可以根据方法的请求参数对其结果进行缓存。