Fix config file caching

This commit is contained in:
Fabian Müller 2023-02-19 03:20:08 +01:00
parent b432503ac9
commit 691f9181e1

View File

@ -10,7 +10,7 @@ import recurring_ical_events
import yaml
from asyncache import cached
from cachetools import TTLCache, LRUCache
from cachetools import TTLCache, FIFOCache
from icalendar import Calendar
from fabcal.models import CalendarEvent
@ -35,20 +35,23 @@ class CalendarClient:
# this will be cached permanently, i.e., the server process needs to be restarted to apply config changes
@cached(LRUCache(100))
def read_calendars_from_config_file():
# note that caching doesn't work at all with iterators (for obvious reasons)
@cached(FIFOCache(1))
def read_calendars_from_config_file() -> List[ConfiguredCalendar]:
rv = []
with open("config.yml") as f:
data = yaml.safe_load(f)
for calendar in data["calendars"]:
yield ConfiguredCalendar(**calendar)
rv.append(ConfiguredCalendar(**calendar))
return rv
class CombinedCalendarClient:
def __init__(self, configured_calendars: List[ConfiguredCalendar]):
# make sure it's a list since read_calendars_from_config_file() returns an iterator
self.configured_calendars = list(configured_calendars)
self.configured_calendars = configured_calendars
async def fetch_calendars(self) -> Dict[ConfiguredCalendar, str]:
async with aiohttp.ClientSession() as session: