From 4373d4ceaa140cb168da9af689a378017b7c092a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 19 Oct 2024 16:59:09 +0200 Subject: [PATCH] [media] Clear the stream media cache on the first update. The stream media cache can easily grow in MB in size. Storing it in Redis means impacting the performance of the application, as on every web media streaming event it'll have to fetch and deserialize MBs of data, and Redis may also flush the .rdb file to disk several times in the process. --- .../http/app/streaming/plugins/media/_register.py | 10 +++++++++- .../http/app/streaming/plugins/media/_registry.py | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/platypush/backend/http/app/streaming/plugins/media/_register.py b/platypush/backend/http/app/streaming/plugins/media/_register.py index de8d0fa852..3c00d20eae 100644 --- a/platypush/backend/http/app/streaming/plugins/media/_register.py +++ b/platypush/backend/http/app/streaming/plugins/media/_register.py @@ -3,7 +3,9 @@ from typing import Optional from platypush.backend.http.app.utils import logger, send_request from platypush.backend.http.media.handlers import MediaHandler -from ._registry import load_media_map, save_media_map +from ._registry import clear_media_map, load_media_map, save_media_map + +_init = False def get_media_url(media_id: str) -> str: @@ -17,6 +19,12 @@ def register_media(source: str, subtitles: Optional[str] = None) -> MediaHandler """ Registers a media file and returns its associated media handler. """ + global _init + + if not _init: + clear_media_map() + _init = True + media_id = MediaHandler.get_media_id(source) media_url = get_media_url(media_id) media_map = load_media_map() diff --git a/platypush/backend/http/app/streaming/plugins/media/_registry.py b/platypush/backend/http/app/streaming/plugins/media/_registry.py index 8a9e62690e..278085a696 100644 --- a/platypush/backend/http/app/streaming/plugins/media/_registry.py +++ b/platypush/backend/http/app/streaming/plugins/media/_registry.py @@ -43,3 +43,12 @@ def save_media_map(new_map: MediaMap): with media_map_lock: redis = get_redis() redis.mset({MEDIA_MAP_VAR: json.dumps(new_map, cls=Message.Encoder)}) + + +def clear_media_map(): + """ + Clears the media map from the server. + """ + with media_map_lock: + redis = get_redis() + redis.delete(MEDIA_MAP_VAR)