Repository navigation
docs: add runnable examples - #291
Merged
Merged
Conversation
One self-contained FastAPI app per feature under examples/: HTTP caching, CacheManager, cookie sessions with login and logout, JWT sessions, OAuth state, CacheLock, a fixed-window rate limiter and a Redis backend. tests/test_examples.py loads each one from its file and drives its main flow through TestClient, with DeprecationWarning as an error. The JWT and Redis examples are skipped without their packages; the Redis one also follows the live-server opt-in.
This was referenced Sep 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
examples/: one self-contained FastAPI app per feature, each driven through its main flow by the test suite, and links them from the README, the docs index and the guide pages (EN and zh-TW).Files
examples/http_cache.py@cachewith TTL, ETag /304,no_cache,private,clear_path()after an update, monitoring routes behind an admin-token dependencyexamples/app_cache.pyCacheManager.get_or_set(),add()as an idempotency check, theAppCachedependencyexamples/session_login.pyFastAPICacheXSessionMiddlewarewith cookies: anonymous session, login withrotate_session_id(),AuthenticatedSession, logout viarequest.session.clear()examples/session_jwt.pytoken_format="jwt"sessions overAuthorization: Bearer, revoked on logoutjwtextraexamples/oauth_state.pyStateManagerwithbindingexamples/cache_lock.pyCacheLock, blocking (async with) and non-blockingexamples/rate_limit.pybackend.increment(),429+Retry-Afterexamples/redis_backend.pyAsyncRedisCacheBackendfrom env vars in the lifespan, closed on shutdownredisextra, a serverexamples/README.mdtests/test_examples.pyTestClientpyproject.tomlexamples/**(INP001,S106fortoken_format="jwt")README.md,i18n/zh-TW/docs/index.mdHTTP_CACHING,APP_CACHE,SESSION,STATE,LOCK,BACKENDS(Redis section, atomic primitives)examples/…"The examples use the public API only and
MemoryBackendby default. Secrets come from environment variables (SESSION_SECRET_KEY,CACHE_ADMIN_TOKEN); the session fallback is an obvious development placeholder long enough for HS256, and the monitoring routes stay closed while no admin token is set.uv run fastapi devneedsfastapi-cli, which is not a dependency here, so the README documentsuv run --with "fastapi-cli[standard]" fastapi dev examples/<file>.pyand the Uvicorn formuv run --with uvicorn uvicorn examples.<mod>:app. Both were started locally and served requests.Tests
tests/test_examples.py: 12 tests plus the Redis one.DeprecationWarningis an error.session_jwtandredis_backendare skipped viaimportlib.util.find_specwhen their packages are missing;redis_backendalso runs only withCACHEX_TEST_REDIS_PORTset (it usesredis_skip_reason(), soCACHEX_REQUIRE_LIVE_SERVERS=1applies as for the other live suites). All four proxies are reset around each test, and each example is loaded as a fresh module.examples/*.pyhas a test and a row inexamples/README.md.tox -e lowestontests/test_examples.py: 12 passed, 1 skipped.uv run mypy examples --strict, ruff, pre-commit (its mypy hook already coversexamples/), and both strict docs builds are clean.Mutation checks, each applied to one example and run against its tests (all 11 killed):
@cache(ttl=60)from the product routeclear_path()after the updateget_or_set()add()→set()for the idempotency keyrotate_session_id()at loginpop()instead ofclear()CacheLockwith a null contextblocking=TruePackaging
uv build: the wheel contains onlyfastapi_cachex/and its dist-info, and the sdist containsLICENSE,PKG-INFO,README.md,pyproject.toml(plus thepyproject.toml.origthat uv_build writes) andfastapi_cachex/.examples/is in neither, so packaging is unchanged.CHANGELOG
Added
examples/holds one complete FastAPI app perfeature: HTTP caching,
CacheManager, cookie and JWT sessions, OAuthstate,
CacheLock, a rate limiter onincrement()and a Redis backend.tests/test_examples.pyruns each one's main flow, so they keep workingas the library changes. The guide pages link to the matching example.
Part of #241. Next step: #289 (include the example files in the docs as snippets).