因为公司的项目用到了ehcache缓存,我也简单的学了下,知识在于总结嘛~~在这记下自己的理解吧
缓存的使用就是为了解决服务端部分的压力,用户请求的数据直接从缓存里取,而不用去访问数据库查询,这肯定是大大减少了时间的,在大数据量下的查询是非常耗时,所以缓
存这个东西非常棒,但是,用它的过程中没配置好,那麻烦也是大大的,各种数据乱~~
下面先放一个ehcache.xml配置吧,这里配置讲解的已经很详细了。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
monitoring="autodetect">
<!--
diskStore :指定数据存储位置,可指定磁盘中的文件夹位置
defaultCache : 默认的管理策略
以下属性是必须的:
name: Cache的名称,必须是唯一的(ehcache会把这个cache放到HashMap里)。maxElementsInMemory:在内存中缓存的element的最大数目。
maxElementsOnDisk:在磁盘上缓存的element的最大数目,默认值为0,表示不限制。
eternal:设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断。
overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上。
以下属性是可选的:
timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效。只对eternal为false的有效。默认值0,表示一直可以访问。
timeToLiveSeconds: 对象存活时间,指对象从创建到失效所需要的时间。只对eternal为false的有效。默认值0,表示一直可以访问。
diskPersistent: 是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
diskExpiryThreadIntervalSeconds: 对象检测线程运行时间间隔。标识对象状态的线程多长时间运行一次。
diskSpoolBufferSizeMB: DiskStore使用的磁盘大小,默认值30MB。每个cache使用各自的DiskStore。
memoryStoreEvictionPolicy: 如果内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
缓存的3 种清空策略 :
FIFO ,first in first out (先进先出).
LFU , Less Frequently Used (最少使用).意思是一直以来最少被使用的。缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存。
LRU ,Least Recently Used(最近最少使用). (ehcache 默认值).缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
-->
<!--缓存路径-->
<diskStore path="/opt/cachetmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="10000000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
<!--接口缓存数据,包括终端,缓存有效时间为1h-->
<cache name="interfaceCache" maxElementsInMemory="200000"
maxElementsOnDisk="0" eternal="false" overflowToDisk="true"
diskSpoolBufferSizeMB="150" timeToIdleSeconds="1800000" timeToLiveSeconds="3600000"
memoryStoreEvictionPolicy="LFU" >
<!--
RMI缓存分布同步查找 class使用net.sf.ehcache.distribution.RMICacheReplicatorFactory
这个工厂支持以下属性:
replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers。默认是true。
replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。
replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。
replicateAsynchronously=true | false – 复制方式是否异步的。默认是true。
replicatePutsViaCopy=true | false – 当一个新增元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
asynchronousReplicationIntervalMillis=1000
-->
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=false,
replicateRemovals=true "/>
<!-- 用于在初始化缓存,以及自动设置-->
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>
</cache>
<!--主要用来缓存集成播控平台节目或介质信息,缓存有效时间为24h-->
<cache name="movieCache" maxElementsInMemory="500000"
maxElementsOnDisk="0" eternal="false" overflowToDisk="true"
diskSpoolBufferSizeMB="150" timeToIdleSeconds="0" timeToLiveSeconds="86400000"
memoryStoreEvictionPolicy="LFU" >
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
properties="replicateAsynchronously=true,
replicatePuts=true,
replicateUpdates=true,
replicateUpdatesViaCopy=false,
replicateRemovals=true "/>
<!-- 用于在初始化缓存,以及自动设置-->
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>
</cache>
<!--配置手动成员发现,多个成员中间用|隔开-->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//125.39.195.219:40001/interfaceCache|//125.39.195.219:40001/movieCache|//125.39.195.220:40002/interfaceCache|//125.39.195.220:40002/movieCache" />
<!--配置监听成员们发向当前CacheManager的消息-->
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=125.39.195.221, port=40002,
socketTimeoutMillis=2000"/>
</ehcache>