| 1 |
import time |
| 2 |
|
| 3 |
from litestar import Controller, Litestar, get |
| 4 |
from litestar.datastructures import CacheControlHeader |
| 5 |
|
| 6 |
|
| 7 |
class MyController(Controller): |
| 8 |
cache_control = CacheControlHeader(max_age=86_400, public=True) |
| 9 |
|
| 10 |
@get("/chance_of_rain", sync_to_thread=False) |
| 11 |
def get_chance_of_rain(self) -> float: |
| 12 |
"""This endpoint uses the cache control value defined in the controller which overrides the app value.""" |
| 13 |
return 0.5 |
| 14 |
|
| 15 |
@get("/timestamp", cache_control=CacheControlHeader(no_store=True), sync_to_thread=False) |
| 16 |
def get_server_time(self) -> float: |
| 17 |
"""This endpoint overrides the cache control value defined in the controller.""" |
| 18 |
return time.time() |
| 19 |
|
| 20 |
|
| 21 |
@get("/population", sync_to_thread=False) |
| 22 |
def get_population_count() -> int: |
| 23 |
"""This endpoint will use the cache control defined in the app.""" |
| 24 |
return 100000 |
| 25 |
|
| 26 |
|
| 27 |
app = Litestar( |
| 28 |
route_handlers=[MyController, get_population_count], |
| 29 |
cache_control=CacheControlHeader(max_age=2_628_288, public=True), |
| 30 |
) |