让 Hazelcast 在使用 EntryProcessor 时停止反序列化对象

Get Hazelcast to stop deserializing objects when using an EntryProcessor

我使用的是 Hazelcast IMap,我在其中使用 EntryProcessor 更新了很多值。与此同时,其他线程也在调用get()。如果可能的话,我想避免所有不必要的反序列化。但似乎即使在使用单个节点时它仍然在反序列化。

有什么办法可以避免这种情况吗?我已将 Map 和近缓存的内存格式设置为 OBJECT。我还将 cache-local-entries 设置为 true,但仍然没有成功。我正在使用 Hazelcast 3.12。

这是我的示例代码:

public class HazelcastSerializationTest {

    static int readCount = 0;
    private String mapName = "map1";

    private HazelcastInstance createHazelcast() {
        Config config = new Config("HazelcastSerializationTest");
        config.addMapConfig(new MapConfig(mapName)
                   .setInMemoryFormat(InMemoryFormat.OBJECT)
                   .setNearCacheConfig(new NearCacheConfig(mapName)
                                .setInMemoryFormat(InMemoryFormat.OBJECT)
                                .setCacheLocalEntries(true)));

        config.getNetworkConfig().getJoin()
                  .getMulticastConfig().setEnabled(false);
        return Hazelcast.newHazelcastInstance(config);
    }

    @Test
    public void testEntry() {
        HazelcastInstance instance = createHazelcast();
        IMap<Integer, DataObject> map = instance.getMap(mapName);
        map.put(1, new DataObject(0));
        for (int i = 0; i < 100; i++) {
            map.executeOnKey(1, new NothingProcessor());
            map.get(1);
        }
        assertEquals(2, readCount);
    }
}

class NothingProcessor extends AbstractEntryProcessor<Integer, DataObject> {

    @Override
    public Object process(Map.Entry<Integer, DataObject> entry) {
        entry.setValue(new DataObject(entry.getValue().getValue() + 1));
        return null;
    }
}

class DataObject implements Externalizable {

    private int value;

    public DataObject(int value) {
        this.value = value;
    }

    public DataObject() { }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeInt(value);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException {
        HazelcastSerializationTest.readCount++;
        value = in.readInt();
    }
}

我错过了什么吗?我希望这只是 return 来自近缓存的真实对象。反序列化似乎是不必要的。我还应该提到,在我的场景中,键和值对象是不可变的。

当您通过 EntryProcessor 更新条目时,它还会使 NearCache 中的键值无效。因此,无论何时您对该键执行 get(),都会从服务器获取新值并存储在 NearCache 中。并从服务器上获取 get() = 序列化 + 反序列化,无论内存格式类型如何。