-
-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: adjust response cache * docs: move code into examples --------- Co-authored-by: Jacob Coffee <[email protected]>
- Loading branch information
1 parent
f83c26f
commit 5acf84b
Showing
6 changed files
with
80 additions
and
54 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from litestar import Litestar, get | ||
from litestar.config.response_cache import CACHE_FOREVER | ||
|
||
|
||
@get("/cached", cache=True) | ||
async def my_cached_handler() -> str: | ||
return "cached" | ||
|
||
|
||
@get("/cached-seconds", cache=120) # seconds | ||
async def my_cached_handler_seconds() -> str: | ||
return "cached for 120 seconds" | ||
|
||
|
||
@get("/cached-forever", cache=CACHE_FOREVER) | ||
async def my_cached_handler_forever() -> str: | ||
return "cached forever" | ||
|
||
|
||
app = Litestar( | ||
[my_cached_handler, my_cached_handler_seconds, my_cached_handler_forever], | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from litestar import Litestar, Request | ||
from litestar.config.response_cache import ResponseCacheConfig | ||
|
||
|
||
def key_builder(request: Request) -> str: | ||
return request.url.path + request.headers.get("my-header", "") | ||
|
||
|
||
app = Litestar([], response_cache_config=ResponseCacheConfig(key_builder=key_builder)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from litestar import Litestar, Request, get | ||
|
||
|
||
def key_builder(request: Request) -> str: | ||
return request.url.path + request.headers.get("my-header", "") | ||
|
||
|
||
@get("/cached-path", cache=True, cache_key_builder=key_builder) | ||
async def cached_handler() -> str: | ||
return "cached" | ||
|
||
|
||
app = Litestar([cached_handler]) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import asyncio | ||
|
||
from litestar import Litestar, get | ||
from litestar.config.response_cache import ResponseCacheConfig | ||
from litestar.stores.redis import RedisStore | ||
|
||
|
||
@get(cache=10) | ||
async def something() -> str: | ||
await asyncio.sleep(1) | ||
return "something" | ||
|
||
|
||
redis_store = RedisStore.with_client(url="redis://localhost/", port=6379, db=0) | ||
cache_config = ResponseCacheConfig(store="redis_backed_store") | ||
app = Litestar( | ||
[something], | ||
stores={"redis_backed_store": redis_store}, | ||
response_cache_config=cache_config, | ||
) |
This file contains 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