本文整理汇总了Python中pymemcache.client.hash.HashClient.add方法的典型用法代码示例。如果您正苦于以下问题:Python HashClient.add方法的具体用法?Python HashClient.add怎么用?Python HashClient.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymemcache.client.hash.HashClient
的用法示例。
在下文中一共展示了HashClient.add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MemcachedDriver
# 需要导入模块: from pymemcache.client.hash import HashClient [as 别名]
# 或者: from pymemcache.client.hash.HashClient import add [as 别名]
#.........这里部分代码省略.........
# Run heartbeat here because pymemcache use a lazy connection
# method and only connect once you do an operation.
self.heartbeat()
self._group_members = collections.defaultdict(set)
self._executor.start()
@_translate_failures
def _stop(self):
for lock in list(self._acquired_locks):
lock.release()
self.client.delete(self._encode_member_id(self._member_id))
for g in list(self._joined_groups):
try:
self.leave_group(g).get()
except (coordination.MemberNotJoined,
coordination.GroupNotCreated):
# Guess we got booted out/never existed in the first place...
pass
except coordination.ToozError:
LOG.warning("Unable to leave group '%s'", g, exc_info=True)
self._executor.stop()
# self.client.close()
def _encode_group_id(self, group_id):
return self.GROUP_PREFIX + group_id
def _encode_member_id(self, member_id):
return self.MEMBER_PREFIX + member_id
def _encode_group_leader(self, group_id):
return self.GROUP_LEADER_PREFIX + group_id
@_retry.retry()
def _add_group_to_group_list(self, group_id):
"""Add group to the group list.
:param group_id: The group id
"""
group_list, cas = self.client.gets(self.GROUP_LIST_KEY)
if cas:
group_list = set(group_list)
group_list.add(group_id)
if not self.client.cas(self.GROUP_LIST_KEY,
list(group_list), cas):
# Someone updated the group list before us, try again!
raise _retry.Retry
else:
if not self.client.add(self.GROUP_LIST_KEY,
[group_id], noreply=False):
# Someone updated the group list before us, try again!
raise _retry.Retry
@_retry.retry()
def _remove_from_group_list(self, group_id):
"""Remove group from the group list.
:param group_id: The group id
"""
group_list, cas = self.client.gets(self.GROUP_LIST_KEY)
group_list = set(group_list)
group_list.remove(group_id)
if not self.client.cas(self.GROUP_LIST_KEY,
list(group_list), cas):
# Someone updated the group list before us, try again!
raise _retry.Retry
示例2: MemcacheService
# 需要导入模块: from pymemcache.client.hash import HashClient [as 别名]
# 或者: from pymemcache.client.hash.HashClient import add [as 别名]
class MemcacheService(apiproxy_stub.APIProxyStub):
"""Python only memcache service.
This service keeps all data in any external servers running memcached.
"""
# The memcached default port.
MEMCACHE_PORT = 11211
# An AppScale file which has a list of IPs running memcached.
APPSCALE_MEMCACHE_FILE = "/etc/appscale/memcache_ips"
def __init__(self, project_id, service_name='memcache'):
"""Initializer.
Args:
service_name: Service name expected for all calls.
"""
super(MemcacheService, self).__init__(service_name)
self._memcache = None
self.setupMemcacheClient()
self._methods = {MemcacheSetRequest.SET: self._memcache.set,
MemcacheSetRequest.ADD: self._memcache.add,
MemcacheSetRequest.REPLACE: self._memcache.replace,
MemcacheSetRequest.CAS: self._memcache.cas}
self._project_id = project_id
def setupMemcacheClient(self):
""" Sets up the memcache client. """
if os.path.exists(self.APPSCALE_MEMCACHE_FILE):
memcache_file = open(self.APPSCALE_MEMCACHE_FILE, "r")
all_ips = memcache_file.read().split("\n")
memcache_file.close()
else:
all_ips = ['localhost']
memcaches = [(ip, self.MEMCACHE_PORT) for ip in all_ips if ip]
memcaches.sort()
self._memcache = HashClient(
memcaches, serializer=serializer, deserializer=deserializer,
connect_timeout=5, timeout=1, use_pooling=True)
# The GAE API expects return values for all mutate operations.
for client in six.itervalues(self._memcache.clients):
client.default_noreply = False
def _Dynamic_Get(self, request, response):
"""Implementation of gets for memcache.
Args:
request: A MemcacheGetRequest protocol buffer.
response: A MemcacheGetResponse protocol buffer.
"""
# Remove duplicate keys.
original_keys = {
encode_key(self._project_id, request.name_space(), key): key
for key in request.key_list()}
try:
backend_response = self._memcache.get_many(
original_keys.keys(), gets=request.for_cas())
except MemcacheClientError as error:
raise apiproxy_errors.ApplicationError(INVALID_VALUE, str(error))
except TRANSIENT_ERRORS as error:
raise apiproxy_errors.ApplicationError(
UNSPECIFIED_ERROR, 'Transient memcache error: {}'.format(error))
for encoded_key, value_tuple in six.iteritems(backend_response):
item = response.add_item()
item.set_key(original_keys[encoded_key])
if request.for_cas():
item.set_cas_id(int(value_tuple[1]))
value_tuple = value_tuple[0]
item.set_value(value_tuple[0])
item.set_flags(value_tuple[1])
def _Dynamic_Set(self, request, response):
"""Implementation of sets for memcache.
Args:
request: A MemcacheSetRequest.
response: A MemcacheSetResponse.
"""
namespace = request.name_space()
if any(item.set_policy() not in self._methods
for item in request.item_list()):
raise apiproxy_errors.ApplicationError(
INVALID_VALUE, 'Unsupported set_policy')
if not all(item.has_cas_id() for item in request.item_list()
if item.set_policy() == MemcacheSetRequest.CAS):
raise apiproxy_errors.ApplicationError(
INVALID_VALUE, 'All CAS items must have a cas_id')
for item in request.item_list():
try:
encoded_key = encode_key(self._project_id, namespace, item.key())
except apiproxy_errors.ApplicationError:
response.add_set_status(MemcacheSetResponse.ERROR)
continue
#.........这里部分代码省略.........
示例3: CouchbaseMemcacheMirror
# 需要导入模块: from pymemcache.client.hash import HashClient [as 别名]
# 或者: from pymemcache.client.hash.HashClient import add [as 别名]
#.........这里部分代码省略.........
st.cb_error = e
st.mc_status = mc_meth(key, value)
def incr(self, key, value):
return self._do_incrdecr(key, value, True)
def decr(self, key, value):
return self._do_incrdecr(key, value, False)
def touch(self, key, expire=0):
st = Status()
try:
self.cb.touch(key, ttl=expire)
except NotFoundError as e:
st.cb_error = st
st.mc_status = self.mc.touch(key)
def set(self, key, value, expire=0):
"""
Write first to Couchbase, and then to Memcached
:param key: Key to use
:param value: Value to use
:param expire: If set, the item will expire in the given amount of time
:return: Status object if successful (will always be success).
on failure an exception is raised
"""
self.cb.upsert(key, value, ttl=expire)
self.mc.set(key, value, expire=expire)
return Status()
def set_multi(self, values, expire=0):
"""
Set multiple items.
:param values: A dictionary of key, value indicating values to store
:param expire: If present, expiration time for all the items
:return:
"""
self.cb.upsert_multi(values, ttl=expire)
self.mc.set_many(values, expire=expire)
return Status()
def replace(self, key, value, expire=0):
"""
Replace existing items
:param key: key to replace
:param value: new value
:param expire: expiration for item
:return: Status object. Will be OK
"""
status = Status()
try:
self.cb.replace(key, value, ttl=expire)
except NotFoundError as e:
status.cb_error = e
status.mc_status = self.mc.replace(key, value, expire=expire)
return status
def add(self, key, value, expire=0):
status = Status()
try:
self.cb.insert(key, value, ttl=expire)
except KeyExistsError as e:
status.cb_error = e
status.mc_status = self.mc.add(key, value, expire=expire)
return status
def _append_prepend(self, key, value, is_append):
cb_meth = self.cb.append if is_append else self.cb.prepend
mc_meth = self.mc.append if is_append else self.mc.prepend
st = Status()
try:
cb_meth(key, value, format=FMT_UTF8)
except (NotStoredError, NotFoundError) as e:
st.cb_error = e
st.mc_status = mc_meth(key, value)
def append(self, key, value):
return self._append_prepend(key, value, True)
def prepend(self, key, value):
return self._append_prepend(key, value, False)
def cas(self, key, value, cas, expire=0):
if self._primary == PRIMARY_COUCHBASE:
try:
self.cb.replace(key, value, cas=cas, ttl=expire)
self.mc.set(key, value, ttl=expire)
return True
except KeyExistsError:
return False
except NotFoundError:
return None
else:
return self.mc.cas(key, value, cas)
开发者ID:couchbaselabs,项目名称:sk-python-couchbase-memcache-mirror,代码行数:104,代码来源:couchbase_memcache_mirror.py