{"id":1481,"library":"flask-caching","title":"Flask-Caching","description":"Flask-Caching adds comprehensive caching support to Flask applications, providing various backend options such as SimpleCache, RedisCache, MemcachedCache, FileSystemCache, and others. It integrates seamlessly with Flask applications to cache view functions or parts of templates. The current version is 2.3.1, and the library maintains an active release cadence with regular updates and patches.","status":"active","version":"2.3.1","language":"en","source_language":"en","source_url":"https://github.com/pallets-eco/flask-caching","tags":["flask","caching","web development","backend","performance"],"install":[{"cmd":"pip install Flask-Caching","lang":"bash","label":"Base installation"},{"cmd":"pip install Flask-Caching[redis]","lang":"bash","label":"With Redis backend"},{"cmd":"pip install Flask-Caching[memcached]","lang":"bash","label":"With Memcached backend"}],"dependencies":[{"reason":"Core web framework integration.","package":"Flask","optional":false},{"reason":"Required for RedisCache backend.","package":"redis","optional":true},{"reason":"Required for MemcachedCache backend (pylibmc is an alternative).","package":"python-memcached","optional":true}],"imports":[{"symbol":"Cache","correct":"from flask_caching import Cache"}],"quickstart":{"code":"from flask import Flask, request\nfrom flask_caching import Cache\nimport datetime\n\napp = Flask(__name__)\n\n# Example Configuration (using SimpleCache, good for development)\n# For production, consider 'RedisCache', 'MemcachedCache', etc.\napp.config.from_mapping({\n    \"CACHE_TYPE\": \"SimpleCache\", # Type of cache to use (e.g., SimpleCache, RedisCache, MemcachedCache)\n    \"CACHE_DEFAULT_TIMEOUT\": 300 # Default timeout in seconds for cached items\n    # For RedisCache: \"CACHE_REDIS_HOST\": \"localhost\", \"CACHE_REDIS_PORT\": 6379\n})\n\ncache = Cache(app)\n\n@app.route(\"/\")\n@cache.cached(timeout=60) # Cache this view for 60 seconds\ndef index():\n    # This part will only execute once every 60 seconds\n    current_time = datetime.datetime.now().strftime(\"%H:%M:%S\")\n    return f\"<h1>Hello, Flask-Caching!</h1><p>The time is: {current_time}</p><p>This page is cached.</p>\"\n\n@app.route(\"/reset_cache\")\ndef reset_cache():\n    cache.clear()\n    return \"Cache cleared!\"\n\n# Example with dynamic URL and custom cache key\n@app.route(\"/user/<username>\")\n@cache.cached(timeout=30, make_cache_key=lambda: request.path)\ndef user_profile(username):\n    # Simulate a database lookup\n    import time\n    time.sleep(2) \n    return f\"<h2>Profile for {username}</h2><p>Data fetched at {datetime.datetime.now().strftime(\"%H:%M:%S\")}</p>\"\n\nif __name__ == '__main__':\n    app.run(debug=True)\n","lang":"python","description":"This quickstart demonstrates setting up Flask-Caching with a `SimpleCache` backend, caching a simple view function (`/`), and using a custom `make_cache_key` for a dynamic route (`/user/<username>`). It also includes an endpoint to clear the cache. For external backends like Redis or Memcached, ensure the respective `CACHE_TYPE` and configuration variables are set in `app.config` and the necessary dependencies are installed (e.g., `pip install Flask-Caching[redis]` and `pip install redis`)."},"warnings":[{"fix":"Upgrade Flask to 2.0+ (e.g., `pip install Flask>=2.0`) or keep Flask-Caching at v1.x or earlier (e.g., `pip install Flask-Caching==1.11.1`).","message":"Flask-Caching v2.0.0 and newer versions require Flask 2.0 or higher. Applications running older Flask versions (e.g., Flask 1.x) must either upgrade Flask or stick to Flask-Caching v1.x to maintain compatibility.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Update import statements. For example, change `from flask_caching.ext.redis import RedisCache` to `from flask_caching.backends.rediscache import RedisCache`. Consult the documentation for specific backend import paths.","message":"Starting with v2.0.0, the import paths for many cache backends have changed. Previously, backends like `RedisCache` were often imported from `flask_caching.ext.redis`. The `ext` module has been removed, and all backends are now located under `flask_caching.backends.<backend_name_module>`. This is a significant change requiring update of import statements.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"Migrate to an officially supported caching backend such as RedisCache, MemcachedCache, or FileSystemCache. If migration is not feasible, pin Flask-Caching to v1.9.x or older.","message":"The `uwsgicache` backend was deprecated in Flask-Caching v1.10.0 and completely removed in v2.0.0. If your application relies on this backend, it will break when upgrading to v2.0.0 or later.","severity":"breaking","affected_versions":"1.10.0+ (deprecated), 2.0.0+ (removed)"},{"fix":"Use the `make_cache_key` argument with the `@cache.cached` decorator. Provide a function that generates a unique key based on relevant request attributes, such as `request.path`, `request.args`, or a combination of them. Example: `@cache.cached(timeout=30, make_cache_key=lambda: request.path)`.","message":"When caching dynamic routes (e.g., `/user/<int:user_id>`), the default cache key generation might not always produce unique keys for different parameters, potentially leading to stale or incorrect data being served. It's crucial to explicitly define a custom `make_cache_key`.","severity":"gotcha","affected_versions":"all"},{"fix":"Carefully review your `app.config` settings for Flask-Caching. Verify that `CACHE_TYPE` corresponds to the desired backend, that the necessary Python packages are installed (e.g., `redis` for RedisCache), and that all required backend-specific configuration variables are correctly set according to the Flask-Caching documentation.","message":"Incorrectly configuring the `CACHE_TYPE` or its associated parameters (e.g., `CACHE_REDIS_HOST`, `CACHE_MEMCACHED_SERVERS`) is a common source of errors. Ensure the chosen backend library is installed and its configuration matches the chosen `CACHE_TYPE`.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}