首页 > 解决方案 > 如何在 redisson 中的多个 JVM 之间同步本地缓存(RLocalCachedMap 实例)?

问题描述

我正在尝试在 Redisson 中实现 RLocalCachedMap,这很容易。我关心的是如何在多个 JVM 实例之间同步本地缓存。它是自动完成的,还是有一些 Redisson 代码将缓存同步到多个 JVM 实例?

我已经包含了文档中提供的缓存选项代码。有一种方法可以跨各种本地缓存实例提供同步策略。但是我的问题仍然是“我们如何告诉一个 JVM 连接到另一个 JVM 实例以共享本地缓存?

LocalCachedMapOptions options = LocalCachedMapOptions.defaults()

      // Used to evict local cache entries.
      // Has follow options:
      // LFU - Counts how often an item was requested. Those that are used least often are discarded first.
      // LRU - Discards the least recently used items first
      // SOFT - Uses weak references, entries are removed by GC 
      // WEAK - Uses soft references, entries are removed by GC
      // NONE - No eviction
     .evictionPolicy(EvictionPolicy.NONE)

      // If cache size is 0 then local cache is unbounded.
     .cacheSize(1000)

      // Used to load missed updates during any connection failures to Redis. 
      // Since, local cache updates can't be get in absence of connection to Redis. 
      // Follow reconnection strategies are available:
      // CLEAR - Clear local cache if map instance has been disconnected for a while.
      // LOAD - Store invalidated entry hash in invalidation log for 10 minutes
      //        Cache keys for stored invalidated entry hashes will be removed 
      //        if LocalCachedMap instance has been disconnected less than 10 minutes
      //        or whole cache will be cleaned otherwise.
      // NONE - Default. No reconnection handling
     .reconnectionStrategy(ReconnectionStrategy.NONE)

      // Used to synchronize local cache changes.
      // Follow sync strategies are available:
      // INVALIDATE - Default. Invalidate cache entry across all LocalCachedMap instances on map entry change
      // UPDATE - Update cache entry across all LocalCachedMap instances on map entry change
      // NONE - No synchronizations on map changes
     .syncStrategy(SyncStrategy.INVALIDATE)

      // time to live for each map entry in local cache
     .timeToLive(10000)
      // or
     .timeToLive(10, TimeUnit.SECONDS)

      // max idle time for each map entry in local cache
     .maxIdle(10000)
      // or
     .maxIdle(10, TimeUnit.SECONDS);

标签: javacachingredisredisson

解决方案


推荐阅读