-
Notifications
You must be signed in to change notification settings - Fork 0
memcached
Luke Chen edited this page Dec 26, 2020
·
12 revisions
https://github.com/memcached/memcached/wiki
https://realpython.com/python-memcache-efficient-caching/
https://pymemcache.readthedocs.io/en/latest/getting_started.html
lchen154@ubuntu:~/Downloads$ pip3 install pymemcache
https://readthedocs.org/projects/pymemcache/downloads/pdf/latest/
import json
from pymemcache.client.base import Client
class JsonSerde(object):
def serialize(self, key, value):
if isinstance(value, str):
return value, 1
return json.dumps(value), 2
def deserialize(self, key, value, flags):
if flags == 1:
return value
if flags == 2:
return json.loads(value)
raise Exception("Unknown serialization format")
client = Client(('localhost', 11211), serde=JsonSerde())
client.set('key', {'a':'b', 'c':'d'})
result = client.get('key')
>>> result['a']
'b'
from pymemcache.client.base import Client
from pymemcache import serde
client = Client(('localhost', 11211), serde=serde.pickle_serde)
>>> client.set('key', ['aaa', 'bbb', 'ccc'])
True
>>> client.get('key')
['aaa', 'bbb', 'ccc']
>>> client.get('key')[1]
'bbb'
Alternatively: https://pypi.org/project/python3-memcached/
lchen154@ubuntu:~/Downloads$ pip3 install python3-memcached
>>> import memcache
>>> client = memcache.Client(['localhost:11211'])
>>> data1 = ["apple", "banana", "cherry"]
>>> client.set('data1', data1)
True
>>> client.get('data1')
"['apple', 'banana', 'cherry']"
>>> data = client.get('data1')
>>> data
"['apple', 'banana', 'cherry']"