From 101940de071d3b175077701624272fb919fd69b9 Mon Sep 17 00:00:00 2001 From: Jana Peper Date: Tue, 24 Jun 2025 13:55:59 +0200 Subject: [PATCH] fix: caching Signed-off-by: Jana Peper --- ex_app/lib/all_tools/calendar.py | 2 -- ex_app/lib/all_tools/lib/decorator.py | 23 +++++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/ex_app/lib/all_tools/calendar.py b/ex_app/lib/all_tools/calendar.py index 6b4f06d..87e7d17 100644 --- a/ex_app/lib/all_tools/calendar.py +++ b/ex_app/lib/all_tools/calendar.py @@ -244,7 +244,5 @@ def add_task(calendar_name: str, title: str, description: str, due_date: Optiona def get_category_name(): return "Calendar and Tasks" -@timed_memoize(5*60) def is_available(nc: Nextcloud): - print('SHOULD DISAPPEAR') return 'calendar' in nc.apps.get_list() \ No newline at end of file diff --git a/ex_app/lib/all_tools/lib/decorator.py b/ex_app/lib/all_tools/lib/decorator.py index 2fe9ea3..c9c6ca4 100644 --- a/ex_app/lib/all_tools/lib/decorator.py +++ b/ex_app/lib/all_tools/lib/decorator.py @@ -12,27 +12,30 @@ def dangerous_tool(tool): setattr(tool, 'safe', False) return tool +# cache for get_tools +# needs NextcloudApp as first arg in the cached function def timed_memoize(timeout): def decorator(func): - cached_result = None - timestamp = 0 + cached_result = {} + timestamp = {} @wraps(func) - def wrapper(*args): + def wrapper(*args): # needs NextcloudApp as first arg nonlocal cached_result nonlocal timestamp + user_id = args[0].user # cache result saved per user current_time = time.time() - if cached_result != None: - if current_time - timestamp < timeout: - return cached_result + if user_id in cached_result: + if current_time - timestamp[user_id] < timeout: + return cached_result[user_id] else: # Cache expired - cached_result = None - timestamp = 0 + del cached_result[user_id] + timestamp[user_id] = 0 # Call the function and cache the result result = func(*args) - cached_result = result - timestamp = current_time + cached_result[user_id] = result + timestamp[user_id] = current_time return result return wrapper