5
Ehcache
Calvin edited this page 2013-11-14 01:37:15 +08:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

##Ehcache集群 共有RMI,JGroups,JMS三个方案。JMS太过复杂,直接掠过。JGroups方案的配置比较复杂,如果不熟悉JGroups的同学可能用不好,而且是非核心包。所以简简单单就用ehcache-core自带的RMI了。

例子见showcase里的ehcache-hibernate-rmi.xml, 配置很简单,先把Factory配起来,简单的multicast自动发现:

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
	properties="peerDiscovery=automatic,multicastGroupAddress=230.0.0.1, multicastGroupPort=4446" />
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory" />

然后配每一个Cache,默认的配置会让整个集群里的所有Ehcache内容保持同步,但不想网络里漫天飞舞ehcahce的消息,所以选择了一种保守的方式。自己存自己的,不同步新增信息,有更新了也只会通知其他节点删除改内容,而不是将新内容同步出去。

<cache name="account.User" maxElementsInMemory="1000" >
    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
	roperties="replicatePuts=false,replicateUpdatesViaCopy=false" />
</cache>

##Ehcache 2.x的变化 1.按实际Size而不是按Element数量进行限制,showcase中的ehcache.xml做了演示。 见http://ehcache.org/documentation/configuration/cache-size

2.为了配合BigMemory和Size Limit,原来的属性最好改名

  • maxElementsInMemory -> maxEntriesLocalHeap
  • maxElementsOnDisk -> maxEntriesLocalDisk

3.最好在ehcache.xml里声明不去check update.

<ehcache updateCheck="false">

##其他注意事项

  • 如果有多个配置文件产生多个CacheManager,需要在配置文件里加上名字
<ehcache name="hibernateCache">

##其他没用上的功能:

##Default Value 参考 http://ehcache.org/ehcache.xml ,配置中有一些default值可以直接采用的,就没有在配置文件里重复配置, 减少眼睛疲劳。

  • memoryStoreEvictionPolicy,默认为LRU。
  • diskPersistent, 默认为false。
  • diskExpiryThreadIntervalSeconds, 默认为120秒。
  • diskSpoolBufferSizeMB, 默认为30Mb。
  • statistics,默认为false。

##Spring的EhCacheManagerFactoryBean 简单的使用以Spring Resource格式定义的配置文件,返回一个EhcacheManager,并在应用关闭时调用CacheMananger的shutdown,在showcase中有使用。