ehcache 是一个纯Java的进程内缓存框架,具备快速、精干等特色,是Hibernate中默认的CacheProvider
它具备内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,支持REST和SOAP api等特色html
算法名称 | 存在的问题 |
---|---|
LRU | 会存在一次冷数据的批量查询而误淘汰大量热点的数据 |
LFU | 会致使最近新加入的数据总会被很容易被剔除掉 |
FIFO | 这种算法有其特殊的使用领域,好比在做业调度、消息队列等方面 |
ehcache.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="true" name="shiroCache">
<diskStore path="/data/html/ehcache/sh" /><!--达到内存上限后缓存文件保存位置-->
<defaultCache maxElementsInMemory="10000" <!--cache 中最多能够存放的元素的数量-->
eternal="false"<!--是否永驻内存。若是值是true,cache中的元素将一直保存在内存中,不会由于时间超时而丢失,因此在这个值为true的时候,timeToIdleSeconds和timeToLiveSeconds两个属性的值就不起做用了-->
timeToIdleSeconds="120"<!--访问这个cache中元素的最大间隔时间。若是超过这个时间没有访问这个cache中的某个元素,那么这个元素将被从cache中清除-->
timeToLiveSeconds="86400"<!--cache中元素的生存时间。意思是从cache中的某个元素从建立到消亡的时间,从建立开始计时,当超过这个时间,这个元素将被从cache中清除-->
overflowToDisk="true" <!--溢出是否写入磁盘-->
diskSpoolBufferSizeMB="30" <!--设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB-->
maxElementsOnDisk="10000000" <!--磁盘中最大元素数量 -->
diskPersistent="false" <!--是否持久化磁盘缓存-->
diskExpiryThreadIntervalSeconds="120" <!--磁盘缓存的清理线程运行间隔-->
memoryStoreEvictionPolicy="LRU" <!--内存存储与释放策略-->
/>
<cache name="fillSettleData" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false" />
</ehcache>
复制代码
结合spring使用spring
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory" />
</bean>
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManagerFactory"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="WEB-INF/env/ehcache.xml" />
<property name="shared" value="true" />
</bean>
复制代码