怎么样?
将本地 ConcurrentHashMap 作为本地缓存。创建 Hazelcast 分布式地图/缓存。开始侦听分布式映射事件并更新本地 ConcurrentHashMap。
现在,每个成员上的本地缓存将相同。自动同步。
import com.hazelcast.core.IMap;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.EntryListener;
import com.hazelcast.core.EntryEvent;
import java.util.concurrent.ConcurrentHashMap;
public class Sample implements EntryListener {
Map localCache = new ConcurrentHashMap ();
public static void main(String[] args) {
Sample sample = new Sample();
IMap map = Hazelcast.getMap("default");
//Listen for all added/updated/removed entries
map.addEntryListener(sample, true);
}
public void entryAdded(EntryEvent event) {
localCache.put(event.getKey(), event.getValue());
}
public void entryRemoved(EntryEvent event) {
localCache.remove(event.getKey());
}
public void entryUpdated(EntryEvent event) {
localCache.put(event.getKey(), event.getValue());
}
}