From 691f9181e1af5c354a1f67c0f3890a6a285247f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=BCller?= Date: Sun, 19 Feb 2023 03:20:08 +0100 Subject: [PATCH] Fix config file caching --- fabcal/calendar_client.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/fabcal/calendar_client.py b/fabcal/calendar_client.py index bbb2fc2..a4eb07c 100644 --- a/fabcal/calendar_client.py +++ b/fabcal/calendar_client.py @@ -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: