想要清除榛树缓存。这样一旦缓存达到 Max Entries 或 Max Heap Size,就将其刷新到文件中。
hazelcast.xml 中的配置
<map name="TEST_CACHE">
<max-size policy="PER_NODE">3</max-size>
<eviction-policy>LFU</eviction-policy>
</map>
测试代码
public class Test implements EntryEvictedListener , Serializable{
private static final long serialVersionUID = -1927328346902661527L;
private static HazelcastInstance hazelcastInstance = HazelCacheUtil
.getHazelCacheInstance();
private static volatile IMap<Object, Object> hazel_cache = hazelcastInstance
.getMap("TEST_CACHE");
public static void main(String[] args) throws Exception {
hazel_cache.addEntryListener(new Test(),true);
for (int i = 0; i < 3; i++) {
hazel_cache.put(i+1, new Test());
}
System.out.println(hazel_cache);
System.out.println("");
for (Entry e : hazel_cache.entrySet()) {
System.out.println(e.getKey() + " : " + e.getValue());
}
}
@Override
public void entryEvicted(EntryEvent e) {
System.out.println(e.getKey()+" : "+e.getOldValue()+"\n");
}
}
在 entryEvicted 我只得到第三个条目对象
Entry EVICTED :: 1
3 : com.test.CAO.Test@5a6b6126
一旦达到最大大小,有没有办法将缓存内容刷新到文件/数据库中?