本文整理汇总了Python中memcache.Client.cas方法的典型用法代码示例。如果您正苦于以下问题:Python Client.cas方法的具体用法?Python Client.cas怎么用?Python Client.cas使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类memcache.Client
的用法示例。
在下文中一共展示了Client.cas方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: WrappedClient
# 需要导入模块: from memcache import Client [as 别名]
# 或者: from memcache.Client import cas [as 别名]
class WrappedClient(object):
def __init__(self, *args):
self.args = args
self.mc = Client(*args, cache_cas=True, socket_timeout=10)
self.del_que = []
import threading
def gets(self, key):
while True:
result = self.mc.gets(key)
if isinstance(result, tuple):
return result[0]
return result
def cas(self, key, value):
retry_count = 0
try:
while True:
result = self.mc.cas(key, value)
if not isinstance(result, bool):
if retry_count <= 10:
retry_count += 1
wait_time = 0.001 * randint(0, 1 << retry_count)
print "add fail, retry for sleep"
sleep(wait_time)
self.mc = Client(*self.args, cache_cas=True, socket_timeout=10)
continue
# raise ConnectionError
return result
except TypeError:
return False
def add(self, key, value):
retry_count = 0
while True:
result = self.mc.add(key, value)
if not isinstance(result, bool):
if retry_count <= 10:
retry_count += 1
wait_time = 0.001 * randint(0, 1 << retry_count)
print "add fail, retry for sleep"
sleep(wait_time)
print self.args
self.mc = Client(*self.args, cache_cas=True, socket_timeout=10)
continue
# raise ConnectionError
return result
# delegation
def __getattr__(self, attrname):
return getattr(self.mc, attrname)
示例2: MemcachedCacheClient
# 需要导入模块: from memcache import Client [as 别名]
# 或者: from memcache.Client import cas [as 别名]
class MemcachedCacheClient(CacheClient):
"""Memcached cache client implementation."""
def __init__(self, config):
super(MemcachedCacheClient, self).__init__(config["host"], config["port"], config["cache"])
self.config = config
if self.cache_name and self.cache_name != "" and self.cache_name != DEFAULT_MEMCACHED_CACHE_NAME:
print "WARNING: memcached client doesn't support named caches. cache_name config value will be ignored and the cache name configured on the server will be used instead."
self.memcached_client = Client([self.host + ':' + self.port], debug=0)
return
def put(self, key, value, version=None, lifespan=None, max_idle=None, put_if_absent=False):
time = 0
if lifespan != None:
if lifespan > MEMCACHED_LIFESPAN_MAX_SECONDS:
self._error("Memcached cache client supports lifespan values only up to %s seconds (30 days)." % MEMCACHED_LIFESPAN_MAX_SECONDS)
time = lifespan
if max_idle != None:
self._error("Memcached cache client doesn't support max idle time setting.")
try:
if (version == None):
if (put_if_absent):
if not self.memcached_client.add(key, value, time, 0):
# current python-memcached doesn't recoginze these states
# if self.memcached_client.last_set_status == "NOT_STORED":
# raise ConflictError
# else:
# self._error("Operation unsuccessful. " + self.memcached_client.last_set_status)
self._error("Operation unsuccessful. Possibly CONFLICT.")
else:
if not self.memcached_client.set(key, value, time, 0):
# self._error("Operation unsuccessful. " + self.memcached_client.last_set_status)
self._error("Operation unsuccessful.")
else:
try:
self.memcached_client.cas_ids[key] = int(version)
except ValueError:
self._error("Please provide an integer version.")
if not self.memcached_client.cas(key, value, time, 0):
# if self.memcached_client.last_set_status == "EXISTS":
# raise ConflictError
# if self.memcached_client.last_set_status == "NOT_FOUND":
# raise NotFoundError
# else:
# self._error("Operation unsuccessful. " + self.memcached_client.last_set_status)
self._error("Operation unsuccessful. Possibly CONFLICT, NOT_FOUND.")
except CacheClientError as e:
raise e #rethrow
except Exception as e:
self._error(e)
def get(self, key, get_version=False):
try:
if get_version:
val = self.memcached_client.gets(key)
if val == None:
raise NotFoundError
version = self.memcached_client.cas_ids[key]
if version == None:
self._error("Couldn't obtain version info from memcached server.")
return version, val
else:
val = self.memcached_client.get(key)
if val == None:
raise NotFoundError
return val
except CacheClientError as e:
raise e #rethrow
except Exception as e:
self._error(e.args)
def delete(self, key, version=None):
try:
if version:
self._error("versioned delete operation not available for memcached client")
if self.memcached_client.delete(key, 0):
if self.memcached_client.last_set_status == "NOT_FOUND":
raise NotFoundError
else:
self._error("Operation unsuccessful. " + self.memcached_client.last_set_status)
except CacheClientError as e:
raise e #rethrow
except Exception as e:
self._error(e.args)
def clear(self):
try:
self.memcached_client.flush_all()
except CacheClientError as e:
raise e #rethrow
except Exception as e:
self._error(e.args)