當前位置: 首頁>>代碼示例>>Python>>正文


Python Client.get_host_by_key方法代碼示例

本文整理匯總了Python中libmc.Client.get_host_by_key方法的典型用法代碼示例。如果您正苦於以下問題:Python Client.get_host_by_key方法的具體用法?Python Client.get_host_by_key怎麽用?Python Client.get_host_by_key使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在libmc.Client的用法示例。


在下文中一共展示了Client.get_host_by_key方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_realtime_host_w_failover

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
    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)
開發者ID:douban,項目名稱:libmc,代碼行數:20,代碼來源:test_router.py

示例2: test_md5_router_mass

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
 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]
開發者ID:douban,項目名稱:libmc,代碼行數:12,代碼來源:test_router.py

示例3: test_md5_router

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
    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))
開發者ID:douban,項目名稱:libmc,代碼行數:42,代碼來源:test_router.py

示例4: test_none_host

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
    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)
開發者ID:douban,項目名稱:libmc,代碼行數:25,代碼來源:test_router.py

示例5: test_ketama

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
 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])
開發者ID:douban,項目名稱:libmc,代碼行數:18,代碼來源:test_cmemcached_regression.py

示例6: test_crc_32_router

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
 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])
開發者ID:douban,項目名稱:libmc,代碼行數:19,代碼來源:test_router.py

示例7: test_ketama_with_alias

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]
 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])
開發者ID:douban,項目名稱:libmc,代碼行數:22,代碼來源:test_cmemcached_regression.py

示例8: CmemcachedRegressionCase

# 需要導入模塊: from libmc import Client [as 別名]
# 或者: from libmc.Client import get_host_by_key [as 別名]

#.........這裏部分代碼省略.........
            "pointer_size",
            "rusage_user",
            "rusage_system",
            "curr_items",
            "total_items",
            "bytes",
            "curr_connections",
            "total_connections",
            "connection_structures",
            "cmd_get",
            "cmd_set",
            "get_hits",
            "get_misses",
            "evictions",
            "bytes_read",
            "bytes_written",
            "limit_maxbytes",
            "threads",
        }
        self.assertTrue(set(st.keys()) >= st_keys)
        ''' TODO
        mc = cmemcached.Client([INVALID_SERVER_ADDR, self.server_addr])
        s = mc.stats()
        self.assertEqual(len(s), 2)
        '''

    def test_append_multi(self):
        N = 10
        K = "test_append_multi_%d"
        data = b"after\n"
        for i in range(N):
            self.assertEqual(self.mc.set(K % i, b"before\n"), True)
        keys = [K % i for i in range(N)]
        # append
        self.assertEqual(self.mc.append_multi(keys, data), True)
        self.assertEqual(self.mc.get_multi(keys),
                         dict(zip(keys, [b"before\n" + data] * N)))
        # prepend
        self.assertEqual(self.mc.prepend_multi(keys, data), True)
        self.assertEqual(self.mc.get_multi(keys),
                         dict(zip(keys, [data + b"before\n" + data] * N)))
        # delete
        self.assertEqual(self.mc.delete_multi(keys), True)
        self.assertEqual(self.mc.get_multi(keys), {})

    def test_append_multi_performance(self):
        N = 70000
        K = "test_append_multi_%d"
        data = b"after\n"
        keys = [K % i for i in range(N)]
        t = time.time()
        self.mc.append_multi(keys, data)
        t = time.time() - t
        threshold = 5
        assert t < threshold, 'should append 7w key in %s secs, ' \
                              'actual val: %f' % (threshold, t)

    def test_get_host(self):
        host = self.mc.get_host_by_key("str")
        self.assertEqual(host, self.server_addr)

    def test_get_list(self):
        self.mc.set("a", 'a')
        self.mc.delete('b')
        v = self.mc.get_list(['a', 'b'])
        self.assertEqual(v, ['a', None])

    @pytest.mark.skipif(np is None or cmemcached is None,
                        reason='cmemcached and numpy are not installed')
    def test_general_get_set_regression(self):
        key = 'anykey'
        key_dup = '%s_dup' % key
        for val in ('x', np.uint32(1), np.int32(2), 0, int(0), False, True):
            self.old_mc.set(key, val)
            val2 = self.mc.get(key)
            assert val2 == val
            self.mc.set(key_dup, val)
            val3 = self.old_mc.get(key_dup)
            assert val3 == val

    @pytest.mark.skipif(cmemcached is None,
                        reason='cmemcached is not installed')
    def test_large_mc_split_regression(self):
        key = 'anykey'
        key_dup = '%s_dup' % key
        for val in ('i' * int(_DOUBAN_CHUNK_SIZE * 1.5), 'small_value'):
            self.old_mc.set(key, val)
            assert self.mc.get(key) == self.old_mc.get(key) == val
            self.mc.set(key, val)
            assert self.mc.get(key) == self.old_mc.get(key) == val

            raw_val1, flags1 = self.old_mc.get_raw(key)
            assert len(raw_val1) <= len(val)  # compressed
            assert self.old_mc.set_raw(key_dup, raw_val1, 0, flags1)
            assert self.old_mc.get_raw(key) == self.old_mc.get_raw(key_dup)

            raw_val2, flags2 = self.mc.get_raw(key)
            assert len(raw_val2) <= len(val)  # compressed
            assert self.mc.set_raw(key_dup, raw_val2, 0, flags2)
            assert self.mc.get_raw(key) == self.mc.get_raw(key_dup)
開發者ID:douban,項目名稱:libmc,代碼行數:104,代碼來源:test_cmemcached_regression.py


注:本文中的libmc.Client.get_host_by_key方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。