本文整理汇总了Python中libmc.Client.get方法的典型用法代码示例。如果您正苦于以下问题:Python Client.get方法的具体用法?Python Client.get怎么用?Python Client.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类libmc.Client
的用法示例。
在下文中一共展示了Client.get方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Memcached
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
class Memcached(object):
def __init__(self, servers):
"""Initialize memcached.
@param servers: an array of servers. Servers can be passed in two forms:
1. Strings of the form host:port (implies a default weight of 1).
2. Tuples of the form (host:port, weight) (weight as integer)
"""
if LIBMC:
self._client = Client(servers, comp_threshold=10240, noreply=True)
else:
self._client = Client(servers)
self.timeout = 0
def reset(self):
self._client.flush_all()
def size(self):
bytes = 0
stats = self._client.get_stats()
for name, stat in stats:
bytes += int(stat["bytes"])
return bytes
def keys(self):
raise MemcachedException(
"It's not possible to fetch keys from memcached"
)
def values(self):
raise MemcachedException(
"It's not possible to fetch values from memcached"
)
def get(self, key, default=None):
value = self._client.get(key)
if value is not None:
return value
return default
def __getitem__(self, key):
return self._client.get(key)
def __setitem__(self, key, object):
if PYLIBMC and not LIBMC:
self._client.set(
key,
object,
time=self.timeout,
min_compress_len=1024000,
compress_level=zlib.Z_BEST_SPEED,
)
else:
self._client.set(key, object, time=self.timeout)
def __delitem__(self, key):
self._client.delete(key)
示例2: ErrorCodeTestCase
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
class ErrorCodeTestCase(unittest.TestCase):
def setUp(self):
self.mc = Client(["127.0.0.1:21211"])
assert self.mc.version()
assert self.mc.get_last_error() == MC_RETURN_OK
def test_invalid_key(self):
self.mc.get('invalid key')
assert self.mc.get_last_error() == MC_RETURN_INVALID_KEY_ERR
def test_mc_server_err(self):
mc = Client(["not_exist_host:11211"])
mc.get('valid_key')
assert mc.get_last_error() == MC_RETURN_MC_SERVER_ERR
示例3: MCStore
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
class MCStore(object):
def __init__(self, server):
self.server = server
self.mc = Client([server], do_split=0)
def __del__(self):
self.close()
def __repr__(self):
return '<MCStore(server=%s)>' % repr(self.server)
def __str__(self):
return self.server
def set(self, key, data, rev=0):
return bool(self.mc.set(key, data, rev))
def set_raw(self, key, data, rev=0, flag=0): # interface for sync data
if rev < 0:
raise str(rev)
return self.mc.set_raw(key, data, rev, flag)
def get(self, key):
try:
r = self.mc.get(key)
if r is None and self.mc.get_last_error() != 0:
raise IOError(self.mc.get_last_error())
return r
except ValueError:
self.mc.delete(key)
def get_raw(self, key):
r, flag = self.mc.get_raw(key)
if r is None and self.mc.get_last_error() != 0:
raise IOError(
self.mc.get_last_error(), self.mc.get_last_strerror())
return r, flag
def get_multi(self, keys):
return self.mc.get_multi(keys)
def delete(self, key):
return bool(self.mc.delete(key))
def close(self):
if self.mc is not None:
self.mc = None
示例4: SingleServerCase
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
class SingleServerCase(unittest.TestCase):
def setUp(self):
self.mc = Client(["127.0.0.1:21211"])
self.compressed_mc = Client(["127.0.0.1:21211"], comp_threshold=1024)
self.noreply_mc = Client(["127.0.0.1:21211"], noreply=True)
def test_attribute(self):
"Test attributes are accessible from Python code"
mc = self.mc
assert hasattr(mc, 'comp_threshold')
assert hasattr(mc, 'servers')
def test_misc(self):
mc = self.mc
mc.get_multi(['foo', 'tuiche'])
mc.delete('foo')
mc.delete('tuiche')
assert mc.get('foo') is None
assert mc.get('tuiche') is None
mc.set('foo', 'biu')
mc.set('tuiche', 'bb')
assert mc.get('foo') == 'biu'
assert mc.get('tuiche') == 'bb'
assert (mc.get_multi(['foo', 'tuiche']) ==
{'foo': 'biu', 'tuiche': 'bb'})
mc.set_multi({'foo': 1024, 'tuiche': '8964'})
assert (mc.get_multi(['foo', 'tuiche']) ==
{'foo': 1024, 'tuiche': '8964'})
def test_delete(self):
mc = self.mc
assert mc.set('smmf', 0xCA909) is True
assert mc.get('smmf') == 0xCA909
assert mc.delete('smmf') is True
assert mc.delete_multi(['smmf']) is True
assert mc.set('smmf', 0xCA909) is True
assert mc.delete_multi(['smmf']) is True
assert mc.delete('smmf') is True
assert mc.delete('smmf') is True
def test_incr_decr(self):
mc = self.mc
mc.set('wazi', 99)
assert mc.incr('wazi', 1) == 100
assert mc.decr('wazi', 1) == 99
mc.delete('wazi')
assert mc.incr('wazi', 1) is None
assert mc.decr('wazi', 1) is None
def test_cas(self):
mc = self.mc
mc.delete('bilinda')
k1 = 'bilinda'
v1 = 'butchers'
assert mc.gets(k1) is None
mc.set(k1, v1)
v1_, ck = mc.gets('bilinda')
v2 = 'ding'
assert v1 == v1_
assert mc.cas(k1, v2, 0, ck) is True
assert mc.cas(k1, v1, 0, ck) is not True
v2_, ck = mc.gets('bilinda')
assert v2 == v2_
def test_large(self):
mc = self.mc
BUF_500KB = 'i' * (500 * 1000)
BUF_1MB = 'i' * (1000 * 1000)
key_500kb = 'test_500kb'
key_1mb = 'test_1mb'
mc.delete(key_500kb)
mc.delete(key_1mb)
mc.set(key_500kb, BUF_500KB)
assert mc.get(key_500kb) == BUF_500KB
mc.set(key_1mb, BUF_1MB)
assert mc.get(key_1mb) == BUF_1MB
mc.delete(key_500kb)
mc.delete(key_1mb)
dct = {key_500kb: BUF_500KB, key_1mb: BUF_1MB}
mc.set_multi(dct)
assert mc.get_multi(dct.keys()) == dct
def test_extra_large(self):
threshold = 1000000
mc = self.mc
key_xl = 'test_very_large'
val = 'i' * threshold
assert mc.set(key_xl, val)
assert mc.get(key_xl) == val
val += 'b'
assert mc.set(key_xl, val)
assert mc.get(key_xl) == val
val = 'i' * (threshold + threshold)
assert mc.set(key_xl, val)
assert mc.get(key_xl) == val
val += 'b'
assert mc.set(key_xl, val)
#.........这里部分代码省略.........
示例5: test_mc_server_err
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
def test_mc_server_err(self):
mc = Client(["not_exist_host:11211"])
mc.get('valid_key')
assert mc.get_last_error() == MC_RETURN_MC_SERVER_ERR
示例6: CmemcachedRegressionCase
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
class CmemcachedRegressionCase(unittest.TestCase):
def setUp(self):
host = "127.0.0.1"
port = 21211
self.server_addr = '%s:%d' % (host, port)
self.mc = Client([self.server_addr], comp_threshold=1024)
if cmemcached is not None:
self.old_mc = cmemcached.Client([self.server_addr],
comp_threshold=1024)
def test_set_get(self):
self.mc.set("key", "value")
self.assertEqual(self.mc.get("key"), "value")
self.mc.set("key_int", 1)
self.assertEqual(self.mc.get("key_int"), True)
self.mc.set("key_long", int(1234567890))
self.assertEqual(self.mc.get("key_long"), int(1234567890))
self.mc.set("key_object", BigObject())
self.assertEqual(self.mc.get("key_object"), BigObject())
big_object = BigObject('x', 1000001)
self.mc.set("key_big_object", big_object)
self.assertEqual(self.mc.get("key_big_object"), big_object)
def test_set_get_none(self):
self.assertEqual(self.mc.set('key', None), True)
self.assertEqual(self.mc.get('key'), None)
def test_chinese_set_get(self):
key = '豆瓣'
value = '在炎热的夏天我们无法停止上豆瓣'
self.assertEqual(self.mc.set(key, value), True)
self.assertEqual(self.mc.get(key), value)
def test_unicode_set_get(self):
key = "test_unicode_set_get"
value = u"中文"
self.assertEqual(self.mc.set(key, value), True)
self.assertEqual(self.mc.get(key), value)
def test_special_key(self):
key = 'keke a kid'
value = 1024
self.assertEqual(self.mc.set(key, value), False)
self.assertEqual(self.mc.get(key), None)
key = 'u:keke a kid'
self.assertEqual(self.mc.set(key, value), False)
self.assertEqual(self.mc.get(key), None)
def test_unicode_key(self):
key1 = u"answer"
key2 = u"答案"
bytes_key1 = "answer"
bytes_key2 = "答案"
value = 42
self.assertEqual(self.mc.set(key1, value), True)
self.assertEqual(self.mc.get(key1), value)
self.assertEqual(self.mc.set(key2, value), True)
self.assertEqual(self.mc.get(key2), value)
self.assertEqual(self.mc.incr(key2), value + 1)
self.assertEqual(self.mc.get(key2), value + 1)
self.assertEqual(self.mc.delete(key1), True)
self.assertEqual(self.mc.get(key1), None)
self.assertEqual(self.mc.add(key1, value), True)
self.assertEqual(self.mc.get(key1), value)
self.assertEqual(self.mc.add(key1, value), False)
self.assertEqual(self.mc.set(key1, value), True)
self.assertEqual(self.mc.get(bytes_key1), self.mc.get(key1))
self.assertEqual(self.mc.get(bytes_key2), self.mc.get(key2))
def test_add(self):
key = 'test_add'
self.mc.delete(key)
self.assertEqual(self.mc.add(key, 'tt'), True)
self.assertEqual(self.mc.get(key), 'tt')
self.assertEqual(self.mc.add(key, 'tt'), 0)
self.mc.delete(key + '2')
self.assertEqual(self.mc.add(key + '2', range(10)), True)
def test_replace(self):
key = 'test_replace'
self.mc.delete(key)
self.assertEqual(self.mc.replace(key, ''), 0)
self.assertEqual(self.mc.set(key, 'b'), True)
self.assertEqual(self.mc.replace(key, 'a'), True)
self.assertEqual(self.mc.get(key), 'a')
def test_append(self):
key = "test_append"
#.........这里部分代码省略.........
示例7: CmemcachedRegressionPrefixCase
# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import get [as 别名]
class CmemcachedRegressionPrefixCase(unittest.TestCase):
def setUp(self):
host = "127.0.0.1"
port = 21211
self.prefix = '/prefix'
self.server_addr = '%s:%d' % (host, port)
self.prefixed_mc = Client([self.server_addr], comp_threshold=1024,
prefix=self.prefix)
self.mc = Client([self.server_addr], comp_threshold=1024)
def test_duplicate_prefix_text(self):
for case in ['%sforever/young', 'forever%s/young', 'forever/young/%s']:
nasty_key = case % self.prefix
self.prefixed_mc.set(nasty_key, 1)
self.assertEqual(self.prefixed_mc.get(nasty_key), 1)
self.assertEqual(self.prefixed_mc.get_multi([nasty_key]),
{nasty_key: 1})
def test_misc(self):
raw_mc = self.mc
prefixed_mc = self.prefixed_mc
raw_mc.delete('a')
prefixed_mc.set('a', 1)
assert(prefixed_mc.get('a') == 1)
assert(raw_mc.get(self.prefix + 'a') == 1)
assert(raw_mc.get('a') is None)
prefixed_mc.add('b', 2)
assert(prefixed_mc.get('b') == 2)
assert(raw_mc.get(self.prefix + 'b') == 2)
assert(raw_mc.get('b') is None)
prefixed_mc.incr('b')
assert(prefixed_mc.get('b') == 3)
assert(raw_mc.get(self.prefix + 'b') == 3)
raw_mc.decr(self.prefix + 'b')
assert(prefixed_mc.get('b') == 2)
prefixed_mc.set_multi({'x': 'a', 'y': 'b'})
ret = prefixed_mc.get_multi(['x', 'y'])
assert(ret.get('x') == 'a' and ret.get('y') == 'b')
assert(prefixed_mc.delete_multi(['a', 'b', 'x', 'y']))
prefixed_mc.set('?foo', 'bar')
assert prefixed_mc.get('?foo') == 'bar'
assert prefixed_mc.get_multi(['?foo']) == {'?foo': 'bar'}
assert raw_mc.get('?%sfoo' % self.prefix) == 'bar'
prefixed_mc.set_multi({'?bar': 'foo'})
assert prefixed_mc.get('?bar') == 'foo'
assert raw_mc.get('?%sbar' % self.prefix) == 'foo'
assert raw_mc.get_list(['?%sbar' % self.prefix]) == ['foo']
def test_ketama(self):
mc = Client(
['localhost', 'myhost:11211', '127.0.0.1:11212', 'myhost:11213'])
rs = {
'test:10000': 'localhost:11211',
'test:20000': '127.0.0.1:11212',
'test:30000': '127.0.0.1:11212',
'test:40000': '127.0.0.1:11212',
'test:50000': '127.0.0.1:11212',
'test:60000': 'myhost:11213',
'test:70000': '127.0.0.1:11212',
'test:80000': '127.0.0.1:11212',
'test:90000': '127.0.0.1:11212',
}
for k in rs:
self.assertEqual(mc.get_host_by_key(k), rs[k])
def test_ketama_with_jocky_alias(self):
mc = Client([
'localhost localhost',
'myhost:11211 myhost',
'127.0.0.1:11212 127.0.0.1:11212',
'myhost:11213 myhost:11213'
])
rs = {
'test:10000': 'localhost',
'test:20000': '127.0.0.1:11212',
'test:30000': '127.0.0.1:11212',
'test:40000': '127.0.0.1:11212',
'test:50000': '127.0.0.1:11212',
'test:60000': 'myhost:11213',
'test:70000': '127.0.0.1:11212',
'test:80000': '127.0.0.1:11212',
'test:90000': '127.0.0.1:11212',
}
for k in rs:
self.assertEqual(mc.get_host_by_key(k), rs[k])
def test_ketama_with_alias(self):
mc = Client([
'192.168.1.211:11211 tango.mc.douban.com',
'192.168.1.212:11212 uniform.mc.douban.com',
'192.168.1.211:11212 victor.mc.douban.com',
'192.168.1.212:11211 whiskey.mc.douban.com',
#.........这里部分代码省略.........