{"id":50356510,"url":"https://github.com/redis/fastapi-redis-sdk","last_synced_at":"2026-05-29T23:00:37.802Z","repository":{"id":361110884,"uuid":"1246787859","full_name":"redis/fastapi-redis-sdk","owner":"redis","description":"FastAPI SDK for Redis","archived":false,"fork":false,"pushed_at":"2026-05-29T07:27:10.000Z","size":376,"stargazers_count":2,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-05-29T09:18:21.550Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-22T14:54:51.000Z","updated_at":"2026-05-29T07:25:00.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/redis/fastapi-redis-sdk","commit_stats":null,"previous_names":["redis/fastapi-redis-sdk"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/redis/fastapi-redis-sdk","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis%2Ffastapi-redis-sdk","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis%2Ffastapi-redis-sdk/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis%2Ffastapi-redis-sdk/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis%2Ffastapi-redis-sdk/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redis","download_url":"https://codeload.github.com/redis/fastapi-redis-sdk/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis%2Ffastapi-redis-sdk/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33673628,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-05-29T23:00:37.035Z","updated_at":"2026-05-29T23:00:37.794Z","avatar_url":"https://github.com/redis.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Official FastAPI integration for Redis\n\nIdiomatic Redis integration for FastAPI - connection management and DI-based caching with automatic key consistency.\n\n[![Integration](https://github.com/redis/fastapi-redis-sdk/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/redis/fastapi-redis-sdk/actions/workflows/ci.yml)\n[![PyPI - Version](https://img.shields.io/pypi/v/fastapi-redis-sdk)](https://pypi.org/project/fastapi-redis-sdk/)\n[![Python Version](https://img.shields.io/badge/python-3.10%2B-blue\u0026logo=redis)](https://www.python.org/downloads/)\n[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)\n[![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v0.json)](https://astral.sh/ruff)\n[![codecov](https://codecov.io/gh/redis/fastapi-redis-sdk/branch/main/graph/badge.svg?token=yenl5fzxxr)](https://codecov.io/gh/redis/fastapi-redis-sdk)\n\n\n[![Discord](https://img.shields.io/discord/697882427875393627.svg?style=social\u0026logo=discord)](https://discord.gg/redis)\n[![Twitch](https://img.shields.io/twitch/status/redisinc?style=social)](https://www.twitch.tv/redisinc)\n[![YouTube](https://img.shields.io/youtube/channel/views/UCD78lHSwYqMlyetR0_P4Vig?style=social)](https://www.youtube.com/redisinc)\n[![Twitter](https://img.shields.io/twitter/follow/redisinc?style=social)](https://twitter.com/redisinc)\n[![Stack Exchange questions](https://img.shields.io/stackexchange/stackoverflow/t/fastapi-redis-sdk?style=social\u0026logo=stackoverflow\u0026label=Stackoverflow)](https://stackoverflow.com/questions/tagged/fastapi-redis-sdk)\n\n### Features\n\n- **Fluent setup** - `FastAPIRedis(app).lifespan().caching()` configures pools and caching in one chain\n- **DI-based caching** - `cache()`, `cache_evict()`, `cache_put()` as `Depends()` factories, plus `CacheBackend` for complex invalidation and conditional logic\n- **HTTP-native caching** - ETag, 304 Not Modified, `Cache-Control` directives out of the box\n- **Testable** - full `dependency_overrides` support for caching; no monkey-patching\n- **Production-ready** - TLS, auth, OSS Cluster mode, configurable via environment variables or `.env`\n\n### Requirements\n\n| Dependency   | Supported versions |\n|--------------|--------------------|\n| Python       | 3.10 to 3.14       |\n| FastAPI      | 0.115+             |\n| redis-py     | 6.0+               |\n| Pydantic     | 2.0+               |\n| Redis server | 7.4+               |\n\n## Installation\n\n```bash\npip install fastapi-redis-sdk\n```\n\n## Quick Start\n\n```python\nfrom fastapi import FastAPI\nfrom redis_fastapi import FastAPIRedis, RedisDep, AsyncRedisDep\n\napp = FastAPI()\nFastAPIRedis(app).lifespan()\n\n@app.get(\"/items\")\ndef get_items(redis: RedisDep):\n    return {\"items\": redis.get(\"items\")}\n\n@app.get(\"/async-items\")\nasync def get_items_async(redis: AsyncRedisDep):\n    return {\"items\": await redis.get(\"items\")}\n```\n\nConnection pools are managed automatically and closed on shutdown. The builder wraps any existing lifespan - multiple libraries can each register their own without conflicting. See [Configuration](#configuration) for connection options.\n\n## Caching\n\nTwo caching patterns for different needs, sharing the same connection pool:\n\n| Pattern                                   | Best for                                |\n|-------------------------------------------|-----------------------------------------|\n| `cache()`, `cache_evict()`, `cache_put()` | Most endpoints - read/write/invalidate  |\n| **CacheBackend**                          | Complex invalidation, conditional logic |\n\n### DI factories - read, invalidate, write-through\n\n```python\nfrom fastapi import Depends\nfrom redis_fastapi import FastAPIRedis, cache, cache_evict, cache_put, default_key_builder\n\napp = FastAPI()\nFastAPIRedis(app).lifespan().caching()\n\n\n# READ - cache GET responses\n@app.get(\"/products/{product_id}\", dependencies=[Depends(cache(ttl=300, eviction_group=\"products\"))])\nasync def get_product(product_id: int):\n    return await db.get_product(product_id)\n\n\n# INVALIDATE - evict the cached entry on delete\n@app.delete(\n    \"/products/{product_id}\",\n    dependencies=[Depends(cache_evict(eviction_group=\"products\", key_builder=default_key_builder))],\n)\nasync def delete_product(product_id: int):\n    await db.delete(product_id)\n\n\n# WRITE-THROUGH - update the cache so the next GET is a HIT\n@app.put(\n    \"/products/{product_id}\",\n    dependencies=[Depends(cache_put(eviction_group=\"products\", key_builder=default_key_builder, ttl=300))],\n)\nasync def replace_product(product_id: int, body: Product):\n    return await db.update(product_id, body)\n```\n\nAll three factories share the same `key_builder`, so GET, DELETE, and PUT on the same path target the exact same cache key. Responses include `X-Redis-Cache` (HIT/MISS), `Cache-Control`, and `ETag` headers with 304 Not Modified support. Full `dependency_overrides` support makes testing straightforward - no monkey-patching required.\n\n### CacheBackend - full control via dependency injection\n\n```python\nfrom redis_fastapi import CacheBackendDep\n\n@app.get(\"/dashboard/{user_id}\")\nasync def dashboard(user_id: int, cache: CacheBackendDep):\n    cached = await cache.get(f\"stats:{user_id}\", eviction_group=\"dashboard\")\n    if cached:\n        return cached\n    result = await compute_dashboard(user_id)\n    await cache.set(f\"stats:{user_id}\", result, ttl=300, eviction_group=\"dashboard\")\n    return result\n```\n\nProvides `get`/`set`/`delete`/`has`/`delete_group` with automatic JSON serialization. Use it for conditional caching, cascade invalidation, dynamic TTL, and intermediate result caching.\n\nSee the [Caching Guide](docs/guide/caching.md) for detailed examples, feature comparison, and best practices.\n\n## Configuration\n\nAll settings are read from environment variables (prefixed `REDIS_`) or a `.env` file. Set `REDIS_URL` for the simplest setup:\n\n```bash\nexport REDIS_URL=redis://user:pass@host:6379/0\n```\n\nOr configure individual fields:\n\n```bash\nexport REDIS_HOST=redis.example.com\nexport REDIS_PORT=6380\nexport REDIS_PASSWORD=secret\n```\n\nAdditional options: TLS (`REDIS_SSL`, `REDIS_SSL_CERTFILE`, etc.), connection pool (`REDIS_MAX_CONNECTIONS`, `REDIS_SOCKET_TIMEOUT`), OSS Cluster mode (`REDIS_CLUSTER=true`), key prefix (`REDIS_PREFIX`), and default cache TTL (`REDIS_DEFAULT_TTL`, default `0` = no expiry).\n\nFor programmatic configuration:\n\n```python\nfrom redis_fastapi import get_settings\n\nsettings = get_settings()\nsettings.url = \"redis://custom:6379/0\"\nsettings.default_ttl = 120\n```\n\nSee the [Configuration Guide](docs/guide/configuration.md) for the full environment variable reference and API details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis%2Ffastapi-redis-sdk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredis%2Ffastapi-redis-sdk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis%2Ffastapi-redis-sdk/lists"}