Skip to content

Commit

Permalink
Add user-id to fastapi ex.
Browse files Browse the repository at this point in the history
  • Loading branch information
janbjorge committed Feb 26, 2024
1 parent e7f9d74 commit 614e0b5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ APP = FastAPI(lifespan=app_setup_teardown)
predicate=lambda x: x.operation == "update",
)
)
async def cached_query() -> dict[str, str]:
async def cached_query(user_id: int) -> dict[str, str]:
"""
Simulates a database query that benefits from cache invalidation.
Expand All @@ -59,11 +59,11 @@ async def cached_query() -> dict[str, str]:

# Define a FastAPI route to fetch data, utilizing the cached_query function.
@APP.get("/data")
async def get_data() -> dict:
async def get_data(user_id: int) -> dict:
"""
This endpoint uses the cached_query function to return data, demonstrating
how cache invalidation can be integrated into a web application route.
"""
# Fetch and return the data using the cached query function.
return await cached_query()
return await cached_query(user_id)
```
6 changes: 3 additions & 3 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ async def app_setup_teardown(_: FastAPI) -> typing.AsyncGenerator[None, None]:
APP = FastAPI(lifespan=app_setup_teardown)

@decorators.cache(strategy=strategies.Greedy(listener=listener, predicate=lambda x: x.operation == "update"))
async def cached_query() -> dict[str, str]:
async def cached_query(user_id: int) -> dict[str, str]:
return {"data": "query result"}

@APP.get("/data")
async def get_data() -> dict:
return await cached_query()
async def get_data(user_id: int) -> dict:
return await cached_query(user_id)
```

0 comments on commit 614e0b5

Please sign in to comment.