From ec9c0db867bedc28ea3a684e10d2a93f18719686 Mon Sep 17 00:00:00 2001 From: chrisk325 Date: Fri, 6 Mar 2026 01:25:51 +0530 Subject: [PATCH] fix logic for cdn caching --- src/services/trailerService.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/services/trailerService.ts b/src/services/trailerService.ts index 7f5238da..86762505 100644 --- a/src/services/trailerService.ts +++ b/src/services/trailerService.ts @@ -14,8 +14,8 @@ interface CacheEntry { } export class TrailerService { - // YouTube CDN URLs expire ~6h; cache for 5h - private static readonly CACHE_TTL_MS = 5 * 60 * 60 * 1000; + // Cache for 3 minutes — just enough to avoid re-extracting on quick re-renders + private static readonly CACHE_TTL_MS = 3 * 60 * 1000; private static urlCache = new Map(); // --------------------------------------------------------------------------- @@ -137,6 +137,22 @@ export class TrailerService { this.urlCache.delete(key); return null; } + // Check the URL's own CDN expiry — googlevideo.com URLs carry an `expire` + // param (Unix timestamp). Treat as stale if it expires within 2 minutes. + if (entry.url.includes('googlevideo.com')) { + try { + const u = new URL(entry.url); + const expire = u.searchParams.get('expire'); + if (expire) { + const expiresAt = parseInt(expire, 10) * 1000; + if (Date.now() > expiresAt - 2 * 60 * 1000) { + logger.info('TrailerService', `Cached URL expired or expiring soon — re-extracting`); + this.urlCache.delete(key); + return null; + } + } + } catch { /* ignore */ } + } return entry.url; }