From 61ef428fadad00585c858849264b8c3acc0bbdc7 Mon Sep 17 00:00:00 2001 From: filoor Date: Thu, 29 May 2025 21:25:10 +0200 Subject: [PATCH] perf(webapp): voeg caching toe voor verbeterde API-prestaties MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Caching is geïmplementeerd voor API-aanroepen om de prestaties van de webapplicatie te optimaliseren. Een lru_cache decorator en een aangepaste cache-functionaliteit zijn toegevoegd voor het laden van instellingen en het ophalen van weers- en Sonos-zonegegevens. Deze wijzigingen verminderen de belasting van externe bronnen en verbeteren de reactiesnelheid van de applicatie door herhaalde aanroepen binnen een korte tijdsduur te vermijden. --- adhan-webapp/app.py | 48 +++++++++++++++++++++++++++++++++++---------- adhan-webapp/done | 1 + 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/adhan-webapp/app.py b/adhan-webapp/app.py index 9ba346e..784ca71 100644 --- a/adhan-webapp/app.py +++ b/adhan-webapp/app.py @@ -1,11 +1,31 @@ from flask import Flask, render_template, request, redirect, send_from_directory, jsonify, request as flask_request import requests, json, os, random, subprocess -from datetime import datetime +from datetime import datetime, timedelta from config import * from hijridate import Gregorian +from functools import lru_cache +import time app = Flask(__name__) +# Cache configuratie +CACHE_DURATION = 300 # 5 minuten cache voor API calls +last_api_call = {} +cached_data = {} + +def get_cached_data(key, fetch_func, duration=CACHE_DURATION): + """Haal data uit cache of voer fetch_func uit als cache verlopen is""" + current_time = time.time() + + if key in cached_data and key in last_api_call: + if current_time - last_api_call[key] < duration: + return cached_data[key] + + data = fetch_func() + cached_data[key] = data + last_api_call[key] = current_time + return data + # Voeg cache-control headers toe voor statische bestanden @app.after_request def add_header(response): @@ -28,8 +48,21 @@ CLIPS_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', debug_time_offset = 0 # Offset in seconden voor debug mode debug_mode_active = False +@lru_cache(maxsize=1) +def load_settings(): + try: + if os.path.exists(SETTINGS_PATH) and os.path.getsize(SETTINGS_PATH) > 0: + with open(SETTINGS_PATH) as f: + return json.load(f) + except Exception as e: + print(f"⚠️ Fout bij laden van settings: {e}") + return {"volume": 30, "zones": ["Woonkamer"], "audio_clip": "adhan1.mp3"} + def fetch_weather_data(): """Haalt weersinformatie op voor de geconfigureerde locatie""" + return get_cached_data('weather', lambda: _fetch_weather_data_impl()) + +def _fetch_weather_data_impl(): try: params = { 'q': WEATHER_LOCATION, @@ -70,15 +103,6 @@ def load_hadith(): print(f"⚠️ Fout bij laden van hadith: {e}") return {"text": "De daden die Allah het meest liefheeft zijn degenen die regelmatig worden verricht, zelfs als ze klein zijn.", "bron": "Sahih al-Bukhari"} -def load_settings(): - try: - if os.path.exists(SETTINGS_PATH) and os.path.getsize(SETTINGS_PATH) > 0: - with open(SETTINGS_PATH) as f: - return json.load(f) - except Exception as e: - print(f"⚠️ Fout bij laden van settings: {e}") - return {"volume": 30, "zones": ["Woonkamer"], "audio_clip": "adhan1.mp3"} - def get_current_volume(settings): """Bepaal het juiste volume op basis van de huidige tijd""" from datetime import datetime, timedelta @@ -158,7 +182,11 @@ def apply_prayer_offsets(gebedstijden, settings): return adjusted_times +@lru_cache(maxsize=1) def fetch_sonos_zones(): + return get_cached_data('sonos_zones', lambda: _fetch_sonos_zones_impl()) + +def _fetch_sonos_zones_impl(): try: res = requests.get(f'http://{SONOS_API_IP}:5005/zones', timeout=5) res.raise_for_status() diff --git a/adhan-webapp/done b/adhan-webapp/done index f009148..cea6f1d 100644 --- a/adhan-webapp/done +++ b/adhan-webapp/done @@ -13,3 +13,4 @@ Mon May 26 18:17:57 CEST 2025: Tijdzone probleem opgelost - Container gebruikt n 2025-05-28 03:49:24 - Adzkaar fullscreen functionaliteit geïmplementeerd: nieuwe /adzkaar route, instellingen, debug knoppen, automatische trigger na gebedstijden, Nederlandse/Arabische dhikr content 2025-05-28 03:55:38 - Adzkaar scherm verbeterd naar kaart-voor-kaart weergave met navigatie knoppen en toetsenbord besturing Wed May 28 14:09:12 CEST 2025: Sonos debug tijd synchronisatie geïmplementeerd - get_current_volume functie en cron script gebruiken nu debug tijd API, volume bepaling werkt correct in debug mode +2025-05-29 21:24:37 - Performance optimalisaties toegevoegd: caching voor API calls en instellingen