Shivan Dragon
Shivan Dragon

Reputation: 15219

GAE MemCache behaviour of put() + ADD_ONLY_IF_NOT_PRESENT

The put(...) method of GAE's memcahe API accepts as an argument (in one of it's overloaded implementations) a SetPolicy argument. In the Javadocs here it sais that if you chose "ADD_ONLY_IF_NOT_PRESENT" as policy it's, and I quote:

"useful to avoid race conditions."

My questions are:

  1. what happends with an expired value that was set with the same key? If I add to memcache something like (key=1, value=whatever), then this entry expires, and then I try to add (key=1, value=whatever2) using ADD_ONLY_IF_NOT_PRESENT is whatever2 added to cache or not?

  2. What does it mean "useful for race conditions"? More specifically, does it mean that if I use put(...) with ADD_ONLY_IF_NOT_PRESENT SetPolicy I am no longer required to use getIdentifiable and putIfUntouched in order to avoid race conditions when adding stuff concurrentlly to the memcache?

Upvotes: 1

Views: 313

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

  1. If the value expires, it's not in memcache anymore, so the RPC will set it.

  2. If you do a get, then do a put only if nothing was there, you've introduced a race condition whereby someone else might've put the data while you were checking. Doing a single operation avoids this.

Upvotes: 1

Related Questions