缓存之Ehcache

[size=medium][align=center]Ehcache分布式缓存配置[/align][/size]
一、简介
 1、从Hibernate项目发展起来,已经被Terracotta收购开发统一的缓存的解决放啊
 2、持续的维护和发布中
 3、标准的缓存支持(最大数配置、移除算法、磁盘持久化、重启缓存持久化)
 4、分布式缓存(RMI、Jgroups、JMS)
 5、集中式缓存(类似Memcached)
 6、页面缓存技术(OScache)
二、Ehcahe主要配置说明
maxElementsInMemory=10000 缓存最大数目
eternal="false" 缓存是否持久 
overflowToDisk="true" 是否保存到磁盘
timeToIdleSeconds="300" 多长时间不访问该缓存,ehcache就清除缓存
timeToLiveSeconds="180" 缓存的存活时间,从开始创建算起
三、缓存策略说明
1、FIFO:先进先出
2、LFU:一直以来使用最少的,缓存的元素有一个hit属性,hit值越小的将会被清除缓存
3、LRU:最近最少使用的,缓存的元素有个时间戳,当缓存容量满了,又腾不出地方来缓存新的元素的时候,那么现有缓存中时间戳离当前时间最远的元素将被清除缓存
四、分布式集群配置
ehcache提供三种网络连接策略来实现集群rmi,jgroup还有jms。这里只说rmi方式。
具体说明,配置cacheManagerPeerListenerFactory是配宿主主机配置监听程序来发现其他主机发来的同步请求配置cacheManagerPeerProviderFactory是指定除自身之外的网络群体中其他提供同步的主机列表用“|”分开不同的主机。
下面的例子的测试过程是主B缓存开启,并从名为UserCache的缓存中循环抓取键值为“key1”的元素。直到取到才退出循环。主机A缓存启动,并在名为UserCache的缓存中放入键值为“key1”的元素。显然,如果主机B取到的元素。那么就证明同步成功,也就是集群成功。所以在测试过程中先启动主机B的测试程序,在启动主机A的测试程序。
下面具体说配置文件:
1.主机A的配置文件
config/ehcache_cluster.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.1.254:40000/UserCache" />
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.126,port=40000,socketTimeoutMillis=120000" />
<defaultCache
maxElementsInMemory="10000"eternal="false
timeToIdleSeconds="120"timeToLiveSeconds="120"
overflowToDisk="true"diskSpoolBufferSizeMB="30"
maxElementsOnDisk="10000000"diskPersistent="false"
diskExpiryThreadIntervalSecon="120"
memoryStoreEvictionPolicy="LRU">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" <cacheEventListenerFactory />
<defaultCache/>
<cache name="UserCache"
maxElementsInMemory="1000"eternal="false" timeToIdleSeconds="100000"timeToLiveSeconds="100000"
overflowToDisk="false">
<cacheEventListenerFactor class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>
</ehcache>

2.主机B的配置文件
与A机类似,将 cacheManagerPeerProviderFactorcacheManagerPeerListenerFactory中的IP,端口互换即可。
3.分布式主要类RMICacheReplicatorFactory属性说明
 class使用net.sf.ehcache.distribution.RMICacheReplicatorFactory
这个工厂支持以下属性:
replicatePuts=true | false – 当一个新元素增加到缓存中的时候是否要复制到其他的peers。默认是true。
replicateUpdates=true | false – 当一个已经在缓存中存在的元素被覆盖时是否要进行复制。默认是true。
replicateRemovals= true | false – 当元素移除的时候是否进行复制。默认是true。
replicateAsynchronously=true | false – 复制方式是异步的指定为true时,还是同步的,指定为false时。默认是true。
replicateUpdatesViaCopy=true | false – 当一个元素被拷贝到其他的cache中时是否进行复制指定为true时为复制,默认是true。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值