本文整理汇总了Python中memcache.Client.replace方法的典型用法代码示例。如果您正苦于以下问题:Python Client.replace方法的具体用法?Python Client.replace怎么用?Python Client.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类memcache.Client
的用法示例。
在下文中一共展示了Client.replace方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MemcachedCacheStore
# 需要导入模块: from memcache import Client [as 别名]
# 或者: from memcache.Client import replace [as 别名]
class MemcachedCacheStore(AbstractCacheStore):
servers = ('127.0.0.1:11211')
def __init__(self, servers=None, debug=False):
if servers is None:
servers = self.servers
from memcache import Client as MemcachedClient
self._client = MemcachedClient(servers, debug)
def set(self, key, val, time=0):
self._client.set(key, val, time)
def add(self, key, val, time=0):
res = self._client.add(key, val, time)
if not res:
raise Error('a value for key %r is already in the cache'%key)
self._data[key] = (val, time)
def replace(self, key, val, time=0):
res = self._client.replace(key, val, time)
if not res:
raise Error('a value for key %r is already in the cache'%key)
self._data[key] = (val, time)
def delete(self, key):
res = self._client.delete(key, time=0)
if not res:
raise KeyError(key)
def get(self, key):
val = self._client.get(key)
if val is None:
raise KeyError(key)
else:
return val
def clear(self):
self._client.flush_all()
示例2: MemcacheFeatureStorage
# 需要导入模块: from memcache import Client [as 别名]
# 或者: from memcache.Client import replace [as 别名]
class MemcacheFeatureStorage(FeatureStorage):
PREFIX = 'georest_buckets'
support_version = False
def __init__(self, hosts):
""" Feature storage implemented in Memcache
:param list hosts: list of hosts
1. Strings of the form C{"host:port"}
2. Tuples of the form C{("host:port", weight)}
:rtype :class:`MemcacheFeatureStorage`
"""
self._client = Client(servers=hosts)
def create_bucket(self, name, overwrite=False, **kwargs):
bucket_name = self._make_bucket_name(name)
timestamp = time.time()
try:
add_ok = self._client.add(key=bucket_name, val=timestamp)
except Exception as e:
raise StorageInternalError(message='add error', e=e)
if not add_ok:
if overwrite:
try:
rep_ok = self._client.replace(
key=bucket_name, val=timestamp)
except Exception as e:
raise StorageInternalError('replace error', e=e)
if not rep_ok:
raise StorageInternalError(message='failed to replace')
else:
raise DuplicatedBucket(name)
return MemcacheFeatureBucket(name, self._client, str(timestamp))
def get_bucket(self, name):
bucket_name = self._make_bucket_name(name)
try:
timestamp = self._client.get(bucket_name)
except Exception as e:
raise StorageInternalError(message='get error', e=e)
if not timestamp:
raise BucketNotFound(name)
return MemcacheFeatureBucket(name, self._client, str(timestamp))
def delete_bucket(self, name):
bucket_name = self._make_bucket_name(name)
try:
delete_ok = self._client.delete(bucket_name)
except Exception as e:
raise StorageInternalError(message='delete error', e=e)
if not delete_ok:
raise BucketNotFound(name)
return True
def has_bucket(self, name):
bucket_name = self._make_bucket_name(name)
try:
get_ok = self._client.get(bucket_name)
except Exception as e:
raise StorageInternalError(message='get error', e=e)
return get_ok is not None
def close(self):
pass
def _make_bucket_name(self, name):
if isinstance(name, unicode):
name = name.encode('utf-8')
return '.'.join((self.PREFIX, name))