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