Python memcache Common operations

Source: Internet
Author: User

    • Add () Adds a key value pair, and if key already exists, repeating the add operation will report an exception

      mc.add(‘name2‘, ‘lisi‘)print(mc.get(‘name2‘))# lisi
    • Replace modifies the value of a key, and if key does not exist, the report is abnormal

      # mc.add(‘name‘,‘wangwu‘)    # 添加个已存在key ,发生异常# MemCached: while expecting ‘STORED‘, got unexpected response ‘NOT_STORED‘
    • Set () sets a key-value pair, if key does not exist, is created, exists, then modified

      mc.set(‘name2‘, ‘zhaoliu‘)print(mc.get(‘name2‘))# zhaoliu
The difference between add () and set ():
    • Add () Adds a data to the memcache to be cached, and the call fails when key is present
    • Set () sets the cache contents of a specified key, there is a change in key, there is no creation, set () is the collection of Add () and replace ()
mport memcachemc = 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:\Python27\python.exe D:/Python/memcache/memcache2.py# lisi# zhaoliu## Process finished with exit code
  • Set_muilt () sets multiple key-value pairs, key exists, modifies, does not exist, creates a key-value pair to be passed in as a dictionary

    mc.set_multi({‘key1‘:‘v100‘, ‘key2‘:‘v200‘, ‘key3‘:‘v300‘, ‘key4‘:‘v400‘, ‘key5‘:‘v500‘})
  • Get () Gets the value of a key
  • Get_muilt () Gets the value of more than one key, and multiple keys are passed in list mode, returning a Dictionary object

    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 () Deletes a specified key-value pair
  • Delete_muild () Deletes a specified number of key values to multiple keys in list mode

    mc.delete("key1")print(mc.get(‘key1‘))# Nonemc.delete_multi([‘key2‘, ‘key3‘, ‘key4‘])print(mc.get_multi([‘key1‘, ‘key2‘, ‘key3‘, ‘key4‘, ‘key5‘]))# {‘key5‘: ‘v500‘}
  • Append () modifies the value of the specified key, appending the content after the value
  • Prepend () modifies the value of the specified key to insert the contents before the value

    mc.add(‘test‘,‘hello‘)print(mc.get(‘test‘))# hellomc.append(‘test‘, ‘world‘)print(mc.get(‘test‘))# helloworldmc.prepend(‘test‘, ‘hi,‘)print(mc.get(‘test‘))# hi,helloworld
  • INCR (key[, n]) increments the value of a key by n (n default = 1)

    mc.add(‘num‘, ‘1101‘)mc.incr(‘num‘)print(mc.get(‘num‘))mc.incr(‘num‘,100)print(mc.get(‘num‘)
  • DECR (key[, n]) decrement, the value of one key is reduced by N (n default is 1)

    mc.set(‘num‘, ‘1000‘)mc.decr(‘num‘)print(mc.get(‘num‘))# 999mc.decr(‘num‘,100)print(mc.get(‘num‘))# 899

Python memcache Common operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.