From 71f9820ed62ee093ec1997d8d913ff5cfd89c7f7 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 20 Feb 2024 11:47:48 -0500 Subject: [PATCH] Improve video queue caching --- CHANGELOG.md | 1 + .../Services/Invidious/InvidiousService.bs | 79 ++++++++++++------- .../VideoPlayer/VideoContentTask.bs | 2 +- .../VideoQueue/VideoQueueViewContentTask.bs | 40 ++++++---- 4 files changed, 75 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43fc23bd..d35b8631 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The `Web App` tab is now called `Remote`. It can be used to open the web app, or connect using Lounge (cast from YouTube) - Removed `fields` from Invidious requests, as per [https://github.com/iv-org/invidious/pull/4276](https://github.com/iv-org/invidious/pull/4276) - The `POST /api/queue/` no longer returns the current queue. Instead it returns a 204. +- [Perf] improved some caching of videos in the queue ## [0.19.2] - 2024-02-11 diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs index be23055e..a8e789e7 100644 --- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs +++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs @@ -43,14 +43,11 @@ namespace Invidious m.node = node end function - function DefaultQueryParams() as object - if m._defaultQueryParams = invalid - m._defaultQueryParams = { - "region": m.node.applicationInfo@.GetUserCountryCode() - } + function GetUserRegion() as string + if m._userRegion = invalid + m._userRegion = m.node.applicationInfo@.GetUserCountryCode() end if - - return m._defaultQueryParams + return m._userRegion end function function GetAuthenticationHeaders(token as string) as object @@ -89,7 +86,7 @@ namespace Invidious url = `${instance}${Invidious.SEARCH_SUGGEST_ENDPOINT}` request = HttpClient.Get(url) - request.QueryParams(m.DefaultQueryParams()) + request.QueryParam("region", m.GetUserRegion()) request.QueryParam("q", q) request.CacheSeconds(60 * 60 * 24) @@ -99,33 +96,59 @@ namespace Invidious return response.Json() end function - function GetVideoMetadata(videoId as string, cancellation = invalid as object) as object - instance = m.GetInstance() - url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}` - - request = HttpClient.Get(url) - request.QueryParams(m.DefaultQueryParams()) + function GetVideoMetadata(videoId as string, options = invalid as object) as object + region = true ' 5 hours cache. Videos should expire in 6 hours, but since we don't ' have the exact expiration time, we'll use 5 hours. Invidious currently ' Does a fresh fetch if the cache is older than 10 minutes. ' https://github.com/iv-org/invidious/blob/069e91d2a609b1df94bdfbb693335969b2a281d8/src/invidious/videos.cr#L349 - request.CacheSeconds(60 * 60 * 5) + cacheSeconds = 18000 ' 5 hours + cancellation = invalid + tryCount = 2 + + if options <> invalid + if options.DoesExist("region") + region = options.region + end if + if options.DoesExist("cacheSeconds") + cacheSeconds = options.cacheSeconds + end if + if options.DoesExist("cancellation") + cancellation = options.cancellation + end if + if options.DoesExist("tryCount") + tryCount = options.tryCount + end if + end if + + instance = m.GetInstance() + url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}` + + request = HttpClient.Get(url) + + if region = true + request.QueryParam("region", m.GetUserRegion()) + end if + + request.CacheSeconds(cacheSeconds) request.Cancellation(cancellation) - request.TryCount(2) - request.RetryCallback(function(context as object) as boolean - response = context.response - ' In some cases, video info requests return "HTTP/0.9 when not allowed" errors. - ' Retrying should usually fix the issue. - if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL - errorMessage = response.ErrorMessage() - if errorMessage.InStr("HTTP/0.9") <> -1 - return true + if tryCount > 1 + request.TryCount(tryCount) + request.RetryCallback(function(context as object) as boolean + response = context.response + ' In some cases, video info requests return "HTTP/0.9 when not allowed" errors. + ' Retrying should usually fix the issue. + if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL + errorMessage = response.ErrorMessage() + if errorMessage.InStr("HTTP/0.9") <> -1 + return true + end if end if - end if - return false - end function) + return false + end function) + end if return request.Await() end function @@ -142,7 +165,6 @@ namespace Invidious request.Headers(m.GetAuthenticationHeaders(authToken.token)) end if - request.QueryParams(m.DefaultQueryParams()) request.QueryParam("index", index) request.Cancellation(cancellation) @@ -154,7 +176,6 @@ namespace Invidious url = `${instance}${Invidious.CHANNELS_ENDPOINT}/${authorId}` request = HttpClient.Get(url) - request.QueryParams(m.DefaultQueryParams()) request.Cancellation(cancellation) request.CacheSeconds(60 * 60 * 24)' 24 hours request.TryCount(3) diff --git a/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs b/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs index 3792ed21..4a54bb7d 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoContentTask.bs @@ -17,7 +17,7 @@ function VideoContentTask(input as object) as object service = new Invidious.InvidiousService(invidiousNode) - response = service.GetVideoMetadata(contentNode.videoId, m.top.cancellation) + response = service.GetVideoMetadata(contentNode.videoId, { cancellation: m.top.cancellation }) if m.top.cancel return invalid diff --git a/playlet-lib/src/components/VideoQueue/VideoQueueViewContentTask.bs b/playlet-lib/src/components/VideoQueue/VideoQueueViewContentTask.bs index aac1708a..44335ab9 100644 --- a/playlet-lib/src/components/VideoQueue/VideoQueueViewContentTask.bs +++ b/playlet-lib/src/components/VideoQueue/VideoQueueViewContentTask.bs @@ -13,11 +13,22 @@ function VideoQueueViewContentTask(input as object) as object index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1) + requestOptions = { + ' Not providing a region should lead to a better DB hit rate + ' When the region is provided, the video will be fetched fresh every time + ' and will not be saved to the database. + region: false + cacheSeconds: 8640000 ' 100 days + cancellation: cancellation + tryCount: 1 + } + for i = index to videoNodes.Count() - 1 videoNode = videoNodes[i] - LoadVideoDetail(videoNode, service, instance, cancellation) - ' Sleep for a bit to avoid creating too much traffic - sleep(500) + if not LoadVideoDetail(videoNode, service, instance, requestOptions) + ' Sleep for a bit to avoid creating too much traffic + sleep(500) + end if if m.top.cancel = true return invalid @@ -26,9 +37,10 @@ function VideoQueueViewContentTask(input as object) as object for i = index - 1 to 0 step -1 videoNode = videoNodes[i] - LoadVideoDetail(videoNode, service, instance, cancellation) - ' Sleep for a bit to avoid creating too much traffic - sleep(1000) + if not LoadVideoDetail(videoNode, service, instance, requestOptions) + ' Sleep for a bit to avoid creating too much traffic + sleep(1000) + end if if m.top.cancel = true return invalid @@ -38,17 +50,10 @@ function VideoQueueViewContentTask(input as object) as object return invalid end function -function LoadVideoDetail(videoNode as object, service as object, instance as string, cancellation as object) as void +' Returns true if the video was loaded from cache. +function LoadVideoDetail(videoNode as object, service as object, instance as string, requestOptions as object) as boolean videoId = videoNode.videoId - url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}` - - request = HttpClient.Get(url) - request.QueryParams(service.DefaultQueryParams()) - ' A large cache interval, since we don't care about the video detail accuracy - request.CacheSeconds(8640000) - request.Cancellation(cancellation) - - response = request.Await() + response = service.GetVideoMetadata(videoId, requestOptions) metadata = response.Json() if not response.IsSuccess() or metadata = invalid @@ -58,8 +63,9 @@ function LoadVideoDetail(videoNode as object, service as object, instance as str ' We set the title to make sure we don't try to load it again videoNode.title = "Video not available" end if - return + return false end if InvidiousContent.ToVideoContentNode(videoNode, metadata, instance) + return response.IsCached() end function