Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ex_app/lib/all_tools/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
23 changes: 13 additions & 10 deletions ex_app/lib/all_tools/lib/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading