使用Python操作memcache
2021-06-23 02:05
标签:class unknown turn hash算法 val ash lag python cas Python连接memcached的库有很多,处于简单以及高效的原则,最终选择了pymemcache, 使用Python操作memcache 标签:class unknown turn hash算法 val ash lag python cas 原文地址:https://www.cnblogs.com/navysummer/p/9675952.html
pip install pymemcache
from pymemcache.client.base import Client
client = Client((‘localhost‘, 11211))
client.set(‘some_key‘, ‘some_value‘)
result = client.get(‘some_key‘)
使用一致性HASH算法支持集群
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‘)
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‘)