Apache Ignite的近缓存默认是存在堆内存中的。但是,我们可以将它配置为离堆存储来提高缓存的性能和可靠性。我们可以使用以下示例代码将近缓存设置为离堆内存存储:
CacheConfiguration
// Enabling near cache.
NearCacheConfiguration
// Setting up off-heap memory for the near cache. nearCfg.setNearEvictionPolicyFactory(new LruEvictionPolicyFactory<>()); nearCfg.setNearOffHeapMaxMemory(100L * 1024L * 1024L);
cfg.setNearConfiguration(nearCfg); cfg.setMemoryMode(CacheMemoryMode.OFFHEAP_TIERED);
Ignite ignite = Ignition.start();
ignite.addCacheConfiguration(cfg);
在上面的代码示例中,我们使用CacheConfiguration将一个名为“personCache”的缓存配置为包括近缓存的离堆内存存储。我们使用NearCacheConfiguration来启用近缓存,并使用setNearOffHeapMaxMemory方法指定我们希望将多少内存分配给离堆存储。最后,我们将缓存的内存模式设置为“OFFHEAP_TIERED”,即将近缓存配置为离堆存储。