From 834e4670480d777d80a03e56894636df3e70e51b Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Thu, 31 Oct 2024 20:07:26 +0100 Subject: [PATCH] modified: app.py --- app.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 9dac0eb..d99b060 100644 --- a/app.py +++ b/app.py @@ -106,21 +106,27 @@ def make_discord_session(token=None, state=None): ) def fetch_and_cache_profile_picture(user_id, avatar_url): - """Lädt das Profilbild herunter, wenn es nicht lokal vorhanden ist, und speichert es in Redis.""" + """Lädt das Profilbild herunter, speichert es lokal und in Redis, wenn es nicht vorhanden ist.""" if not user_id: print(f"Invalid user_id: {user_id}. Using default profile picture.") return "/static/default_profile.png" - # Erstellen des lokalen Pfads für das Bild + # Pfad zum lokalen Bild local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png") - # Überprüfen, ob das Bild lokal gespeichert ist + # Prüfe, ob das Bild lokal vorhanden ist if os.path.exists(local_image_path): print(f"Local image already exists for user {user_id}: {local_image_path}") - r.set(str(user_id), local_image_path) + r.set(str(user_id), local_image_path) # Speichere in Redis für zukünftige Anfragen return local_image_path - # Wenn das Bild nicht lokal vorhanden ist, versuche, es herunterzuladen + # Prüfe Redis für den Pfad + cached_image = r.get(str(user_id)) + if cached_image: + print(f"Redis cache hit for user {user_id}: {cached_image.decode('utf-8')}") + return cached_image.decode('utf-8') + + # Falls das Bild nicht lokal vorhanden ist und Redis keinen Pfad hat, versuche das Bild herunterzuladen if avatar_url: try: print(f"Downloading profile picture for user {user_id} from {avatar_url}") @@ -143,10 +149,11 @@ def fetch_and_cache_profile_picture(user_id, avatar_url): return "/static/default_profile.png" def get_profile_picture(user_id, avatar_url): - """Gibt den Pfad zum Profilbild zurück oder lädt es herunter, wenn es nicht in Redis oder lokal vorhanden ist.""" + """Gibt den Pfad zum Profilbild eines Benutzers zurück oder lädt es herunter, falls es nicht in Redis oder lokal vorhanden ist.""" cached_image = r.get(str(user_id)) if cached_image: - return cached_image.decode('utf-8') + print(f"Using cached image for user {user_id}") + return cached_image.decode('utf-8') # Rückgabe des Pfads aus Redis return fetch_and_cache_profile_picture(user_id, avatar_url) @app.context_processor