Python之memcache(二)
1.add、replace、set
#!/usr/bin/env python # -*- coding:utf8 -*- # @Time : 2017/12/11 13:59 # @Author : hantong # @File : memcache_2.py import memcache mc = memcache.Client(["10.3.1.126:12000"],debug=True) #add 增加数据 mc.add("k1","v1") print(mc.get('k1')) #replace 替换 mc.replace("k1","Hello World") #get获取key的对应的value print(mc.get("k1")) #set(key value) 设置 mc.set("k2","v2") print(mc.get("k2")) mc.set("k2","Hello K2") print(mc.get("k2")) #如果key值存在,add就会报错,set不会报错,会进行重新复制并覆盖 #set_multi(dict)一次设置多个key:value #get_multi(list)一次获取到多个key,多个ke时,以list形式作为参数传入,返回类型是dict mc.set_multi({"k100":"v100","k101":"v101","k102":"v102"}) print(mc.get_multi(["k100","k101","k102"]))执行结果:
Hello World
Hello World
v2
Hello K2
{'k100': 'v100', 'k101': 'v101', 'k102': 'v102'}
2.append、prepend、incr、decr
#!/usr/bin/env python # -*- coding:utf8 -*- # @Time : 2017/12/11 14:45 # @Author : hantong # @File : mamcache_3.py import memcache mc = memcache.Client(["10.3.1.126:12000"],debug=True) mc.set_multi({"test1":"v1","test2":"v2","test3":"v3"}) print(mc.get_multi(["test1","test2","test3"])) #append 末尾追加 #prepend 开头追加 mc.set_multi({"test1":"v1","test2":"v2","test3":"v3"}) mc.append("test1","ling") #在test1的values后面追加ling print(mc.get("test1")) mc.prepend("test2","hello") print(mc.get("test2")) #incr自增,默认自增1 #decr自减,默认自减1 mc.set("shop",1000) mc.incr("shop") print(mc.get("shop")) #默认自增1 mc.incr("shop",100) print(mc.get("shop")) mc.decr("shop") #默认自减1 print(mc.get("shop")) mc.decr("shop",200) print(mc.get("shop")) #delete删除 mc.delete("test1") mc.delete_multi(["test2","test3"])执行结果:
{'test1': 'v1', 'test3': 'v3', 'test2': 'v2'}
v1ling
hellov2
1001
1101
1100
900