feat: add redis cache

This commit is contained in:
2024-08-22 08:45:30 +03:00
parent 0638fb8d50
commit d3ef03a6a0
19 changed files with 194 additions and 24 deletions
+40
View File
@@ -0,0 +1,40 @@
import datetime
from aiocache import cached
from gallery.sketch.cached import CachedApi
from .api import WeatherApi
from .model import WeatherResponse
class CachedWeatherApi(WeatherApi, CachedApi[WeatherApi]):
CACHE_KEY = "weather"
@cached(
key_builder=lambda fun, self: f"api.{self.CACHE_KEY}.{self.provider}.locations",
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
)
async def get_locations(self) -> list[str]:
return await self._api.get_locations()
@cached(
key_builder=lambda fun, self, location_id, date: (
f"api.{self.CACHE_KEY}.{self.provider}.day.{location_id}.{date}"
),
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
)
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
return await self._api.get_day(location_id, date)
@cached(
key_builder=lambda fun, self, location_id, date: (
f"api.{self.CACHE_KEY}.{self.provider}.day.{location_id}.{date}"
),
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
)
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
return await self._api.get_days(location_id, days)