本文整理汇总了Python中pymemcache.client.base.Client类的典型用法代码示例。如果您正苦于以下问题:Python Client类的具体用法?Python Client怎么用?Python Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MemcachedWrapper
class MemcachedWrapper(object):
'''Class to allow readonly access to underlying memcached connection'''
def __init__(self, counter, host, port=11211, socket_connect_timeout=1):
if not host:
raise ConfigurationError('Memcached wrapper improperly configured. Valid memcached host is required!')
self.__con = Client((host, port))
self._counter = counter('')
def __del__(self):
self.__con.quit()
def get(self, key):
return self.__con.get(key)
def json(self, key):
return pyjson.loads(self.get(key))
def stats(self, extra_keys=[]):
data = self.__con.stats()
ret = {}
for key in data:
if key in COUNTER_KEYS:
ret['{}_per_sec'.format(key.replace('total_', ''))] = \
round(self._counter.key(key).per_second(data.get(key, 0)), 2)
elif key in VALUE_KEYS:
ret[key] = data[key]
elif key in extra_keys:
ret[key] = data[key]
return ret
示例2: test_socket_connect_unix
def test_socket_connect_unix(self):
server = '/tmp/pymemcache.{pid}'.format(pid=os.getpid())
with MockUnixSocketServer(server):
client = Client(server)
client._connect()
assert client.sock.family == socket.AF_UNIX
示例3: make_client_pool
def make_client_pool(self, hostname, mock_socket_values,
serializer=None, **kwargs):
mock_client = Client(hostname, serializer=serializer, **kwargs)
mock_client.sock = MockSocket(mock_socket_values)
client = PooledClient(hostname, serializer=serializer)
client.client_pool = pool.ObjectPool(lambda: mock_client)
return mock_client
示例4: Reader
class Reader(BaseReader):
"""
Memcached settings Reader
A simple memcached getter using pymemcache library.
"""
_default_conf = {
'host': 'localhost',
'port': 11211
}
def __init__(self, conf):
super(Reader, self).__init__(conf)
self.client = Client((self.conf['host'], self.conf['port']))
def _get(self, key):
result = self.client.get(key)
if isinstance(result, six.binary_type):
result = result.decode('utf-8')
return result
def _set(self, key, value):
self.client.set(key, value, noreply=False)
示例5: test_socket_close
def test_socket_close(self):
server = ("example.com", 11211)
client = Client(server, socket_module=MockSocketModule())
client._connect()
assert client.sock is not None
client.close()
assert client.sock is None
示例6: plugin
def plugin(srv, item):
srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)
if HAVE_SLACK == False:
srv.logging.error("slacker module missing")
return False
token = item.config.get("token")
if token is None:
srv.logging.error("No token found for slack")
return False
try:
channel_id, size_x, size_y, timespan, dir_to_save = item.addrs
except:
srv.logging.error("Incorrect target configuration")
return False
# make animated gif, save to local disk, upload to slack channel
client = Client(("127.0.0.1", 11211))
images_original = []
images_resized = []
size = size_x, size_y
cur_month = datetime.datetime.now().strftime("%Y%m")
cur_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
cur_dir = dir_to_save + "/" + cur_month
if not os.path.exists(cur_dir):
os.makedirs(cur_dir)
cur_img_original = cur_dir + "/" + cur_time + ".gif"
cur_img_resized = cur_dir + "/" + cur_time + "_s.gif"
# make a gif file
try:
result = client.get("curno")
if result:
curno = int(result)
for i in range((curno - timespan), curno):
data = client.get(str(i))
if data:
im = Image.open(StringIO.StringIO(data))
images_original.append(im)
imresized = ImageOps.fit(im, size, Image.ANTIALIAS)
images_resized.append(imresized)
if len(images_original) > 0:
images2gif.writeGif(cur_img_original, images_original, duration=0.2)
images2gif.writeGif(cur_img_resized, images_resized, duration=0.2)
except Exception, e:
srv.logging.warning("Cannot make a gif: %s" % (str(e)))
return False
示例7: make_client
def make_client(self, mock_socket_values, **kwargs):
client = Client(None, **kwargs)
# mock out client._connect() rather than hard-settting client.sock to
# ensure methods are checking whether self.sock is None before
# attempting to use it
sock = MockSocket(list(mock_socket_values))
client._connect = mock.Mock(side_effect=functools.partial(
setattr, client, "sock", sock))
return client
示例8: test_socket_connect_closes_on_failure
def test_socket_connect_closes_on_failure(self):
server = ("example.com", 11211)
socket_module = MockSocketModule(connect_failure=OSError())
client = Client(server, socket_module=socket_module)
with pytest.raises(OSError):
client._connect()
assert len(socket_module.sockets) == 1
assert socket_module.sockets[0].connections == []
assert socket_module.sockets[0].closed
示例9: test_socket_close_exception
def test_socket_close_exception(self):
server = ("example.com", 11211)
socket_module = MockSocketModule(close_failure=OSError())
client = Client(server, socket_module=socket_module)
client._connect()
assert client.sock is not None
client.close()
assert client.sock is None
示例10: test_incr_decr
def test_incr_decr(client_class, host, port, socket_module):
client = Client((host, port), socket_module=socket_module)
client.flush_all()
result = client.incr(b'key', 1, noreply=False)
assert result is None
result = client.set(b'key', b'0', noreply=False)
assert result is True
result = client.incr(b'key', 1, noreply=False)
assert result == 1
def _bad_int():
client.incr(b'key', b'foobar')
with pytest.raises(MemcacheClientError):
_bad_int()
result = client.decr(b'key1', 1, noreply=False)
assert result is None
result = client.decr(b'key', 1, noreply=False)
assert result == 0
result = client.get(b'key')
assert result == b'0'
示例11: MemcachedCache
class MemcachedCache(CachualCache):
"""A cache using `Memcached <https://memcached.org/>`_ as the backing
cache. The same caveats apply to keys and values as for Redis - you should
only try to store strings (using the packing/unpacking functions). See the
documentation on Keys and Values here:
:class:`pymemcache.client.base.Client`.
:type host: string
:param host: The Memcached host to use for the cache.
:type port: integer
:param port: The port to use for the Memcached server.
:type kwargs: dict
:param kwargs: Any additional args to pass to the :class:`CachualCache`
constructor.
"""
def __init__(self, host='localhost', port=11211, **kwargs):
super(MemcachedCache, self).__init__(**kwargs)
self.client = MemcachedClient((host, port))
def get(self, key):
"""Get a value from the cache using the given key.
:type key: string
:param key: The cache key to get the value for.
:returns: The value for the cache key, or None in the case of cache
miss.
"""
return self.client.get(key)
def put(self, key, value, ttl=None):
"""Put a value into the cache at the given key. For constraints on keys
and values, see :class:`pymemcache.client.base.Client`.
:type key: string
:param key: The cache key to use for the value.
:param value: The value to store in the cache.
:type ttl: integer
:param ttl: The time-to-live for key in seconds, after which it will
expire.
"""
if ttl is None:
ttl = 0
self.client.set(key, value, expire=ttl)
示例12: Memcached
class Memcached(object):
DELAY = 0.5
DEBUG = False
def __init__(self, hostname, port, **params):
self.mc = Client((hostname, port))
def handle(self, topic, message):
"""
"""
if 'cmd' not in message:
raise Exception("Bad message: no command")
cmd = message['cmd']
if not hasattr(self, cmd):
raise Exception("Unknown command: " + cmd)
tryit = True
while tryit:
tryit = False
try:
getattr(self, cmd)(message)
except MemcacheUnexpectedCloseError:
# Server dropped dead - we'll retry
tryit = True
except IOError:
# Something network-related - retry
tryit = True
if tryit:
time.sleep(self.DELAY)
def set(self, message):
text = message['val'].encode('utf-8')
if message.get('sbt', None):
purge_time = time.time() + message.get('uto', 0)
text = text.replace('$UNIXTIME$', '%.6f' % purge_time)
if self.DEBUG:
print("Set {0}-{1}-{2}".format(message['key'].encode('utf-8'), text, int(message['ttl'])))
self.mc.set(message['key'].encode('utf-8'), text, int(message['ttl']))
def delete(self, message):
self.mc.delete(message['key'])
示例13: __init__
def __init__(self, timeline_rate_reserve=5, multi_proc_logger=None):
"""
The reserve arguments are how many requests to hold back on to leave
some form of buffer in place with regards to API limits
"""
if multi_proc_logger:
self.logger = multi_proc_logger
else:
self.logger = utils.MultiProcessCheckingLogger(module_logger)
self.memcacheClient = MemCacheClient(
(interns_settings.memcache_host, interns_settings.memcache_port)
)
self.timeline_rate_reserve = timeline_rate_reserve
self.tl_total_reqs = interns_settings.twitter_timeline_requests
self.tl_reqs_left = interns_settings.twitter_timeline_req_left
self.tl_reqs_reset_time = interns_settings.twitter_timeline_reset_time
self.update_limits()
示例14: test_serialization_deserialization
def test_serialization_deserialization(host, port, socket_module):
def _ser(key, value):
return json.dumps(value).encode('ascii'), 1
def _des(key, value, flags):
if flags == 1:
return json.loads(value.decode('ascii'))
return value
client = Client((host, port), serializer=_ser, deserializer=_des,
socket_module=socket_module)
client.flush_all()
value = {'a': 'b', 'c': ['d']}
client.set(b'key', value)
result = client.get(b'key')
assert result == value
示例15: __init__
def __init__(self, hostname, port, **params):
self.mc = Client((hostname, port))