跨两个点燃缓存的原子操作

Atomic operations across two ignite caches

我有两个 ignite 缓存。它们都使用 key/value 对(并且没有涉及数据库)来存储一些数据。

cache1 存储实际数据,而 cache2 存储一些元数据。要求是确保两个缓存始终保持同步,即使出现任何问题。

有什么方法可以确保这两个更新都会发生?如果其中一个操作失败,那么另一个操作也应该回滚。

这里是示例代码

cache1.put(someKey, someValue);
// some other code
cache2.put(anotherKey, anotherValue);

如何确保两个缓存都已更新或none?

是的,您需要 use transactions。例如:

Ignite ignite = Ignition.ignite();

IgniteTransactions transactions = ignite.transactions();

try (Transaction tx = transactions.txStart()) {
  Integer hello = cache.get("Hello");

  if (hello == 1)
    cache.put("Hello", 11);

  cache.put("World", 22);

  tx.commit(); 
}

你的两个缓存都需要将 atomicityMode 参数设置为 TRANSACTIONAL 才能工作(默认为 ATOMIC)。