{"id":1158,"library":"cachelib","title":"cachelib","description":"Cachelib is a Python library that provides a collection of cache implementations sharing a common API interface, originally extracted from the Werkzeug project. It offers various backends like in-memory, file system, Redis, Memcached, DynamoDB, and MongoDB. The library is actively maintained by the Pallets organization, with several minor and patch releases occurring throughout the year.","status":"active","version":"0.13.0","language":"python","source_language":"en","source_url":"https://github.com/pallets-eco/cachelib/","tags":["cache","caching","in-memory","file-system","redis","memcached","pallets"],"install":[{"cmd":"pip install cachelib","lang":"bash","label":"Install core library"},{"cmd":"pip install cachelib[redis]","lang":"bash","label":"Install with Redis support"},{"cmd":"pip install cachelib[memcached]","lang":"bash","label":"Install with Memcached support"},{"cmd":"pip install cachelib[dynamodb]","lang":"bash","label":"Install with DynamoDB support"},{"cmd":"pip install cachelib[mongodb]","lang":"bash","label":"Install with MongoDB support"}],"dependencies":[{"reason":"Required for RedisCache backend.","package":"redis","optional":true},{"reason":"Required for MemcachedCache backend. Alternative: pylibmc.","package":"python-memcached","optional":true},{"reason":"Required for DynamoDbCache backend.","package":"boto3","optional":true},{"reason":"Required for MongoDbCache backend.","package":"pymongo","optional":true}],"imports":[{"symbol":"SimpleCache","correct":"from cachelib import SimpleCache"},{"symbol":"FileSystemCache","correct":"from cachelib import FileSystemCache"},{"symbol":"RedisCache","correct":"from cachelib import RedisCache"}],"quickstart":{"code":"from cachelib import SimpleCache\n\n# Initialize an in-memory cache\ncache = SimpleCache(threshold=500, default_timeout=300)\n\n# Set a value\ncache.set(\"my_key\", \"my_value\", timeout=60)\n\n# Get a value\nvalue = cache.get(\"my_key\")\nprint(f\"Retrieved: {value}\")\n\n# Check if a key exists\nexists = cache.has(\"my_key\")\nprint(f\"Key 'my_key' exists: {exists}\")\n\n# Delete a key\ncache.delete(\"my_key\")\nexists_after_delete = cache.has(\"my_key\")\nprint(f\"Key 'my_key' exists after delete: {exists_after_delete}\")\n\n# Example with FileSystemCache (requires a directory)\nimport os\nimport tempfile\n\nwith tempfile.TemporaryDirectory() as temp_dir:\n    file_cache = FileSystemCache(cache_dir=os.path.join(temp_dir, \"cache\"), threshold=100)\n    file_cache.set(\"file_key\", \"file_value\", timeout=300)\n    file_value = file_cache.get(\"file_key\")\n    print(f\"Retrieved from file cache: {file_value}\")\n    file_cache.clear() # Clean up temporary cache dir","lang":"python","description":"This quickstart demonstrates how to initialize a `SimpleCache` (in-memory) and a `FileSystemCache` (file-based) to store, retrieve, check for existence, and delete cached values. It highlights basic `set`, `get`, `has`, and `delete` operations. For `FileSystemCache`, a temporary directory is used for demonstration purposes."},"warnings":[{"fix":"Upgrade to Python 3.8 or newer, or pin cachelib to a compatible older version if Python 3.7 or 3.6 is strictly required.","message":"Python 3.7 support was dropped in Cachelib 0.11.0. Older versions (0.7.0) dropped Python 3.6 support. Ensure your Python environment is 3.8+.","severity":"breaking","affected_versions":">=0.11.0 (for Python 3.7), >=0.7.0 (for Python 3.6)"},{"fix":"Avoid direct reliance on internal serialization methods. The library handles serialization internally using `RedisSerializer`.","message":"The methods `RedisCache.load_object` and `RedisCache.dump_object` were removed in Cachelib 0.8.0. These were internal methods and their direct use would have caused breaking changes.","severity":"deprecated","affected_versions":">=0.8.0"},{"fix":"Dedicate a unique directory for each `FileSystemCache` instance and ensure no other process or manual operation interacts with its contents.","message":"The `FileSystemCache` requires exclusive use of its `cache_dir` parameter. If other processes or manual interventions modify files within this directory, the cache may behave unexpectedly (e.g., deleting unintended files).","severity":"gotcha","affected_versions":"All versions"},{"fix":"If `hashlib.md5` is unavailable or restricted in your environment, configure `FileSystemCache` with a custom `hash_method` during initialization. The library will lazy-load `hashlib.md5` if no alternative is provided.","message":"In Cachelib 0.13.0, the `hashlib.md5` default for `FileSystemCache` was changed to be lazy-loaded. This was to prevent issues in FIPS-compliant environments where MD5 might not be available at import time. If you relied on `hashlib.md5` being available immediately at import, or if you need to specify a different hash method for FIPS compliance, consider this change.","severity":"gotcha","affected_versions":">=0.13.0"},{"fix":"Always pass a string (e.g., 'localhost') or a `redis.Redis` client instance to the `host` parameter of `RedisCache`.","message":"In `RedisCache` version 0.12.0, a fix was implemented to correctly handle a string for the `host` parameter, where previously passing a non-string 'host object' might have caused issues.","severity":"gotcha","affected_versions":">=0.12.0 (fix applied)"},{"fix":"Ensure that all `cachelib` classes are properly imported (e.g., `from cachelib import FileSystemCache`) before they are used in your code.","message":"Specific cache classes (e.g., `FileSystemCache`, `RedisCache`, `SimpleCache`) must be explicitly imported from the `cachelib` module before use. Attempting to use them without an explicit import will result in a `NameError`.","severity":"breaking","affected_versions":"All versions"},{"fix":"Install backend drivers using their dedicated package names (e.g., `pip install cachelib-redis`, `pip install cachelib-memcached`) instead of using extras on the main `cachelib` package.","message":"Cachelib 0.13.0 removed all package extras (e.g., `[redis]`, `[memcached]`, `[dynamodb]`, `[mongodb]`). Drivers for external backends are now provided as separate packages (e.g., `cachelib-redis`, `cachelib-memcached`). Users must install these backend-specific packages explicitly.","severity":"breaking","affected_versions":">=0.13.0"}],"env_vars":null,"search_vec":"'activ':43 'api':17 'backend':28 'cach':12,59,60 'cachelib':1,2 'collect':10 'common':16 'dynamodb':37 'extract':20 'file':33,65 'file-system':64 'implement':13 'in-memori':30,61 'interfac':18 'librari':6,41 'like':29 'maintain':44 'memcach':36,68 'memori':32,63 'minor':51 'mongodb':39 'occur':55 'offer':26 'organ':48 'origin':19 'pallet':47,69 'patch':53 'project':24 'provid':8 'python':5 'redi':35,67 'releas':54 'sever':50 'share':14 'system':34,66 'throughout':56 'various':27 'werkzeug':23 'year':58","created_at":"2026-04-05T14:30:36.907787+00:00","updated_at":"2026-04-16T01:14:43.294755+00:00","problems":[{"fix":"Install the cachelib library using pip: `pip install cachelib`","cause":"The 'cachelib' library is not installed in your Python environment or the environment where your application is running.","error":"ModuleNotFoundError: No module named 'cachelib'"},{"fix":"Replace `from werkzeug.contrib.cache import ...` with `from cachelib import ...` (or a specific cache backend like `from cachelib.redis import RedisCache`) and ensure `cachelib` is installed.","cause":"The `werkzeug.contrib.cache` module was deprecated in Werkzeug 1.0 and extracted into the separate `cachelib` library.","error":"ModuleNotFoundError: No module named 'werkzeug.contrib.cache'"},{"fix":"Upgrade your Python environment to Python 3.3 or newer. If Python 2.x compatibility is strictly required, you might need to pin an older version of cachelib that doesn't use `os.replace` or implement a fallback, though upgrading Python is strongly recommended.","cause":"This error occurs when using cachelib with Python 2.x because cachelib utilizes `os.replace()`, which was introduced in Python 3.3 and is not available in Python 2.","error":"AttributeError: 'module' object has no attribute 'replace'"},{"fix":"Ensure that only one process is accessing a given persistent cache path at a time. This may involve identifying and terminating zombie processes, or configuring separate cache paths for different application instances.","cause":"This error typically occurs when using cachelib's persistent cache with multiple processes attempting to operate on the same cache path simultaneously, as cachelib locks the path to ensure single-instance operation.","error":"Can not lock shm metadata file: Device or resource busy"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.17.0","cli_name":"","cli_version":null,"type":"library","homepage":"https://cachelib.readthedocs.io/","github":"https://github.com/pallets-eco/cachelib","docs":"https://cachelib.readthedocs.io","changelog":"https://cachelib.readthedocs.io/page/changes/","pypi":"https://pypi.org/project/cachelib/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}