1

I'd like to atomically add a new key to google appengine's memcache service using the Java API. This means that the put operation should fail (e.g. throw an exception or return false ) if the key is already present in the memcache, preventing concurrent additions (which would result in overwriting) to the memcache.

I don't see how this can be accomplished with the getIdentifiable / putIfUntouched pair of methods. Also, synchronize does not help because I want control concurrency across different instances of my servlet.

4

1 回答 1

3

您想使用 SetPolicy.ADD_ONLY_IF_NOT_PRESENT ,如果密钥不存在,则只会写入一个值。

boolean put(java.lang.Object key,
      java.lang.Object value,
      Expiration expires,
      MemcacheService.SetPolicy policy)

返回值会告诉你一个值是否已经存在。

于 2013-01-02T01:46:09.310 回答