memcache常用方法
存储命令:set/add/replace/append/prepend/
获取命令:get/gets
其他命令:delete/stats
add 方法 : 添加一条键值对,如果key存在,重复执行add会报错
replace方法:replace修改某个key的值,如果key不存在,则异常。
import memcache mc = memcache.Client([(‘192.168.88.102:11211‘)],debug=True) #add(key,value) mc.add("name5","cnblogs") print mc.get("name5") #replace mc.replace("name5","hello world") print mc.get("name5") mc.set("k2","v2") print mc.get("k2")
运行结果:
cnblogs hello world v2
set和set_multi方法
set :设置一个键值对,如果key不存在,则创建,如果key存在,则修改;
set_multi:设置多个键值对,如果key不存在,则创建,如果key存在,则修改。
import memcache mc = memcache.Client([(‘192.168.88.102:11211‘)],debug=True) mc.set_multi({"k100":"v100","k101":"v101","k102":"v102"}) print mc.get_multi(["k100","k101","k102"])
运行结果:
{‘k100‘: ‘v100‘, ‘k101‘: ‘v101‘, ‘k102‘: ‘v102‘}
import memcache mc = memcache.Client([(‘192.168.88.102:11211‘)],debug=True) mc.set_multi({"test1":"vale2","test2":"value2","test3":"value3","test4":"value4",}) print mc.get_multi(["test1","test2","test3","test4"]) mc.delete("test1") mc.delete_multi(["test2","test3"]) print mc.get_multi(["test1","test2","test3","test4"]) 运行结果: {‘test1‘: ‘vale2‘, ‘test3‘: ‘value3‘, ‘test2‘: ‘value2‘, ‘test4‘: ‘value4‘} {‘test4‘: ‘value4‘}
append和prepend方法 append:修改指定key的值,在该值后面追加内容; prepend:修改指定key的值,在该值前面插入内容。 mc = memcache.Client([(‘192.168.88.102:11211‘)],debug=True) mc.set_multi({"test1":"vale2","test2":"value2","test3":"value3","test4":"value4",}) mc.append("test1","ling") print mc.get("test1") mc.prepend("test2","hello") print mc.get("test2") 运行结果: vale2ling hellovalue2
时间: 2024-10-12 08:36:52