spring-boot - 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
任何帮助表示赞赏。
解决方案
<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>
推荐阅读
- node.js - Axios 响应未正确执行
- amazon-web-services - 调用AWS Textract API时如何指定需要支付的Key值?
- firebase - Google Cloud Build Deploy to Firebase Hosting 出现意外错误
- 3dsmax - 如何在 Maxscript 中更改文件名?
- javascript - 如何检测 React 父组件中的本地存储集
- java - 哈希表,查找函数
- javascript - 在 IF ELSE 语句中使用 props 做出反应
- angular - 为什么 Angular 默认将应用程序路由放入自己的模块中?
- laravel - Laravel:在数据库的下拉列表中显示数据
- yocto - 使用 yocto 发行版的 bitbake 编译颤振桌面应用程序