Memcached Usage Summary: Use Python to manipulate Memcache__python

Source: Internet
Author: User
Python Connection memcached Library There are many, in a simple and efficient principle, the final choice of Pymemcache, the advantages of fully implemented memcached text protocol for SEND/RECV operation can be configured timeout support "noreply" characteristics , this can be the first out of the faster write speed to make serialization/deserialization simpler can be a network exception, memecached error as a cache loss installation Pymemcache
Pip install Pymemcache use Pymemcache Basic operation
From pymemcache.client.base Import client

client = client ((' localhost ', 11211))
client.set (' Some_key ', ' some_ Value ') Result
= Client.get (' Some_key ')
Using the Memcache cluster
Cluster support using a consistent hash algorithm
From Pymemcache.client.hash import hashclient

client = hashclient ([(' 127.0.0.1 ', 11211), (' 127.0.0.1 ', 11212)]
client.set (' Some_key ', ' some value ') result
= Client.get (' Some_key ')

Serialization operations
Import JSON
from pymemcache.client.base import client

def json_serializer (key, value): If type (value) = = str: return value, 1 return
     json.dumps (value), 2

def json_deserializer (key, value, flags): if Flags = = 1:return value
  if flags = = 2:return Json.loads (value) raiseexception ("Unknown serialization format")

client = Client (' LocalHost ', 11211, Serializer=json_serializer,
                deserializer=json_deserializer)
client.set (' key ', {' A ': ' B ', ' C ': ' d '} ' result
= Client.get (' key ')

Best practices when constructing a client, add a timeout configuration to prevent block operations from using "noreply" to improve performance by default by changing properties to "set", "Add", "Replace", "append", "prepend", and " Delete. Operation is on, "CAS", "incr" and "DECR". Use Get_many and Gets_many operations as close as possible in operation to reduce round trip practice using the "Ignore_exc" attribute, The network exception, memecached error as the main url:pypi:https://pypi.python.org/pypi/cache loss PymemcacheOfficial documents: https://pymemcache.readthedocs.io/en/latest/getting_started.html

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.