Registry / database / memcache

memcache

JSON →
library0.14.0pypypi✓ verified 23d ago

The `memcache` library (v0.14.0) is a pure-Python client for Memcached, compatible with Python 3.7+. It is a specific fork of the original `python-memcached` project, designed for Python 3.x compatibility, and has seen sporadic updates (last updated Dec 2023). It is distinct from the `python-memcached` (v1.x.x) series, despite the PyPI `memcache` package linking its homepage to the `python-memcached` GitHub repository, causing significant confusion in the ecosystem.

pip install memcache
INSTALL
IMPORT
SIG · MEMCACHE
M
memcache
databasepythonv0.14.0
Install
1.9s avg
Import
282ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.14.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.304s · 19.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.9s · import 0.260s · 20MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Memcache
from memcache import Memcache
import memcache
AsyncMemcache
from memcache import AsyncMemcache
MemcacheError
from memcache import MemcacheError

This example demonstrates how to initialize the `memcache` client, set, get, and delete values. It includes recommended practices like using the binary protocol and JSON serialization to avoid common pitfalls.

import memcache import os import json # Ensure a memcached server is running, e.g., on 127.0.0.1:11211. # Operations will silently fail (returning None/False) if no server is reachable. # Configuration for memcached servers. Allows environment override. MEMCACHED_SERVERS = os.environ.get('MEMCACHED_SERVERS', '127.0.0.1:11211').split(',') # Initialize the client with best practices: binary protocol and JSON serialization. # This avoids pickle security issues and improves interoperability. mc = memcache.Client( MEMCACHED_SERVERS, debug=0, # Set to 1 for more verbose debugging output binary=True, # Use binary protocol for better performance and features serializer=json.dumps, deserializer=json.loads ) key = "my_test_key" value = {"data": "hello world", "count": 123, "status": True} # Set a value, storing it for 60 seconds if mc.set(key, value, time=60): print(f"Set key '{key}' with value: {value}") else: print(f"Failed to set key '{key}'. Check server connection.") # Get a value retrieved_value = mc.get(key) if retrieved_value is not None: print(f"Retrieved key '{key}': {retrieved_value}") else: print(f"Failed to retrieve key '{key}' or key not found.") # Delete a value if mc.delete(key): print(f"Deleted key '{key}'.") else: print(f"Failed to delete key '{key}' or key was not present.") # Try getting it again to confirm deletion retrieved_value_after_delete = mc.get(key) if retrieved_value_after_delete is None: print(f"Key '{key}' is confirmed deleted (returned None).")
Debug
Known issues
gotchaThe `memcache` PyPI package (v0.14.0) is a specific fork targeting Python 3.7+, distinct from the more commonly maintained `python-memcached` (v1.x.x series), despite sharing historical code and a confusing PyPI homepage link. This often leads to misidentification and incorrect expectations about features or bug fixes.
fix
Carefully verify which `memcache` client you are installing and using. For most new projects, consider `python-memcached` (the v1.x.x series) or `pylibmc` for active maintenance and broader feature sets.
affects: 0.14.0 and earlier (of this fork)
gotchaBy default, non-string values are serialized using Python's `pickle` module, which can introduce security vulnerabilities if deserializing data from untrusted sources. It also limits interoperability with other memcached clients not using pickle.
fix
Pass a custom `serializer` and `deserializer` to the `Client` constructor (e.g., using `json` for simple data) to ensure safe and interoperable data handling. Example: `mc = memcache.Client(..., serializer=json.dumps, deserializer=json.loads)`.
affects: All versions of this `memcache` fork
gotchaThe client defaults to the older ASCII protocol. For better performance, efficiency, and to enable features like bigger value sizes or SASL authentication, the binary protocol is recommended.
fix
Initialize the client with `binary=True`: `mc = memcache.Client(['127.0.0.1:11211'], binary=True)`.
affects: All versions of this `memcache` fork
gotchaError handling for network issues or server unavailability often results in `None` being returned for `get` operations or `False`/`0` for `set`, without raising specific exceptions. Debugging connection problems can be difficult.
fix
Implement explicit checks for `None`, `False`, or `0` return values after operations to detect failures. For more robust error reporting, consider using a more modern client like `python-memcached` (v1.x.x) or `pylibmc`.
affects: All versions of this `memcache` fork
Upgrade
Version history
0.14.0latest on PyPI · released Sep 10, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
4
Amazon
1
Resources
memcache — pip install memcache · libregistry