Skip to content

Commit

Permalink
added cache
Browse files Browse the repository at this point in the history
  • Loading branch information
philippspinnler committed Sep 18, 2024
1 parent 2c083e3 commit 753f554
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
21 changes: 20 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import asynccontextmanager
from typing import AsyncIterator
from fastapi import FastAPI, File, Form, Query, UploadFile
from fastapi.responses import HTMLResponse
from app.plugins.ical import get_events
Expand All @@ -8,8 +10,18 @@
from app.plugins.weather import get_data as get_data_weather
from app.plugins.publictransportation import get_data as get_data_publictransportation
from fastapi.staticfiles import StaticFiles
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache

app = FastAPI()

@asynccontextmanager
async def lifespan(_: FastAPI) -> AsyncIterator[None]:
FastAPICache.init(InMemoryBackend())
yield


app = FastAPI(lifespan=lifespan)


@app.get("/")
Expand All @@ -18,21 +30,25 @@ async def root():


@app.get("/calendar")
@cache(expire=600)
async def calendar():
return get_events()


@app.get("/netatmo")
@cache(expire=1800)
async def netatmo():
return get_data_netatmo()


@app.get("/sonos")
@cache(expire=60)
async def sonos():
return get_data_sonos()


@app.get("/speedtest")
@cache(expire=1800)
async def speedtest():
return get_data_speedtest()

Expand All @@ -41,6 +57,7 @@ async def speedtest():


@app.get("/album")
@cache(expire=1800)
async def album():
return get_data()

Expand All @@ -61,12 +78,14 @@ async def delete_file(filename: str = Form(...)):


@app.get("/weather")
@cache(expire=3600)
async def get_weather(
lat: float = Query(47.4176969, description="Latitude"), lon: float = Query(7.7612123, description="Longitude")
):
return get_data_weather(lat=lat, lon=lon)


@app.get("/public-transportation")
@cache(expire=300)
async def get_departures(connections: str = '[["Hölstein, Süd", "Liestal, Bahnhof", "direct"]]'):
return get_data_publictransportation(connections)
135 changes: 134 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ icalendar = "^5.0.13"
soco = "^0.30.4"
fastapi = {extras = ["standard"], version = "^0.114.2"}
pyatmo = "^8.1.0"
fastapi-cache2 = "^0.2.2"


[build-system]
Expand Down

0 comments on commit 753f554

Please sign in to comment.