【发布时间】:2019-03-28 08:03:51
【问题描述】:
我在 Hazelcast 中有一个分布式地图,如下所示:
ClientConfig clientConfig = new ClientConfig();
clientConfig.getGroupConfig().setName("clusterName").setPassword("clusterPWD");
clientConfig.getNetworkConfig().addAddress("X.X.X.X");
clientConfig.setInstanceName(InstanceName);
HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig);
[...]
map = instance.getMap("MAP_NAME");
[...]
// a lot of map.put();
[...]
// a lot of map.get();
我需要避免OOM问题并且每次都清理缓存。
编辑:似乎默认策略是 NOT EVICTION,因此有必要使用一些策略来清理缓存。 我尝试使用此配置在类路径中添加 hazelcast-client.xml
<near-cache name="wm_info">
<max-size>3</max-size>
<time-to-live-seconds>5</time-to-live-seconds>
<max-idle-seconds>5</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<invalidate-on-change>true</invalidate-on-change>
<in-memory-format>OBJECT</in-memory-format>
</near-cache>
都添加了这段代码
EvictionConfig evictionConfig = new EvictionConfig()
.setEvictionPolicy(EvictionPolicy.LRU)
.setSize(2);
NearCacheConfig nearCacheConfig = new NearCacheConfig()
.setName(WM_MAP_NAME)
.setInMemoryFormat(InMemoryFormat.BINARY)
.setInvalidateOnChange(true)
.setTimeToLiveSeconds(5)
.setEvictionConfig(evictionConfig);
clientConfig.addNearCacheConfig(nearCacheConfig);
但不起作用...即使几分钟后缓存项目仍在缓存中。
EDIT2:它似乎工作的唯一方法是:
map.put(code, json, 5, TimeUnit.SECONDS);
还有其他选择吗?
谢谢 安德烈亚
【问题讨论】: