Improve video queue caching
This commit is contained in:
1 parent
87e50e713f
commit
71f9820ed6
4 files changed
+75
-47
No files matched your search
@@ -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)
|
- 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)
|
- 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.
|
- 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
|
## [0.19.2] - 2024-02-11
|
||||||
|
|
||||||
|
|||||||
@@ -43,14 +43,11 @@ namespace Invidious
|
|||||||
m.node = node
|
m.node = node
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function DefaultQueryParams() as object
|
function GetUserRegion() as string
|
||||||
if m._defaultQueryParams = invalid
|
if m._userRegion = invalid
|
||||||
m._defaultQueryParams = {
|
m._userRegion = m.node.applicationInfo@.GetUserCountryCode()
|
||||||
"region": m.node.applicationInfo@.GetUserCountryCode()
|
|
||||||
}
|
|
||||||
end if
|
end if
|
||||||
|
return m._userRegion
|
||||||
return m._defaultQueryParams
|
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function GetAuthenticationHeaders(token as string) as object
|
function GetAuthenticationHeaders(token as string) as object
|
||||||
@@ -89,7 +86,7 @@ namespace Invidious
|
|||||||
url = `${instance}${Invidious.SEARCH_SUGGEST_ENDPOINT}`
|
url = `${instance}${Invidious.SEARCH_SUGGEST_ENDPOINT}`
|
||||||
|
|
||||||
request = HttpClient.Get(url)
|
request = HttpClient.Get(url)
|
||||||
request.QueryParams(m.DefaultQueryParams())
|
request.QueryParam("region", m.GetUserRegion())
|
||||||
request.QueryParam("q", q)
|
request.QueryParam("q", q)
|
||||||
request.CacheSeconds(60 * 60 * 24)
|
request.CacheSeconds(60 * 60 * 24)
|
||||||
|
|
||||||
@@ -99,33 +96,59 @@ namespace Invidious
|
|||||||
return response.Json()
|
return response.Json()
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function GetVideoMetadata(videoId as string, cancellation = invalid as object) as object
|
function GetVideoMetadata(videoId as string, options = invalid as object) as object
|
||||||
instance = m.GetInstance()
|
region = true
|
||||||
url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`
|
|
||||||
|
|
||||||
request = HttpClient.Get(url)
|
|
||||||
request.QueryParams(m.DefaultQueryParams())
|
|
||||||
' 5 hours cache. Videos should expire in 6 hours, but since we don't
|
' 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
|
' have the exact expiration time, we'll use 5 hours. Invidious currently
|
||||||
' Does a fresh fetch if the cache is older than 10 minutes.
|
' 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
|
' 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.Cancellation(cancellation)
|
||||||
|
|
||||||
request.TryCount(2)
|
if tryCount > 1
|
||||||
request.RetryCallback(function(context as object) as boolean
|
request.TryCount(tryCount)
|
||||||
response = context.response
|
request.RetryCallback(function(context as object) as boolean
|
||||||
' In some cases, video info requests return "HTTP/0.9 when not allowed" errors.
|
response = context.response
|
||||||
' Retrying should usually fix the issue.
|
' In some cases, video info requests return "HTTP/0.9 when not allowed" errors.
|
||||||
if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL
|
' Retrying should usually fix the issue.
|
||||||
errorMessage = response.ErrorMessage()
|
if response.StatusCode() = HttpClient.CurlErrors.CURLE_UNSUPPORTED_PROTOCOL
|
||||||
if errorMessage.InStr("HTTP/0.9") <> -1
|
errorMessage = response.ErrorMessage()
|
||||||
return true
|
if errorMessage.InStr("HTTP/0.9") <> -1
|
||||||
|
return true
|
||||||
|
end if
|
||||||
end if
|
end if
|
||||||
end if
|
|
||||||
|
|
||||||
return false
|
return false
|
||||||
end function)
|
end function)
|
||||||
|
end if
|
||||||
|
|
||||||
return request.Await()
|
return request.Await()
|
||||||
end function
|
end function
|
||||||
@@ -142,7 +165,6 @@ namespace Invidious
|
|||||||
request.Headers(m.GetAuthenticationHeaders(authToken.token))
|
request.Headers(m.GetAuthenticationHeaders(authToken.token))
|
||||||
end if
|
end if
|
||||||
|
|
||||||
request.QueryParams(m.DefaultQueryParams())
|
|
||||||
request.QueryParam("index", index)
|
request.QueryParam("index", index)
|
||||||
request.Cancellation(cancellation)
|
request.Cancellation(cancellation)
|
||||||
|
|
||||||
@@ -154,7 +176,6 @@ namespace Invidious
|
|||||||
url = `${instance}${Invidious.CHANNELS_ENDPOINT}/${authorId}`
|
url = `${instance}${Invidious.CHANNELS_ENDPOINT}/${authorId}`
|
||||||
|
|
||||||
request = HttpClient.Get(url)
|
request = HttpClient.Get(url)
|
||||||
request.QueryParams(m.DefaultQueryParams())
|
|
||||||
request.Cancellation(cancellation)
|
request.Cancellation(cancellation)
|
||||||
request.CacheSeconds(60 * 60 * 24)' 24 hours
|
request.CacheSeconds(60 * 60 * 24)' 24 hours
|
||||||
request.TryCount(3)
|
request.TryCount(3)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ function VideoContentTask(input as object) as object
|
|||||||
|
|
||||||
service = new Invidious.InvidiousService(invidiousNode)
|
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
|
if m.top.cancel
|
||||||
return invalid
|
return invalid
|
||||||
|
|||||||
@@ -13,11 +13,22 @@ function VideoQueueViewContentTask(input as object) as object
|
|||||||
|
|
||||||
index = MathUtils.Clamp(input.index - 2, 0, videoNodes.Count() - 1)
|
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
|
for i = index to videoNodes.Count() - 1
|
||||||
videoNode = videoNodes[i]
|
videoNode = videoNodes[i]
|
||||||
LoadVideoDetail(videoNode, service, instance, cancellation)
|
if not LoadVideoDetail(videoNode, service, instance, requestOptions)
|
||||||
' Sleep for a bit to avoid creating too much traffic
|
' Sleep for a bit to avoid creating too much traffic
|
||||||
sleep(500)
|
sleep(500)
|
||||||
|
end if
|
||||||
|
|
||||||
if m.top.cancel = true
|
if m.top.cancel = true
|
||||||
return invalid
|
return invalid
|
||||||
@@ -26,9 +37,10 @@ function VideoQueueViewContentTask(input as object) as object
|
|||||||
|
|
||||||
for i = index - 1 to 0 step -1
|
for i = index - 1 to 0 step -1
|
||||||
videoNode = videoNodes[i]
|
videoNode = videoNodes[i]
|
||||||
LoadVideoDetail(videoNode, service, instance, cancellation)
|
if not LoadVideoDetail(videoNode, service, instance, requestOptions)
|
||||||
' Sleep for a bit to avoid creating too much traffic
|
' Sleep for a bit to avoid creating too much traffic
|
||||||
sleep(1000)
|
sleep(1000)
|
||||||
|
end if
|
||||||
|
|
||||||
if m.top.cancel = true
|
if m.top.cancel = true
|
||||||
return invalid
|
return invalid
|
||||||
@@ -38,17 +50,10 @@ function VideoQueueViewContentTask(input as object) as object
|
|||||||
return invalid
|
return invalid
|
||||||
end function
|
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
|
videoId = videoNode.videoId
|
||||||
url = `${instance}${Invidious.VIDEOS_ENDPOINT}/${videoId}`
|
response = service.GetVideoMetadata(videoId, requestOptions)
|
||||||
|
|
||||||
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()
|
|
||||||
metadata = response.Json()
|
metadata = response.Json()
|
||||||
|
|
||||||
if not response.IsSuccess() or metadata = invalid
|
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
|
' We set the title to make sure we don't try to load it again
|
||||||
videoNode.title = "Video not available"
|
videoNode.title = "Video not available"
|
||||||
end if
|
end if
|
||||||
return
|
return false
|
||||||
end if
|
end if
|
||||||
|
|
||||||
InvidiousContent.ToVideoContentNode(videoNode, metadata, instance)
|
InvidiousContent.ToVideoContentNode(videoNode, metadata, instance)
|
||||||
|
return response.IsCached()
|
||||||
end function
|
end function
|
||||||
Reference in new issue
Block a user