Reintroduced caching — if it has been less than a 1 second since the last call, just return the same payload

This commit is contained in:
nutty
2026-08-16 05:33:34 +10:00
parent 6523694f17
commit 3226e12c3b
+28 -2
View File
@@ -1,5 +1,5 @@
# Versioning # Versioning
APP_VERSION = "0.0.4" APP_VERSION = "0.0.5"
DEVELOPER = "nutty" DEVELOPER = "nutty"
@@ -161,6 +161,22 @@ try:
async def get_all_media_info(): async def get_all_media_info():
try: try:
# We will cache the last execution time and payload to avoid redundant parsing if requests flood in faster than 1 second.
current_time = time.time()
if not hasattr(get_all_media_info, "last_execution"):
get_all_media_info.last_execution = 0
get_all_media_info.last_payload = None
# If requests flood in faster than 1 second, return the cached result
# to completely spare the CPU from redundant parsing.
if (current_time - get_all_media_info.last_execution) < 1.0 and get_all_media_info.last_payload:
# print("Using cached media info.")
return get_all_media_info.last_payload
# else:
# print("Fetching fresh media info.")
get_all_media_info.last_execution = current_time
# Instantiate the SMTC manager -> This allows use to "talk" to the Windows Media API # Instantiate the SMTC manager -> This allows use to "talk" to the Windows Media API
manager = await SMTC.request_async() manager = await SMTC.request_async()
@@ -271,6 +287,9 @@ try:
try: try:
stream_ref = raw_media.thumbnail stream_ref = raw_media.thumbnail
stream = await stream_ref.open_read_async() stream = await stream_ref.open_read_async()
# Only proceed if there's actual data to read
if stream.size > 0:
reader = DataReader(stream.get_input_stream_at(0)) reader = DataReader(stream.get_input_stream_at(0))
await reader.load_async(stream.size) await reader.load_async(stream.size)
buffer = bytearray(stream.size) buffer = bytearray(stream.size)
@@ -292,8 +311,10 @@ try:
# Check if we already processed this exact artwork for this app # Check if we already processed this exact artwork for this app
if get_all_media_info.cache.get(safe_app_id) == img_hash and os.path.exists(thumb_path): if get_all_media_info.cache.get(safe_app_id) == img_hash and os.path.exists(thumb_path):
# Artwork hasn't changed, reuse existing file and version timestamp # Artwork hasn't changed, reuse existing file and version timestamp
# print("Using cached artwork.")
pass pass
else: else:
print(f"Processing new artwork for: {media_data['Artist']} - {media_data['Title']}")
# Process and save with Pillow only when bytes actually change # Process and save with Pillow only when bytes actually change
img = Image.open(io.BytesIO(buffer)) img = Image.open(io.BytesIO(buffer))
if img.mode in ("RGBA", "P"): if img.mode in ("RGBA", "P"):
@@ -331,11 +352,16 @@ try:
}) })
# Build the final payload # Build the final payload
return { payload = {
"app_version": APP_VERSION, "app_version": APP_VERSION,
"current_session_id": current_session_id, "current_session_id": current_session_id,
"sessions": sessions_list "sessions": sessions_list
} }
# Save to global payload cache
get_all_media_info.last_payload = payload
return payload
except Exception as e: except Exception as e:
return {"current_session_id": None, "sessions": [], "error": str(e)} return {"current_session_id": None, "sessions": [], "error": str(e)}