feat: add redis cache

This commit is contained in:
2024-08-21 22:53:50 +03:00
parent 0638fb8d50
commit d3ef03a6a0
19 changed files with 194 additions and 24 deletions

View File

6
gallery/sketch/api.py Normal file
View File

@@ -0,0 +1,6 @@
class Api:
PROVIDER: str
@property
def provider(self) -> str:
return self.PROVIDER

20
gallery/sketch/cached.py Normal file
View File

@@ -0,0 +1,20 @@
from typing import Generic, TypeVar
from gallery.util import TimeUnit
from .api import Api
API = TypeVar("API", bound=Api)
class CachedApi(Api, Generic[API]):
CACHE_TTL: int = TimeUnit.HOUR
CACHE_ALIAS: str = "redis"
CACHE_KEY: str
def __init__(self, api: API):
self._api = api
@property
def provider(self) -> str:
return self._api.provider

View File

@@ -1,9 +1,10 @@
import datetime
from ..api import Api
from .model import Schedule
class ScheduleApi:
class ScheduleApi(Api):
async def get_channels(self) -> list[str]:
raise NotImplementedError

View File

@@ -0,0 +1,32 @@
import datetime
from aiocache import cached
from gallery.sketch.cached import CachedApi
from .api import ScheduleApi
from .model import Schedule
class CachedScheduleApi(ScheduleApi, CachedApi[ScheduleApi]):
CACHE_KEY = "schedule"
@cached(
key_builder=lambda fun, self: f"api.{self.CACHE_KEY}.{self.provider}.channels",
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
)
async def get_channels(self) -> list[str]:
return await self._api.get_channels()
@cached(
key_builder=lambda fun, self, channel_id, date: (
f"api.{self.CACHE_KEY}.{self.provider}.channel.{channel_id}.{date}"
),
alias=CachedApi.CACHE_ALIAS,
ttl=CachedApi.CACHE_TTL,
)
async def get_channel_schedule(
self, channel_id: str, date: datetime.date
) -> Schedule:
return await self._api.get_channel_schedule(channel_id, date)

View File

@@ -1,9 +1,11 @@
import datetime
from ..api import Api
from .model import WeatherResponse
class WeatherApi:
class WeatherApi(Api):
async def get_locations(self) -> list[str]:
raise NotImplementedError

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)