feat: add redis cache
This commit is contained in:
0
gallery/sketch/__init__.py
Normal file
0
gallery/sketch/__init__.py
Normal file
6
gallery/sketch/api.py
Normal file
6
gallery/sketch/api.py
Normal file
@@ -0,0 +1,6 @@
|
||||
class Api:
|
||||
PROVIDER: str
|
||||
|
||||
@property
|
||||
def provider(self) -> str:
|
||||
return self.PROVIDER
|
||||
20
gallery/sketch/cached.py
Normal file
20
gallery/sketch/cached.py
Normal 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
|
||||
@@ -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
|
||||
|
||||
|
||||
32
gallery/sketch/schedule/cached.py
Normal file
32
gallery/sketch/schedule/cached.py
Normal 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)
|
||||
@@ -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
|
||||
|
||||
|
||||
40
gallery/sketch/weather/cached.py
Normal file
40
gallery/sketch/weather/cached.py
Normal 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)
|
||||
Reference in New Issue
Block a user