Django Memcache代码审查:比较和设置 [英] Django Memcache Code review: Compare and set
问题描述
我试图使用 Memcache的比较和设置 。
I am trying to implement a counter increment in memcached using Memcache’s compare and set.
有人可以查看这个代码,以便潜在的漏洞 ?
Can someone review this code for potential loopholes?
def increment(id):
client = get_cache('memcache')
i = 0
items = 0
while i <= 3:
counter = client._cache.gets(id)
if counter is not None:
items = client._cache.cas(id, counter+1)
if items:
break
else:
items = client._cache.add(id, 0)
if items:
break
i+= 1
return items
此外,自从我使用通过Django无法使用的内部memcached API,这个在设置中附加了我设置的密钥前缀。如果不是我可以附加这个内部API调用的密钥?
Additionally since I am using an internal memcached API not available through Django, does this append the key prefix I set through in settings. If not how I can append the key for this internal API call?
'memcache': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
'KEY_PREFIX': 'store_',
}
推荐答案
我不是直接回答你的问题,但是我不禁问,为什么你不是只使用 incr()
方法用于原子递增值 - 这是它的用途。也许你只是举了一个例子,但是如果是这样,那就是一个误导性的问题。
I'm not really answering your question directly, but I can't help asking why you aren't just using the incr()
method for atomically incrementing a value - that's what it's for. Maybe you just posted an example, but if so, then it's kind of a misleading question.
这篇关于Django Memcache代码审查:比较和设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!