ehcache 实现缓存共享

本文介绍了如何使用Ehcache通过RMI实现两个应用间的缓存共享和同步更新。通过配置Ehcache的xml文件,设置RMIUrls和监听器,以及在代码中注册MBeans,确保了缓存的同步更新。提供了节点一和节点二的配置文件和Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

公司项目最近在做缓存同步的工作,研发组搞了一套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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值