put()+ ADD_ONLY_IF_NOT_PRESENT的GAE MemCache行为 [英] GAE MemCache behaviour of put() + ADD_ONLY_IF_NOT_PRESENT

查看:116
本文介绍了put()+ ADD_ONLY_IF_NOT_PRESENT的GAE MemCache行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GAE的memcahe API的 put(...)方法接受作为参数(在它的一个重载实现中)a SetPolicy 参数。在Javadocs 如果你选择了ADD_ONLY_IF_NOT_PRESENT作为政策,并且引用:



用于避免竞争条件。



我的问题是:


  1. 发生了什么情况,同样的钥匙?如果我添加到memcache之类的东西(key = 1,value = whatever),那么这个条目到期,然后我尝试添加(key = 1,value = whatever2)使用ADD_ONLY_IF_NOT_PRESENT是无论添加到缓存还是不是?


  2. 这是什么意思对比赛条件有用?更具体地说,如果我使用 put(...) ADD_ONLY_IF_NOT_PRESENT SetPolicy,我不再需要要使用 getIdentifiable putIfUntouched 为了避免在向memcache同时添加东西时出现竞争条件? 到期时,它不再存在于memcache中,所以RPC会设置它。

  3. 如果你做了一个get,那么只有在没有任何东西存在的情况下,已经引入了一种竞争条件,其他人可能会在您检查时放置数据。做一个单一的操作可以避免这种情况。


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?

解决方案

  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.

这篇关于put()+ ADD_ONLY_IF_NOT_PRESENT的GAE MemCache行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆