网格事件和oldValue与newValue

时间:2017-02-12 02:11:27

标签: ignite

更新缓存中的实体时,事件中的oldValue = newValue,除非我对原始对象实例执行put。这当然不总是可行的。

这是一个简化的例子。

IgnitePredicate<CacheEvent> locLsnr = evt -> {
    // do something
};
ignite.events().localListen(locLsnr,EventType.EVT_CACHE_OBJECT_PUT);
IgniteCache<TradeKey, Trade> cache = ignite.getOrCreateCache("MyCache");

Trade trade1 = new Trade();
trade1.setId(1);
trade1.setSize(10);
cache.put(new TradeKey(trade.getId()), trade1); 
// event is generated
//evt.oldValue is null, no problem
trade1.setSize(20);
cache.put(new TradeKey(trade.getId()), trade1); 
// event is generated
// evt.oldValue().getSize() is 10, evt.newValue().getSize() is 20, this is GOOD

但是,如果我在更新之前从缓存中再次检索交易(例如,从应用程序的另一部分),我无法看到事件中的旧值。旧值只会显示新值。

IgnitePredicate<CacheEvent> locLsnr = evt -> {
    // do something
};
ignite.events().localListen(locLsnr,EventType.EVT_CACHE_OBJECT_PUT);
IgniteCache<TradeKey, Trade> cache = ignite.getOrCreateCache("MyCache");

Trade trade1 = new Trade();
trade1.setId(1);
trade1.setSize(10);
cache.put(new TradeKey(trade.getId()), trade1); 
// event is generated
//evt.oldValue is null, no problem
trade1 = cache.get(new TradeKey(1)); // or could be a query or any search on the cache
trade1.setSize(20);
cache.put(new TradeKey(trade.getId()),trade1); 
// event is generated
// evt.oldValue().getSize() is 20, evt.newValue().getSize() is 20, this is BAD

有什么建议吗? 感谢。

1 个答案:

答案 0 :(得分:0)