Skip to content

memcached

Luke Chen edited this page Dec 27, 2020 · 12 revisions

memcached

https://github.com/memcached/memcached/wiki

$ ls -la /usr/local/bin/memcached 
-rwxr-xr-x 1 root root 1356496 Dec 26 14:53 /usr/local/bin/memcached
$ memcached

memcached client in python

pymemcache

https://realpython.com/python-memcache-efficient-caching/ https://pymemcache.readthedocs.io/en/latest/getting_started.html

$ 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'

python3-memcached

https://pypi.org/project/python3-memcached/

$ 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']"

Clone this wiki locally