首页 > 解决方案 > Hazelcast 确保成员上的近缓存预加载

问题描述

我有一个用例,在使用它之前,我需要确保将数据加载到缓存附近。我使用 Hazelcast 作为缓存管理器提供程序。我确实以内存格式将数据存储在近缓存中OBJECT,并缓存本地条目。在应用程序启动时,我通过调用服务缓存方法来进行热缓存:

      @Override
      public void onApplicationEvent(final ApplicationReadyEvent applicationReadyEvent) {

       applicationReadyEvent.getApplicationContext().getBean(MyService.class).myCachedMethod(); //1 warm cahce

       applicationReadyEvent.getApplicationContext().getBean(MyService.class).myCachedMethod(); //2 warm near cache here

       MyData myData = applicationReadyEvent.getApplicationContext().getBean(MyService.class).myCachedMethod(); // 3 warm near cache here 
      //4
      //work with data from near cache here
        myDtata.doStufff();
      }

之后,我确实多次调用相同的方法,将数据加载到附近的缓存中。问题是,在我尝试使用方法数据时,有时未加载接近缓存。看起来 Near Cache 是异步加载的,并且我在第 4 步继续接收不是来自附近缓存的数据。所以我的问题是 - 有没有办法使 NearCache 预加载成为可能,或者确保在特定点 NearCache 被填充?即使这意味着在使用之前等待一段时间让它填充。添加Thread.sleep()对我有用,但这绝不是要走的路。Hazelcast 版本是3.11.4

任何帮助表示赞赏。

标签: spring-bootcachinghazelcastnear-cache

解决方案


<preloader>您可以使用配置元素预加载 Near Cache 。请参阅以下示例配置,类似于参考手册中的配置:

<near-cache name="myDataStructure">
  <in-memory-format>OBJECT</in-memory-format>
  <preloader enabled="true"
             directory="nearcache-example"
             store-initial-delay-seconds="600"
             store-interval-seconds="600"/>
</near-cache>

推荐阅读