{"id":4106,"library":"memcache","title":"Memcache Client (PyPI `memcache`)","description":"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.","status":"active","version":"0.14.0","language":"en","source_language":"en","source_url":"https://github.com/memcache/python-memcache","tags":["memcache","caching","fork","data-store"],"install":[{"cmd":"pip install memcache","lang":"bash","label":"Install memcache"}],"dependencies":[],"imports":[{"note":"The Client class is typically accessed via the top-level 'memcache' module, not directly imported.","wrong":"from memcache import Client","symbol":"Client","correct":"import memcache\nmc = memcache.Client(...)"}],"quickstart":{"code":"import memcache\nimport os\nimport json\n\n# Ensure a memcached server is running, e.g., on 127.0.0.1:11211.\n# Operations will silently fail (returning None/False) if no server is reachable.\n\n# Configuration for memcached servers. Allows environment override.\nMEMCACHED_SERVERS = os.environ.get('MEMCACHED_SERVERS', '127.0.0.1:11211').split(',')\n\n# Initialize the client with best practices: binary protocol and JSON serialization.\n# This avoids pickle security issues and improves interoperability.\nmc = memcache.Client(\n    MEMCACHED_SERVERS,\n    debug=0, # Set to 1 for more verbose debugging output\n    binary=True, # Use binary protocol for better performance and features\n    serializer=json.dumps,\n    deserializer=json.loads\n)\n\nkey = \"my_test_key\"\nvalue = {\"data\": \"hello world\", \"count\": 123, \"status\": True}\n\n# Set a value, storing it for 60 seconds\nif mc.set(key, value, time=60):\n    print(f\"Set key '{key}' with value: {value}\")\nelse:\n    print(f\"Failed to set key '{key}'. Check server connection.\")\n\n# Get a value\nretrieved_value = mc.get(key)\nif retrieved_value is not None:\n    print(f\"Retrieved key '{key}': {retrieved_value}\")\nelse:\n    print(f\"Failed to retrieve key '{key}' or key not found.\")\n\n# Delete a value\nif mc.delete(key):\n    print(f\"Deleted key '{key}'.\")\nelse:\n    print(f\"Failed to delete key '{key}' or key was not present.\")\n\n# Try getting it again to confirm deletion\nretrieved_value_after_delete = mc.get(key)\nif retrieved_value_after_delete is None:\n    print(f\"Key '{key}' is confirmed deleted (returned None).\")","lang":"python","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"gotcha","affected_versions":"0.14.0 and earlier (of this fork)"},{"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)`.","message":"By 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.","severity":"gotcha","affected_versions":"All versions of this `memcache` fork"},{"fix":"Initialize the client with `binary=True`: `mc = memcache.Client(['127.0.0.1:11211'], binary=True)`.","message":"The 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.","severity":"gotcha","affected_versions":"All versions of this `memcache` fork"},{"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`.","message":"Error 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.","severity":"gotcha","affected_versions":"All versions of this `memcache` fork"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}