2
我試圖實現memcached的比較並設置的模式,繼圭多的指令:Memcached的比較和設置模式產生錯誤的結果
http://neopythonic.blogspot.nl/2011/08/compare-and-set-in-memcache.html
但是,我似乎並沒有做對了,我不知道什麼是錯的。下面的文件使用Django(1.4.5 Final)和python-memcache(1.48)。
settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
djangocache.py
#!/usr/bin/env python
from django.core.cache import cache
import multiprocessing.dummy
django_key = "TEST"
cached_key = cache.make_key(django_key).encode("UTF-8")
def add_to_cache(item):
client = cache._cache
#client = cache._lib.Client(cache._servers)
while True:
items = client.gets(cached_key)
if client.cas(cached_key, items+(item,)):
break
if __name__ == "__main__":
cache.set(django_key,())
p = multiprocessing.dummy.Pool(2)
p.map(add_to_cache, range(10))
print(len(cache.get(django_key)))
運行它:
[email protected] ~/test $ DJANGO_SETTINGS_MODULE=settings python djangocache.py
5
它occasi在處理比賽條件時,你可以期望輸出6,7等。我試過多個客戶端實例(請參閱評論)。
幫助?