本文整理汇总了Python中libmc.Client类的典型用法代码示例。如果您正苦于以下问题:Python Client类的具体用法?Python Client怎么用?Python Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main():
mc = Client(["127.0.0.1:%d" % (21211 + i) for i in range(10)])
dct = {"test_key_%d" % i: "i" * 1000 for i in range(10)}
while True:
print "%.2f: set_multi: %r" % (time.time(), mc.set_multi(dct))
print "%.2f: get_multi: %r" % (time.time(), mc.get_multi(dct.keys()) == dct)
time.sleep(0.5)
print
示例2: setUp
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)
示例3: mc_sleeper
def mc_sleeper():
s = 2
print "sleep in C++ for %ds, you can sing infinitely now, singer." % s
mc = Client(["127.0.0.1:21211"])
release_gil = True # toggle here
stack.append("mc_sleep_begin")
mc._sleep(s, release_gil)
stack.append("mc_sleep_end")
print "exit sleep"
示例4: test_md5_router_mass
def test_md5_router_mass(self):
with open(os.path.join(RES_DIR, 'server_port.csv')) as fhandler:
server_list = [':'.join(addr.strip().split(','))
for addr in fhandler.readlines()]
mc = Client(server_list, hash_fn=libmc.MC_HASH_MD5)
with open(os.path.join(RES_DIR, 'key_pool_idx.csv')) as fhandler:
for line in fhandler:
key, idx_ = line.strip().split(',')
idx = int(idx_)
assert mc.get_host_by_key(key) == server_list[idx]
示例5: main
def main():
mc = Client(['127.0.0.1:%d' % (21211 + i) for i in range(10)])
dct = {'test_key_%d' % i: 'i' * 1000 for i in range(10)}
while True:
print '%.2f: set_multi: %r' % (time.time(), mc.set_multi(dct))
print '%.2f: get_multi: %r' % (
time.time(), mc.get_multi(dct.keys()) == dct
)
time.sleep(0.5)
print
示例6: __init__
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
示例7: ErrorCodeTestCase
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
示例8: test_ketama
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])
示例9: test_crc_32_router
def test_crc_32_router(self):
crc_32_mc = Client([
'localhost', 'myhost:11211', '127.0.0.1:11212', 'myhost:11213'
], hash_fn=libmc.MC_HASH_CRC_32)
rs = {
'test:10000': '127.0.0.1:11212',
'test:20000': '127.0.0.1:11212',
'test:30000': '127.0.0.1:11212',
'test:40000': 'myhost:11213',
'test:50000': 'myhost:11211',
'test:60000': 'localhost:11211',
'test:70000': 'myhost:11213',
'test:80000': 'myhost:11211',
'test:90000': 'localhost:11211'
}
for k in rs:
self.assertEqual(crc_32_mc.get_host_by_key(k), rs[k])
示例10: test_ketama_with_alias
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',
])
rs = {
'test:10000': 'whiskey.mc.douban.com',
'test:20000': 'victor.mc.douban.com',
'test:30000': 'victor.mc.douban.com',
'test:40000': 'victor.mc.douban.com',
'test:50000': 'victor.mc.douban.com',
'test:60000': 'uniform.mc.douban.com',
'test:70000': 'tango.mc.douban.com',
'test:80000': 'victor.mc.douban.com',
'test:90000': 'victor.mc.douban.com',
}
for k in rs:
self.assertEqual(mc.get_host_by_key(k), rs[k])
示例11: test_realtime_host_w_failover
def test_realtime_host_w_failover(self):
servers = ["127.0.0.1:21211", "127.0.0.1:21212"]
mc = Client(servers)
failover_mc = Client(servers, failover=True)
hosts_visited = set()
for i in range(1000):
key = "test:realtime:route:%d" % i
h1 = mc.get_host_by_key(key)
h2 = mc.get_realtime_host_by_key(key)
assert h1 is not None
assert h1 == h2
h3 = failover_mc.get_host_by_key(key)
h4 = failover_mc.get_realtime_host_by_key(key)
assert h3 == h1
assert h4 == h1
hosts_visited.add(h1)
assert len(hosts_visited) == len(servers)
示例12: test_md5_router
def test_md5_router(self):
server_list = ['localhost', 'myhost:11211', '127.0.0.1:11212',
'myhost:11213']
mc = Client(server_list)
md5_mc = Client(server_list, hash_fn=libmc.MC_HASH_MD5)
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])
self.assertEqual(md5_mc.get_host_by_key(k), rs[k])
for addr in server_list:
ps = addr.split(':')
if len(ps) == 1:
hostname = ps[0]
port = 11211
else:
hostname = ps[0]
port = int(ps[1])
if port == 11211:
key = '%s-10' % hostname
else:
key = '%s:%s-10' % (hostname, port)
self.assertEqual(mc.get_host_by_key(key),
'%s:%s' % (hostname, port))
self.assertEqual(md5_mc.get_host_by_key(key),
'%s:%s' % (hostname, port))
示例13: test_none_host
def test_none_host(self):
existed_server = "127.0.0.1:21211"
not_existed_server = "127.0.0.1:1"
servers = [existed_server, not_existed_server]
mc = Client(servers)
failover_mc = Client(servers, failover=True)
hosts_visited = set()
for i in range(1000):
key = "test:realtime:route:%d" % i
h1 = mc.get_host_by_key(key)
h2 = mc.get_realtime_host_by_key(key)
if h1 == existed_server:
assert h1 == h2
elif h1 == not_existed_server:
assert h2 is None
h3 = failover_mc.get_host_by_key(key)
h4 = failover_mc.get_realtime_host_by_key(key)
assert h3 == h1
assert h4 == existed_server
hosts_visited.add(h1)
assert len(hosts_visited) == len(servers)
示例14: CmemcachedRegressionCase
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"
#.........这里部分代码省略.........
示例15: Memcached
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)