modified: app.py

This commit is contained in:
SimolZimol
2024-10-31 18:21:44 +01:00
parent 609425f41e
commit 0deebc2457

10
app.py
View File

@@ -139,21 +139,21 @@ def fetch_and_cache_profile_picture(user_id, avatar_url):
def get_profile_picture(user_id, avatar_url): def get_profile_picture(user_id, avatar_url):
"""Versucht, das Profilbild eines Benutzers aus Redis, lokalem Speicher und zuletzt durch Download zu holen.""" """Gibt den Pfad zum Profilbild eines Benutzers zurück, zuerst aus Redis, dann aus dem lokalen Speicher und zuletzt durch Download."""
# 1. Versuch: Redis # 1. Prüfen, ob das Bild in Redis gecached ist
cached_image = r.get(str(user_id)) cached_image = r.get(str(user_id))
if cached_image: if cached_image:
print(f"Redis cache hit for user {user_id}: {cached_image}") print(f"Redis cache hit for user {user_id}: {cached_image}")
return cached_image.decode('utf-8') return cached_image.decode('utf-8')
# 2. Versuch: Lokaler Speicher # 2. Prüfen, ob das Bild lokal gespeichert ist
local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png") local_image_path = os.path.join(PROFILE_IMAGE_DIR, f"{user_id}.png")
if os.path.exists(local_image_path): if os.path.exists(local_image_path):
print(f"Local cache hit for user {user_id}") print(f"Local cache hit for user {user_id}")
r.set(str(user_id), local_image_path) # Cache das Bild in Redis r.set(str(user_id), local_image_path) # Speichern in Redis
return local_image_path return local_image_path
# 3. Download und Cache des Bilds # 3. Wenn das Bild weder in Redis noch lokal ist, herunterladen und speichern
print(f"Downloading profile picture for user {user_id}") print(f"Downloading profile picture for user {user_id}")
return fetch_and_cache_profile_picture(user_id, avatar_url) return fetch_and_cache_profile_picture(user_id, avatar_url)