{"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":"en","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)"}],"env_vars":null,"last_verified":"2026-04-05T00:00:00.000Z","next_check":"2026-07-04T00:00:00.000Z"}