python3 交互 memcached

2019-05-30 21:46:14 浏览数 (1)

使用pip3安装python3-memcached库

pip3 install python3-memcached

编写工具类示例

代码语言:javascript复制
import memcache

class MemcachedHelper():

    def __init__(self,ip,port):
        connect = str(ip)   ':'   str(port)
        self.mc = memcache.Client([connect], debug=0)

    def set_key(self,key,value):
        return self.mc.set(key,value)

    def get_key(self,key):
        return self.mc.get(key)

    def delte_key(self,key):
        return self.mc.delete(key)

    def incr_key(self,key):
        return self.mc.incr(key)

    def decr_key(self,key):
        return self.mc.decr(key)

    def set_muti(self,keys_values_dict):
        return self.mc.set_multi(keys_values_dict)

    def get_multi(self,keys_list):
        return self.mc.get_multi(keys_list)

    def del_multi(self,keys_list):
        return self.mc.delete_multi(keys_list)

    def get_stats(self):
        return self.mc.get_stats()[0][1]

    def print_stats_info(self):
        stats_dict = self.mc.get_stats()[0][1]
        for key in stats_dict:
            print("%s , %s" % (key.decode(encoding='utf-8'), stats_dict[key].decode(encoding='utf-8')))

    def get_stats_info(self,name):
        return self.mc.get_stats()[0][1][name.encode(encoding="utf-8")].decode(encoding='utf-8')

    def get_info_cmd_set(self):
        return self.mc.get_stats()[0][1][b'cmd_set'].decode(encoding='utf-8')



def main():
    mh = MemcachedHelper('192.168.196.129',11211)

    mh.print_stats_info()

    print("==============================")

    print("curr_items = %s" % mh.get_stats_info('curr_items'))

if __name__ == '__main__':
    main()

执行如下:

代码语言:javascript复制
D:Python37python3.exe D:/pythonProject/memcached_test/test3.py
pid , 910
uptime , 17386
time , 1557058095
version , 1.4.15
libevent , 2.0.21-stable
pointer_size , 64
rusage_user , 6.542740
rusage_system , 4.809214
curr_connections , 11
total_connections , 7561
connection_structures , 1005
reserved_fds , 20
cmd_get , 30
cmd_set , 2774817
cmd_flush , 0
cmd_touch , 0
get_hits , 27
get_misses , 3
delete_misses , 0
delete_hits , 3
incr_misses , 0
incr_hits , 3
decr_misses , 0
decr_hits , 3
cas_misses , 0
cas_hits , 0
cas_badval , 0
touch_hits , 0
touch_misses , 0
auth_cmds , 0
auth_errors , 0
bytes_read , 2904242070
bytes_written , 22245593
limit_maxbytes , 67108864
accepting_conns , 1
listen_disabled_num , 58
threads , 4
conn_yields , 85668
hash_power_level , 16
hash_bytes , 524288
hash_is_expanding , 0
bytes , 61553823
curr_items , 55759
total_items , 2774823
expired_unfetched , 0
evicted_unfetched , 2680427
evictions , 2680427
reclaimed , 0
==============================
curr_items = 55759

Process finished with exit code 0

0 人点赞