zoukankan      html  css  js  c++  java
  • python memcache 常用操作

    • add() 添加一条键值对,如果key已存在,重复执行add操作会报异常
    mc.add('name2', 'lisi')
    print(mc.get('name2'))
    # lisi
    • replace 修改某个key的值,如果key不存在,报异常
    # mc.add('name','wangwu')    # 添加个已存在key ,发生异常
    # MemCached: while expecting 'STORED', got unexpected response 'NOT_STORED'
    • set() 设置一个键值对,如果Key不存在,则创建,存在,则修改
    mc.set('name2', 'zhaoliu')
    print(mc.get('name2'))
    # zhaoliu

    add() 和set() 的区别:

    • add()用于向memcache添加一个要缓存的数据,当key存在时,调用失败
    • set()用于设置一个指定的key的缓存内容,key存在修改,不存在创建,set()是add()和replace()的集合体
    mport memcache
    
    mc = memcache.Client(['11.11.11.11:12000'], debug=True)
    
    mc.add('name2', 'lisi')
    print(mc.get('name2'))
    # lisi
    
    # mc.add('name','wangwu')    # 添加个已存在key ,发生异常
    # MemCached: while expecting 'STORED', got unexpected response 'NOT_STORED'
    mc.set('name2', 'zhaoliu')
    print(mc.get('name2'))
    # zhaoliu
    ####
    # C:Python27python.exe D:/Python/memcache/memcache2.py
    # lisi
    # zhaoliu
    #
    # Process finished with exit code
    • set_muilt() 设置多个键值对,key存在,修改,不存在,创建 键值对以字典形式传入
    mc.set_multi({'key1':'v100', 'key2':'v200', 'key3':'v300', 'key4':'v400', 'key5':'v500'})
    • get() 获取一个key的值
    • get_muilt() 获取多个key的值 ,多个key以list方式传入,返回一个字典对象
    print(mc.get('key5'))
    print(mc.get_multi(['key1', 'key2', 'key3', 'key4', 'key5']))
    # v500
    # {'key3': 'v300', 'key2': 'v200', 'key1': 'v100', 'key5': 'v500', 'key4': 'v400'}
    • delete() 删除指定的一个键值对
    • delete_muild() 删除指定的多个键值对 多个key以list方式传入
    mc.delete("key1")
    print(mc.get('key1'))
    # None
    mc.delete_multi(['key2', 'key3', 'key4'])
    print(mc.get_multi(['key1', 'key2', 'key3', 'key4', 'key5']))
    # {'key5': 'v500'}
    • append() 修改指定key的值,在该值后面追加内容
    • prepend() 修改指定key的值,在该值前面插入内容
    mc.add('test','hello')
    print(mc.get('test'))
    # hello
    mc.append('test', 'world')
    print(mc.get('test'))
    # helloworld
    mc.prepend('test', 'hi,')
    print(mc.get('test'))
    # hi,helloworld
    • incr(key[, n]) 自增,对某一个key的值增加N (N 默认为1)
    mc.add('num', '1101')
    mc.incr('num')
    print(mc.get('num'))
    mc.incr('num',100)
    print(mc.get('num')
    • decr(key[, n]) 自减,对某一个key的值减少N (N 默认为1)
    mc.set('num', '1000')
    mc.decr('num')
    print(mc.get('num'))
    # 999
    mc.decr('num',100)
    print(mc.get('num'))
    # 899
  • 相关阅读:
    Spring IOC注入接口多实现解决
    Spring Security 学习总结
    Spring Boot自动配置与Spring 条件化配置
    1403. Minimum Subsequence in Non-Increasing Order
    1457. Pseudo-Palindromic Paths in a Binary Tree
    1368. Minimum Cost to Make at Least One Valid Path in a Grid
    1456. Maximum Number of Vowels in a Substring of Given Length
    1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
    1472. Design Browser History
    1471. The k Strongest Values in an Array
  • 原文地址:https://www.cnblogs.com/ExMan/p/9777878.html
Copyright © 2011-2022 走看看