当前位置: 首页>>代码示例>>Python>>正文


Python Client.set_raw方法代码示例

本文整理汇总了Python中libmc.Client.set_raw方法的典型用法代码示例。如果您正苦于以下问题:Python Client.set_raw方法的具体用法?Python Client.set_raw怎么用?Python Client.set_raw使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在libmc.Client的用法示例。


在下文中一共展示了Client.set_raw方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MCStore

# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import set_raw [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
开发者ID:leeccong,项目名称:code,代码行数:49,代码来源:dbclient.py

示例2: SingleServerCase

# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import set_raw [as 别名]

#.........这里部分代码省略.........
        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)
        assert mc.get(key_xl) == val

    def test_noreply(self):
        mc = self.noreply_mc
        assert mc.set('foo', 'bar')
        assert mc.touch('foo', 30)
        v, ck = mc.gets('foo')
        assert mc.cas('foo', 'bar2', 0, ck)
        mc.delete('foo')
        assert mc.set('foo', 1024)
        assert mc.incr('foo', 1) is None
        assert mc.get('foo') == 1025
        assert mc.decr('foo', 3) is None
        assert mc.get('foo') == 1022

    def test_injection(self):
        # credit to The New Page of Injections Book:
        #   Memcached Injections @ blackhat2014 [pdf](http://t.cn/RP0J10Z)
        mc = self.mc
        assert mc.delete('injected')
        assert mc.set('a' * 250, 'biu')
        assert not mc.set('a' * 251, 'biu')
        mc.set('a' * 251, 'set injected 0 3600 10\r\n1234567890')
        assert mc.get('injected') is None
        mc.delete('injected')
        mc.set('key1', '1234567890')
        mc.set('key1 0', '123456789012345678901234567890\r\n'
                         'set injected 0 3600 3\r\nINJ\r\n')
        assert mc.get('injected') is None

    def test_maxiov(self):
        key_tmpl = 'not_existed.%s'
        assert self.mc.get_multi([key_tmpl % i for i in range(10000)]) == {}

    def test_get_set_raw(self):
        self.mc.set('foo', 233)
        assert self.mc.get_raw('foo') == ('233', 2)
        self.mc.set_raw('foo', '2335', 0, 2)
        assert self.mc.get('foo') == 2335

    def test_stats(self):
        stats = self.mc.stats()
        for addr, dct in stats.iteritems():
            assert isinstance(dct['version'], str)
            assert (isinstance(dct['rusage_system'], float) or
                    isinstance(dct['rusage_user'], float))
            assert isinstance(dct['curr_connections'], int)

    def test_get_set_large_raw(self):
        key = 'large_raw_key'
        key_dup = '%s_dup' % key
        val = 'i' * int(_DOUBAN_CHUNK_SIZE * 1.5)
        for mc in (self.mc, self.compressed_mc):
            mc.set(key, val)
            assert mc.get(key) == val
            raw_val1, flags1 = mc.get_raw(key)
            assert mc.set_raw(key_dup, raw_val1, 0, flags1)
            assert mc.get_raw(key) == mc.get_raw(key_dup)

    def test_patch_no_compress(self):
        key = 'no_compress'
        val = '1024'
        self.mc.set(key, val, compress=False)
        large_patch = 'hahahaha' * 512
        self.mc.prepend(key, large_patch)
        assert self.mc.get(key) == large_patch + val
        self.mc.delete(key)

        self.mc.set(key, val, compress=False)
        large_patch = 'hahahaha' * 512
        self.mc.append(key, large_patch)
        assert self.mc.get(key) == val + large_patch
        self.mc.delete(key)
开发者ID:CloudNuts,项目名称:libmc,代码行数:104,代码来源:test_client.py

示例3: CmemcachedRegressionCase

# 需要导入模块: from libmc import Client [as 别名]
# 或者: from libmc.Client import set_raw [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.set_raw方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。