想要清除hazelcast缓存。因此,一旦缓存达到最大条目数或最大堆大小,就将其刷新到文件中。
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对象
Entry EVICTED :: 1
3 : com.test.CAO.Test@5a6b6126有没有办法在达到max-size后将缓存内容刷新到文件/db中?
发布于 2015-11-18 17:51:21
map配置中有一个evictionPercentage。当达到最大大小时,此百分比的条目将被逐出。
还请注意,hazelcast是为大量条目而设计的,每个分区都会执行逐出操作。因此,我建议您使用1000+条目进行测试,以查看预期的结果。
https://stackoverflow.com/questions/33756420
复制相似问题