公司项目最近在做缓存同步的工作,研发组搞了一套zookeeper的插件,缓存存放在ehcache中,因为要依赖第三方插件,感觉很麻烦,ehcache本身就支持缓存同步且方式比较多。
如下样例简单实现两个应用之间的ehcache缓存共享(RMI),同步更新。
同步运行两个Java程序发现跟新缓存会同步更新,具体代码不做解释,工程师们运行一把就知真相。
节点一
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<diskStore path="e:\\Data" />
<defaultCache
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"
/>
<cache name="a"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="true"
diskPersistent="true"
/>
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//localhost:40002/sampleDistributedCache2"
propertySeparator=","
/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=localhost, port=40001, socketTimeoutMillis=2000"
/>
<cache name="sampleDistributedCache2"
maxElementsInMemory="1000000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
overflowToDisk="false">
<cacheEventListe