{
  "schema_version": 2,
  "id": "integrate/flask",
  "title": "Redis with Flask",
  "url": "https://redis.io/docs/latest/integrate/flask/",
  "summary": "Use Redis as the cache and session store for a Flask application.",
  "tags": [
    "docs",
    "integrate",
    "oss",
    "rs",
    "rc"
  ],
  "last_updated": "2026-11-17T15:08:14Z",
  "children": [],
  "page_type": "content",
  "content_hash": "1cf678d12f63b00c80e8fc431a42e2abb5a509c0fa6fed21e0243913c2a4cda6",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "[Flask](https://flask.palletsprojects.com/) is a lightweight Python web framework. Two\nFlask extensions use Redis: [Flask-Caching](https://flask-caching.readthedocs.io/)\nfor [caching](#cache-views-and-functions), and\n[Flask-Session](https://flask-session.readthedocs.io/) for\n[server-side sessions](#store-sessions). Both use the\n[redis-py](https://redis.io/docs/latest/develop/clients/redis-py) client."
    },
    {
      "id": "requirements",
      "title": "Requirements",
      "role": "content",
      "text": "The examples on this page were run with Flask 3.1, Flask-Caching 2.5, Flask-Session 0.8,\nand redis-py against Redis 7.2.\n\nInstall the extensions:\n\n[code example]\n\nYou also need a running Redis server. You can run one locally with\n[Redis Open Source](https://redis.io/docs/latest/operate/oss_and_stack/install/archive/install-redis),\nuse [Docker](https://hub.docker.com/_/redis), or connect to\n[Redis Cloud](https://redis.io/cloud/)."
    },
    {
      "id": "cache-views-and-functions",
      "title": "Cache views and functions",
      "role": "content",
      "text": "Configure Flask-Caching to use Redis, then create the `Cache` object:\n\n[code example]\n\nUse `@cache.cached` to cache the whole response of a view, and `@cache.memoize` to\ncache the result of a function for each set of arguments. This is the\n[cache-aside](https://redis.io/docs/latest/develop/use-cases/cache-aside) pattern:\n\n[code example]\n\nFlask-Caching stores a cached view under a key such as `myapp:view//book/1`, with a TTL\nequal to the `timeout`. To drop a memoized result, call `cache.delete_memoized(find_book, \"2\")`,\nand the next call runs the function again. You can also use `cache.get()` and `cache.set()`\ninside an application context."
    },
    {
      "id": "store-sessions",
      "title": "Store sessions",
      "role": "content",
      "text": "Flask-Session replaces Flask's default cookie-based session with a server-side session\nstored in Redis. The browser keeps only a session ID. Pass Flask-Session a redis-py\nclient and set the session lifetime:\n\n[code example]\n\nTwo requests that share a session cookie return `1` and then `2`. Flask-Session stores\nthe data under `myapp:session:<session-id>` with a TTL equal to the lifetime (900\nseconds in this example).\n\nSee the [session store use case](https://redis.io/docs/latest/develop/use-cases/session-store)\nfor more about this pattern."
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "- [Flask-Caching documentation](https://flask-caching.readthedocs.io/)\n- [Flask-Session documentation](https://flask-session.readthedocs.io/)\n- [Redis OM Python with Flask](https://redis.io/docs/latest/integrate/redisom-for-python) shows how to build a document API with Flask."
    }
  ],
  "examples": [
    {
      "id": "requirements-ex0",
      "language": "bash",
      "code": "pip install flask flask-caching flask-session redis",
      "section_id": "requirements"
    },
    {
      "id": "cache-views-and-functions-ex0",
      "language": "python",
      "code": "from flask import Flask\nfrom flask_caching import Cache\n\napp = Flask(__name__)\napp.config.update(\n    CACHE_TYPE=\"RedisCache\",\n    CACHE_REDIS_URL=\"redis://<your-redis-host>:6379/0\",\n    CACHE_DEFAULT_TIMEOUT=300,\n    CACHE_KEY_PREFIX=\"myapp:\",\n)\ncache = Cache(app)",
      "section_id": "cache-views-and-functions"
    },
    {
      "id": "cache-views-and-functions-ex1",
      "language": "python",
      "code": "@app.route(\"/book/<isbn>\")\n@cache.cached(timeout=60)\ndef book_view(isbn):\n    return render_book(isbn)  # runs only on a cache miss\n\n@cache.memoize(timeout=30)\ndef find_book(isbn):\n    return load_book_from_database(isbn)",
      "section_id": "cache-views-and-functions"
    },
    {
      "id": "store-sessions-ex0",
      "language": "python",
      "code": "from datetime import timedelta\n\nimport redis\nfrom flask import session\nfrom flask_session import Session\n\napp.config.update(\n    SECRET_KEY=\"<your-secret-key>\",\n    SESSION_TYPE=\"redis\",\n    SESSION_REDIS=redis.from_url(\"redis://<your-redis-host>:6379/1\"),\n    SESSION_KEY_PREFIX=\"myapp:session:\",\n    SESSION_PERMANENT=True,\n    PERMANENT_SESSION_LIFETIME=timedelta(minutes=15),\n)\nSession(app)\n\n@app.route(\"/hit\")\ndef hit():\n    session[\"count\"] = session.get(\"count\", 0) + 1\n    return str(session[\"count\"])",
      "section_id": "store-sessions"
    }
  ]
}
