目录
1>、libmc 由 Douban 开发和维护(豆瓣牛B)
2>、不过也嘲讽一波豆瓣所谓的开源(Python Example)
import libmc
mc = libmc.Client(['localhost:7900'])
mc.set("foo", "bar")
mc.get("foo")
3>、以下为豆瓣开源 libmc 源码以及借鉴 python3 交互 memcached(即 import memcache)测试推出
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
'''========================================================
@Project -> File :GoBeansDB -> python3_libmc_beansdb.py
@IDE :PyCharm
@Author :Mr. Wufei
@Date :2019/10/28 20:01
@Desc :Python3 BeansDB / GoBeansDB 连接 - libmc 驱动模块
========================================================='''
import libmc
class InteractiveConn(object):
def __init__(self, db_ips_port_list):
"""
推荐使用 libmc 客户端,获取所有(一个或多个)Client
:param db_ips_port_list: ['beansdb_ip1:beansdb_port1', 'beansdb_ip2:beansdb_port2']
"""
self.__mc = libmc.Client(db_ips_port_list)
def set_key_value(self, key, value):
"""
设置指定 key 的值:写入 K/V 到数据库。(日志重要信息截取:SET SUCC beansdb_ip:beansdb_port key)
:param key:
:param value:
:return: True/False
"""
return self.__mc.set(key, value)
def get_key(self, key):
"""
获取指定 key 的值,没有则返回 None。(日志重要信息截取:GET SUCC beansdb_ip:beansdb_port key)
:param key:
:return: value
"""
return self.__mc.get(key)
def del_key(self, key):
"""
删除指定 key。(日志重要信息截取:key 不存在时:DELETE SUCC NoWhere key;key 存在是:DELETE SUCC beansdb_ip:beansdb_port key)
:param key:
:return: True/False
"""
return self.__mc.delete(key)
def set_multi_dict(self, keys_values_dict):
"""
同时设置一个或多个 key-value。(日志重要信息截取同 set,相当于依次执行 set)
:param keys_values_dict: 字典 {key1 : value1, key2 : value2 , key3 : value3}
:return: True/False
"""
return self.__mc.set_multi(keys_values_dict)
def get_multi_list(self, keys_list):
"""
获取所有(一个或多个)给定 key 的值。(日志重要信息截取:GETM SUCC beansdb_ip1:beans_port1,beansdb_ip2:beansdb_port2,beansdb_ip3:beansdb_port3 key1 key2 key3)
:param keys_list: 列表 [key1, key2, key3]
:return: 字典 {key1 : value1, key2 : value2 , key3 : value3}
"""
return self.__mc.get_multi(keys_list)
def del_multi_list(self, keys_list):
"""
删除所有(一个或多个)给定 key 的值。(日志重要信息截取同 delete,相当于依次执行 delete)
:param keys_list: 列表 [key1, key2, key3]
:return: True/False
"""
return self.__mc.delete_multi(keys_list)
"""
if __name__ == '__main__':
# Python Example
db_ips_port_list = ['10.20.3.36:7905']
mc = InteractiveConn(db_ips_port_list)
print('>> set_key_value()')
cc01 = mc.set_key_value('wufei222', 'SHOWufei222')
print(cc01)
print('>> get_key()')
cc02 = mc.get_key('wufei222')
print(cc02)
print('>> del_key()')
cc03 = mc.del_key('wufei222')
print(cc03)
print('>> get_key()')
cc04 = mc.get_key('wufei222')
print(cc04)
print('>> set_multi_dict()')
keys_values_dict = {'wf01' : 'feifei01', 'wf02' : 'feifei02' , 'wf03' : 'feifei03'}
cc05 = mc.set_multi_dict(keys_values_dict)
print(cc05)
print('>> get_multi_list()')
keys_list = ['wf01', 'wf02', 'wf03']
cc06 = mc.get_multi_list(keys_list)
print(cc06)
print('>> del_multi_list()')
cc07 = mc.del_multi_list(keys_list)
print(cc07)
print('>> get_multi_list()')
cc08 = mc.get_multi_list(keys_list)
print(cc08)
"""
if __name__ == '__main__':
# Python Example
db_ips_port_list = ['10.20.3.36:7905']
mc = InteractiveConn(db_ips_port_list)
print('>> set_key_value()')
cc01 = mc.set_key_value('wufei222', 'SHOWufei222')
print(cc01)
print('>> get_key()')
cc02 = mc.get_key('wufei222')
print(cc02)
print('>> del_key()')
cc03 = mc.del_key('wufei222')
print(cc03)
print('>> get_key()')
cc04 = mc.get_key('wufei222')
print(cc04)
print('>> set_multi_dict()')
keys_values_dict = {'wf01' : 'feifei01', 'wf02' : 'feifei02' , 'wf03' : 'feifei03'}
cc05 = mc.set_multi_dict(keys_values_dict)
print(cc05)
print('>> get_multi_list()')
keys_list = ['wf01', 'wf02', 'wf03']
cc06 = mc.get_multi_list(keys_list)
print(cc06)
print('>> del_multi_list()')
cc07 = mc.del_multi_list(keys_list)
print(cc07)
print('>> get_multi_list()')
cc08 = mc.get_multi_list(keys_list)
print(cc08)
>> set_key_value()
True
>> get_key()
SHOWufei222
>> del_key()
True
>> get_key()
None
>> set_multi_dict()
True
>> get_multi_list()
{'wf01': 'feifei01', 'wf02': 'feifei02', 'wf03': 'feifei03'}
>> del_multi_list()
True
>> get_multi_list()
{}