{"id":4387,"library":"python-memcached","title":"python-memcached Client","description":"python-memcached is a pure Python client for the memcached memory cache daemon. It provides a simple interface for storing and retrieving key-value pairs in one or more memcached servers. Currently at version 1.62, the library is stable but largely in maintenance mode, with `pymemcache` being suggested as a more actively developed alternative. Releases are infrequent but the project is still maintained.","status":"maintenance","version":"1.62","language":"en","source_language":"en","source_url":"https://github.com/linsomniac/python-memcached","tags":["memcached","cache","caching","client"],"install":[{"cmd":"pip install python-memcached","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The top-level package name is `memcache`, not `python_memcached` as one might infer from the PyPI slug.","wrong":"from python_memcached import Client","symbol":"Client","correct":"from memcache import Client"}],"quickstart":{"code":"import memcache\nimport os\n\n# Connect to a memcached server. Use environment variable for host if available.\n# Default to localhost:11211 if MEMCACHED_SERVER is not set.\nMEMCACHED_SERVER = os.environ.get('MEMCACHED_SERVER', '127.0.0.1:11211')\nmc = memcache.Client([MEMCACHED_SERVER], debug=0)\n\n# Set a key-value pair with an expiration time of 60 seconds\nmc.set(\"my_key\", \"Hello, Memcached!\", time=60)\nprint(f\"Set 'my_key': 'Hello, Memcached!' (expires in 60s)\")\n\n# Get the value for the key\nvalue = mc.get(\"my_key\")\nprint(f\"Got 'my_key': {value}\")\n\n# Set a complex object (will be pickled by default)\nsample_object = {\"name\": \"Test User\", \"id\": 123}\nmc.set(\"user_data\", sample_object)\nprint(f\"Set 'user_data': {sample_object}\")\n\n# Retrieve the complex object\nretrieved_object = mc.get(\"user_data\")\nprint(f\"Got 'user_data': {retrieved_object}\")\n\n# Delete a key\nmc.delete(\"my_key\")\nprint(f\"Deleted 'my_key'.\")\n\n# Try to get deleted key\ndeleted_value = mc.get(\"my_key\")\nprint(f\"Attempt to get 'my_key' after deletion: {deleted_value} (should be None)\")","lang":"python","description":"This quickstart demonstrates how to connect to a Memcached server, set and retrieve string values, store and retrieve Python objects (which are automatically pickled/unpickled), and delete keys using `python-memcached`."},"warnings":[{"fix":"Update code to expect `0` for not found/errors and `1` for success when checking `delete()` results.","message":"The `delete()` method's return value changed in version 1.62. It now returns `1` for successful deletion ('DELETED') and `0` for 'NOT_FOUND' or server errors. Previously, its behavior might have been less consistent or different depending on the server response.","severity":"breaking","affected_versions":">=1.62"},{"fix":"Upgrade to a supported Python version (e.g., Python 3.6+).","message":"Support for Python 2.6, 3.2, and 3.3 was officially dropped in version 1.59.","severity":"breaking","affected_versions":">=1.59"},{"fix":"Ensure all clients in your system are running compatible `python-memcached` versions. Implement explicit encoding/decoding if encountering `bytes` where `str` is expected, or consider a custom serializer/deserializer.","message":"Version 1.59 introduced changes to how FLAGS are set, which can break compatibility with older `python-memcached` clients, particularly regarding the handling of strings versus bytes. Keys set by v1.58 as strings might be returned as bytes on v1.59, requiring explicit decoding.","severity":"breaking","affected_versions":">=1.59"},{"fix":"Avoid passing the `time` argument to `delete()`. Memcached `delete` operations are immediate and do not support an expiration time.","message":"The `time` argument for the `delete()` method was removed in version 1.58 when not explicitly set, as it is deprecated in the memcached server itself. While `python-memcached` likely handles this gracefully, relying on it is discouraged.","severity":"deprecated","affected_versions":">=1.58"},{"fix":"Evaluate `pymemcache` for new development. For existing projects, be aware that active feature development for `python-memcached` is minimal.","message":"The `python-memcached` library is in maintenance mode, with `pymemcache` positioned as a more actively developed and feature-rich alternative. New projects or those requiring active enhancements might consider `pymemcache` instead.","severity":"gotcha","affected_versions":"All"},{"fix":"Design your caching strategy to store smaller, frequently accessed data. For larger objects, consider sharding the data or using an alternative storage solution.","message":"Memcached itself has limits on key and value sizes: keys can be at most 250 bytes, and values typically have a maximum size of 1MB. Attempting to store larger items will fail.","severity":"gotcha","affected_versions":"All"},{"fix":"For security-sensitive applications, ensure that only trusted data is unpickled. For better interoperability or performance, explicitly serialize/deserialize complex objects using formats like JSON before passing them to `set()` and after receiving them from `get()`.","message":"By default, `python-memcached` uses Python's `pickle` module to serialize and deserialize complex Python objects (like lists, dictionaries, or custom classes) when storing them. This can have security implications if untrusted data is deserialized, and may also be slower or less efficient than custom serialization (e.g., JSON for simple data structures).","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}