feat: add redis cache
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user