Ehcache复制缓存启动时不同步

2024-01-06

我有一个跨两台机器复制的 ehcache 缓存。一旦两个对等点启动,对等点就会正确地找到彼此并进行复制。但是,如果第一个对等点首先启动并接收多个元素,然后第二个对等点稍后启动...第二个对等点永远不会看到在它尚未存在时添加的元素。

具体顺序如下:

  1. 缓存A已启动
  2. 将“1234”添加到缓存A
  3. 缓存B已启动
  4. 从缓存 B 获取“1234” ->未找到

我的期望:如果复制 2 个缓存,则获取现有元素会为任一缓存返回相同的值。

我的缓存元素只是原始字符串/整数类型。

GitHub 中有一个示例:https://github.com/HamletDRC/EhcachePOC https://github.com/HamletDRC/EhcachePOC

ehcache配置在这里:https://github.com/HamletDRC/EhcachePOC/tree/master/src/main/resources https://github.com/HamletDRC/EhcachePOC/tree/master/src/main/resources

在示例项目中,为 ehcache 类启用了 log4j,以便您可以看到对等点确实相互找到并进行复制,但只有自对等组启动以来添加的元素,而不是以前存在的元素。

您只需要安装 JDK 和 Maven 即可构建它。

重现:

  • 运行ReplicatedCacheWriter
  • 等待 6 秒,让 writer 创建元素 [1, 2, 3, 4, 5, 6]
  • 运行 ReplicatedCacheListener
  • 侦听器查找在激活后“放置”的所有元素,但没有在激活之前“放置”的元素。

这是 ehcache.xml

<cacheManagerPeerProviderFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
        properties="peerDiscovery=automatic, multicastGroupAddress=231.0.0.1,
                  multicastGroupPort=4446, timeToLive=32"/>

<cacheManagerPeerListenerFactory
        class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
        properties="port=40002, socketTimeoutMillis=2000"/>

...

<cacheEventListenerFactory
            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
            properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,
                    replicateUpdatesViaCopy=false, replicateRemovals=true "/>

(显然两个节点的Listener Port是不同的)

如何在启动时同步缓存?


你需要一个bootstrapCacheLoaderFactory 引导缓存加载器工厂 http://ehcache.org/documentation/user-guide/configuration#cache-warming-for-multi-tier-caches在你的ehcache_listener.xml。例如:

<cache name="myCache" ...>
   ...
   <bootstrapCacheLoaderFactory
            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"
            properties="bootstrapAsynchronously=true"
            propertySeparator="," />    
</cache>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ehcache复制缓存启动时不同步 的相关文章

随机推荐