- 2
- 0
- 约5.23千字
- 约 6页
- 2017-03-18 发布于重庆
- 举报
Ehcache缓存配置
Ehcache缓存配置?
Cache的配置很灵活,官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。
你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:
????在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。
????发布时可更改Cache配置。
????可再安装阶段就检查出配置错误信息,而避免了运行时错误。
本文将会对ehcache.xml配置文件进行详细的阐述。在配置的时可以拷贝一个现有的ehcache.xml,如果没有请点击这里去下载。
ehcache-failsafe.xml 如果你调用了CacheManager默认构造方法去创建CacheManager的实例,此方法会到classpath中找ehcache.xml文件,否则它会到类路径下找ehcache-failsafe.xml文件。而ehcache-failsafe.xml被包含在jar包中,所有它肯定能找的到。
ehcache-failsafe.xml提供了一个非常简单的默认配置,这样可以使用户在没有创建ehcache.xml的情况下使用Ehcache。
不过这样做Ehcache会提醒用户创建一个正确的Ehcache配置。
ehcache.xml片段: diskStore path java.io.tmpdir/ defaultCache maxElementsInMemory 10000 eternal false timeToIdleSeconds 120 timeToLiveSeconds 120 overflowToDisk true maxElementsOnDiskdiskPersistent false diskExpiryThreadIntervalSeconds 120 memoryStoreEvictionPolicy LRU / ehcache.xml和其他配置文件 在Ehcache-1.6之前的版本,只支持ASCII编码的ehcache.xml配置文件。在Ehcach-1.6之后版本中,支持UTF8编码的ehcache.xml配置文件。因为向后兼容,所有采用ASCII编码的配置文件完全没有必要转换为UTF8。
一个CacheManager必须要有一个XML配置。由于磁盘路径或是监听端口,多个CacheManager使用同一个配置文件时会出现错误。
下面是ehcache.xml具体实例以及配置指南
ehcache xmlns:xsi /2001/XMLSchema-instance
????CacheManager配置
DmulticastGroupPort 4446,这样可以配置监听端口。
????DiskStore配置
如果你使用的DiskStore(磁盘缓存),你必须要配置DiskStore配置项。如果不配置,Ehcache将会使用java.io.tmpdir。
diskStroe的“path”属性是用来配置磁盘缓存使用的物理路径的,Ehcache磁盘缓存使用的文件后缀名是.data和.index。
disStore path ”java.io.tmpdir”/
????CacheManagerEventListener配置
我们通过CacheManagerEventListenerFactory可以实例化一个CacheManagerPeerProvider,当我们从CacheManager中added和removed Cache时,将通知CacheManagerPeerProvider,这样一来,我们就可以很方面的对CacheManager中的Cache做一些统计。
注册到CacheManager的事件监听类名有: adding a Cache和removing a Cache
cacheManagerEventListenerFacotory class ”” properties ””/
????CacheManagerPeerProvider配置
在集群中CacheManager配置CacheManagerPeerProviderFactory创建CacheManagerPeerProvider。具体的实例如下:
cacheManagerPeerProviderFactoryclass net.sf.ehcache.distribution.
RMICacheManagerPeerProviderFactory
properties peerDiscovery manual, rmiUrls //server1:4000
原创力文档

文档评论(0)