3

トランザクションを使用してデータストアに複数のエンティティを書き込んでいます。これらのエンティティも MemCache に保持したいと考えています。MemCache 内のエンティティのコピーが Datastore 内のコピーと実際に等しいことを確認するにはどうすればよいですか?

たとえば、次のことができます。

tx.begin()
datastore.put(entity)
if (memcache.putIfUntoched(key, entity))
  tx.commit()

ただし、トランザクションが失敗した場合、エンティティはデータストアではなく MemCache に格納される可能性があります。一方、もしそうなら:

tx.begin()
datastore.put(entity)
tx.commit()
memcache.putIfUntoched(key, entity))

その後、データストア トランザクションは成功する可能性がありますが、MemCache の更新は失敗する可能性があります。どうすれば一貫性を確保できますか?

4

1 に答える 1

2

From my experience, it may not be that helpful if you write to the DB and the cache at the same time. In general, mixing DB transactions with other stuffs (e.g. file system) is difficult to do it right.

I suggest you change your program logic, so that

  1. When you create a new record, write only to DB
  2. When you update an existing record, write to DB, and invalidate corresponding slots in cache
  3. When you're looking for a record, just check the cache. If it's not there, load from DB and fill in the cache
于 2012-11-29T09:27:14.940 に答える