Skip to content

Commit 19b48b5

Browse files
committed
feat(DeArrow): cache response
Reduces the number of requests made to the dearrow API by caching the response for the lifetime of the program. A request was made every time a thumbnail came into view (even if it was just recently recycled). While the dearrow server does set appropriate `ETag` headers it still requires executing an HTTP request, just to confirm that the value has no changed.
1 parent e463bce commit 19b48b5

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

app/src/main/java/com/github/libretube/util/DeArrowUtil.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package com.github.libretube.util
22

33
import android.util.Log
4+
import android.util.LruCache
45
import com.github.libretube.api.MediaServiceRepository
56
import com.github.libretube.api.obj.DeArrowContent
67
import com.github.libretube.api.obj.Streams
78
import com.github.libretube.constants.PreferenceKeys
89
import com.github.libretube.helpers.PreferenceHelper
910

11+
private data class CacheObject(val value: DeArrowContent?);
12+
1013
object DeArrowUtil {
14+
// we cannot use segment data directly, as LruCache expects non-null objects, but we also want to cache unlabeled videos
15+
private val memoryCache = object : LruCache<String, CacheObject?>(256) {
16+
override fun sizeOf(key: String, value: CacheObject?): Int = 1
17+
}
18+
1119
private fun extractTitleAndThumbnail(content: DeArrowContent): Pair<String?, String?> {
1220
val title = content.titles.firstOrNull { it.votes >= 0 || it.locked }?.title
1321
val thumbnail = content.thumbnails.firstOrNull {
@@ -19,6 +27,9 @@ object DeArrowUtil {
1927

2028

2129
private suspend fun fetchDeArrowContent(videoId: String): DeArrowContent? {
30+
// prefer cached response, if available
31+
memoryCache.get(videoId)?.let { return it.value }
32+
2233
return try {
2334
MediaServiceRepository.instance.getDeArrowContent(videoId)
2435
.also { memoryCache.put(videoId, CacheObject(it)) }

0 commit comments

Comments
 (0)