问题
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:
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?
What does it mean "useful for race conditions"? More specifically, does it mean that if I use
put(...)withADD_ONLY_IF_NOT_PRESENTSetPolicy I am no longer required to use getIdentifiable and putIfUntouched in order to avoid race conditions when adding stuff concurrentlly to the memcache?
回答1:
If the value expires, it's not in memcache anymore, so the RPC will set it.
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.
来源:https://stackoverflow.com/questions/8080933/gae-memcache-behaviour-of-put-add-only-if-not-present