[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.
This commit is contained in:
Fabio Manganiello 2024-10-19 16:59:09 +02:00
parent 7620e1ead7
commit 4373d4ceaa
Signed by: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 18 additions and 1 deletions

View file

@ -3,7 +3,9 @@ from typing import Optional
from platypush.backend.http.app.utils import logger, send_request from platypush.backend.http.app.utils import logger, send_request
from platypush.backend.http.media.handlers import MediaHandler 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: 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. 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_id = MediaHandler.get_media_id(source)
media_url = get_media_url(media_id) media_url = get_media_url(media_id)
media_map = load_media_map() media_map = load_media_map()

View file

@ -43,3 +43,12 @@ def save_media_map(new_map: MediaMap):
with media_map_lock: with media_map_lock:
redis = get_redis() redis = get_redis()
redis.mset({MEDIA_MAP_VAR: json.dumps(new_map, cls=Message.Encoder)}) 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)