关于ehcache 配置

欠了很久的文章,本来都不想发了,可还是发吧

ehcache的默认配置文件名为ehcache.xml,也可以自己指定。配置文件比较的简单,基本上也就是常用的几个元素。

配置元素说明:

diskStore:配置DiskStore,当需要使用磁盘保存的时候,需要对象实现序列化接口

    属性:path 配置文件存储位置,如user.home,user.dir,java.io.tmpdir

cacheManagerEventListenerFactory :指定CacheManagerEventListenerFactory,用于创建CacheManagerPeerProvider

用于提醒当CacheManager中的缓存增加或者删除时。
   
    属性:class:完整的工厂类名称
         properties:逗号分割的属性

事件包括 增加cache和删除cache

cacheManagerPeerProviderFactory:指定CacheManagerPeerProviderFactory,用于创建 CacheManagerPeerProvider,用于在cluster中查找CacheManagers,主要用于机群环境

    属性:
        class:完整的工厂类名称
        properties:逗号分割的属性

默认的配置:
 
xml 代码
 
  1. <cacheManagerPeerProviderFactory  
  2.                                 class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  3.                                 properties="peerDiscovery=automaticmulticastGroupAddress=230.0.0.1,  
  4.                                     multicastGroupPort=4446timeToLive=32"/>  
  5.         
  6. <cacheManagerPeerProviderFactory class=  
  7.                           "net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  8.                           properties="peerDiscovery=manual,  
  9.                           rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1  
  10.                           | //server1:40000/sampleCache2|//server2:40000/sampleCache2"/>       


cacheManagerPeerListenerFactory:指定CacheManagerPeerListenerFactory,用于创建CacheManagerPeerListener,监听cluster中的复制信息
    属性:
        class:完整的工厂类名称
        properties:逗号分割的属性

默认配置
 
  
xml 代码
 
  1. <cacheManagerPeerListenerFactory  
  2.         class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  
  3.         properties="hostName=fully_qualified_hostname_or_ip,  
  4.                     port=40001,  
  5.                     socketTimeoutMillis=120000"/>       


defaultCache: 默认缓存配置

    必须属性:
        name:设置缓存的名称,用于标志缓存,惟一
        maxElementsInMemory:在内存中最大的对象数量
        maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制
        eternal:设置元素是否永久的,如果为永久,则timeout忽略
        overflowToDisk:是否当memory中的数量达到限制后,保存到Disk

    可选的属性:
        timeToIdleSeconds:设置元素过期前的空闲时间
        timeToLiveSeconds:设置元素过期前的活动时间
        diskPersistent:是否disk store在虚拟机启动时持久化。默认为false
        diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒
        memoryStoreEvictionPolicy:策略关于Eviction

缓存子元素:
   
    cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire
    bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。
如下的例子:
 
 
xml 代码
 
  1. <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"  
  2.          properties="replicateAsynchronously=true,  
  3.          replicatePuts=true,  
  4.          replicateUpdates=true,  
  5.          replicateUpdatesViaCopy=true,  
  6.          replicateRemovals=true "/>       
  7.    
  8. <bootstrapCacheLoaderFactory  
  9.         class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"  
  10.         properties="bootstrapAsynchronously=true, maximumChunkSizeBytes=5000000"/>       
  11.    
  12.  <defaultCache  
  13.             maxElementsInMemory="10000"  
  14.             eternal="false"  
  15.             timeToIdleSeconds="120"  
  16.             timeToLiveSeconds="120"  
  17.             overflowToDisk="true"  
  18.             maxElementsOnDisk="10000000"  
  19.             diskPersistent="false"  
  20.             diskExpiryThreadIntervalSeconds="120"  
  21.             memoryStoreEvictionPolicy="LRU"  
  22.             />       


cache配置同defaultCache
 
xml 代码
 
  1. <cache name="sampleDistributedCache1"  
  2.            maxElementsInMemory="10"  
  3.            eternal="false"  
  4.            timeToIdleSeconds="100"  
  5.            timeToLiveSeconds="100"  
  6.            overflowToDisk="false">  
  7.         <cacheEventListenerFactory  
  8.                 class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>  
  9.         <bootstrapCacheLoaderFactory  
  10.                 class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>  
  11.     </cache>       


基本上都比较简单,当然也可以直接使用程序实现相应的功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xmlehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar包下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值