使用Memcache与GAE,Java和JDO事务

时间:2015-01-07 13:59:29

标签: java google-app-engine memcached google-cloud-datastore jdo

我使用GAE(Java)。我使用JDO&目前的交易,我也想使用memcache。现在我做:

Open manager
Begin transaction
Get entity from db
Change entity
Commit transaction
close manager

有了memcache我有点困惑,我有两种可能性,每个人都有一个问题:
1:

Open manager
Begin transaction
getIdentifiable from memcache -> get from db if not in memcache
Change entity
Commit transaction
Put to memcache if untouched (what if it was touched in meantime and I already commited the transaction)?
Close manager

或2:

Open manager
Begin transaction
getIdentifiable from memcache -> get from db if not in memcache
Change entity
Put to memcache if untouched 
Commit transaction (what if transaction fails now and will be rolled back, and I've already put new value to memcache?)
Close manager

问题在伪代码中。我只是担心我的数据会在数据存储区和内存缓存之间失去同步,因为我不能同时执行提交和putIfUntouched - 如果我成功使用其中一个,我仍然有可能在另一个上失败。应该如何做?

[编辑] 过了一段时间,我猜2)好于1),但是,如果我把值放到memcache并且事务失败了,我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以保留从getIdentifiable获得的值(如果有的话)(改为修改副本),如果确实得到了某些内容,请使用https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/memcache/MemcacheService#putIfUntouched(java.lang.Object, com.google.appengine.api.memcache.MemcacheService.IdentifiableValue, java.lang.Object, com.google.appengine.api.memcache.Expiration)有条件地将修改后的副本写回memcache - 比较 - 并设置操作。

我并不熟悉memcache的Java接口中putIfUntouched的许多重载的细节,但这些概念类似于Python中比较和设置操作的简单实现实现服务接口。

相关问题