pymemcache is a comprehensive, fast, pure-Python client for memcached. It fully implements the memcached text protocol and supports connections over UNIX sockets, TCP (IPv4 or IPv6), and configurable timeouts. It is currently at version 4.0.0 and maintains an active release cadence.
pip install pymemcacheVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a memcached server using `pymemcache.Client`, set a key-value pair with an expiration, and retrieve it. Note that `pymemcache` returns values as bytes, requiring explicit decoding for string values.
Upgrade to Python 3.7+ or pin pymemcache to <4.0.0.
After retrieval, use `.decode('utf-8')` (or appropriate encoding) on the returned value: `value.decode('utf-8')`.Ensure keys are ASCII compliant. For unicode keys, instantiate the client with `allow_unicode_keys=True` and ensure consistent encoding (e.g., UTF-8) across clients.
Convert any float `expire` values to `int`.
Update custom serialization logic to use the `serde` object pattern. For example: `client = Client(..., serde=MyCustomSerde())`.
Always provide explicit `connect_timeout` and `timeout` values (e.g., `Client(..., connect_timeout=1, timeout=0.5)`).
Install the library using pip: `pip install pymemcache`
Ensure the Memcached server is running and accessible from the machine where your Python application is executing, and that the client is configured with the correct host and port (e.g., `Client('127.0.0.1:11211')`).Change the import statement from `import memcache` to `from pymemcache.client.base import Client`.
Verify that all Memcached servers specified in the client configuration are running and reachable. For `Client` instances, this typically means the single configured server is down. Consider using `RetryingClient` or `ignore_exc=True` for graceful degradation, but always investigate server availability.
Decode the retrieved bytes to a string (e.g., `client.get('key').decode('utf-8')`) or configure the client with a `serde` object or separate `serializer` and `deserializer` functions upon initialization. For example: `from pymemcache.serde import python_memcache_serializer, python_memcache_deserializer; client = Client(('localhost', 11211), serializer=python_memcache_serializer, deserializer=python_memcache_deserializer)`.