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

@@ -2,7 +2,6 @@ import datetime
import logging
from typing import Any, Dict, List
from aiocache import cached
from bs4 import BeautifulSoup
from gallery.sketch.source import ApiSource
@@ -17,6 +16,7 @@ logger = logging.getLogger("gismeteo")
class GismeteoApi(WeatherApi):
PROVIDER = "gismeteo"
SOURCE = ApiSource(
"https://www.gismeteo.ru",
cookies={
@@ -32,7 +32,6 @@ class GismeteoApi(WeatherApi):
)
},
)
CACHE_TTL = 10 * 60
def _parse_oneday(self, date: datetime.date, data: str) -> WeatherResponse:
result: List[Dict[str, Any]] = []
@@ -76,12 +75,10 @@ class GismeteoApi(WeatherApi):
LocationId.ZMIYEVKA,
]
@cached(ttl=CACHE_TTL)
async def get_day(self, location_id: str, date: datetime.date) -> WeatherResponse:
data = await self.SOURCE.request(f"weather-{location_id}/{datehelp.dump(date)}")
return self._parse_oneday(date, data)
@cached(ttl=CACHE_TTL)
async def get_days(self, location_id: str, days: int) -> WeatherResponse:
data = await self.SOURCE.request(f"weather-{location_id}/{days}-days")
return self._parse_manydays(data)