当前位置: 首页 > 工具软件 > pylibmc > 使用案例 >

pylibmc客户端pool使用

富凯旋
2023-12-01

http://sendapatch.se/projects/pylibmc/pooling.html#thread-mapped-pooling

 

#!/usr/bin/env python

#import memcache
import pylibmc

class OSDClient():

    """ OSD Client Operation"""
    def __init__(self, osd_dev, **kwargs):
        #osd_devs = "127.0.0.1:11212", "127.0.0.1:11213", "127.0.0.1:11214"
        list_item = list(osd_dev)
        self.mem_server = ':'.join(list_item)
        server = [self.mem_server.strip()]
        mc_pool_size = 4
        #self.memcache = memcache.Client(server)
        #for osd_dev in osd_devs:
        #    mc = memcache.Client(server)
        #for osd_dev in osd_devs:
        #server = "127.0.0.1:11212",
        self.mc = pylibmc.Client(server)
        self.pool = pylibmc.ClientPool(self.mc, mc_pool_size)
       
    def get(self, key):
        """
        Get an object from storage based on the specified key,
        if error, return None

        :param key: the key to a object
        """
        with self.pool.reserve() as mc:
            result = mc.get(key)
        return result

    def delete(self, key):
        """
        Delete an object from storage based on the specified key,
        if success, return 0

        :param key: the key to a object
        :param time: specified time (second)
        """
        with self.pool.reserve() as mc:
            result = mc.delete(key)
        return result

    def set(self, key, val):
        """
        Set an object to storage, if success, return 0

        :param key: specified key to a object
        :param val: object value
        :param time: timeout (second)
        :param min_compress_len: used to zlib compress
        """
        with self.pool.reserve() as mc:
            result = mc.set(key, val)
        return result
'''
dev = "127.0.0.1,11212",
print dev

osd_client = OSDClient(dev)


print osd_client.set('1','1')

print osd_client.get('1')


print osd_client.delete('1')

print osd_client.get('1')

'''

 类似资料: