Skip to content

Commit

Permalink
add caching repository
Browse files Browse the repository at this point in the history
  • Loading branch information
theScrabi committed Oct 24, 2024
1 parent 3dae80e commit 91473e8
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package net.newpipe.newplayer.repository

import android.graphics.Bitmap
import androidx.media3.common.MediaMetadata
import net.newpipe.newplayer.data.Chapter
import net.newpipe.newplayer.data.Stream
import net.newpipe.newplayer.data.Subtitle

/**
* This is a meta media repository that functions as a cache.
* By default, NewPlayer itself does not cache anything, which is why it asks the repository
* for anything over and over again. If these requests are not cached your app might unnecessarily
* perform network requests or other IO. In order to prevent this use this media repository.
*
* However, it's discouraged to use this cache if your app already has a cache. Use your own
* cache instead in order not to cache the same data twice. When using your own cache you will also
* be able to share cached data between your app and NewPlayer. (IE. NewPipe should not use this).
*/
class CachingMediaRepository(val actualRepository: MediaRepository) : MediaRepository {

open class Cache<K, T> {
var cache: HashMap<K, T> = HashMap()

suspend fun get(key: K, onCacheMiss: suspend () -> T): T =
cache[key] ?: run {
val newValue = onCacheMiss()
if(newValue != null) {
cache[key] = newValue
}
newValue
}

fun flush() {
cache = HashMap()
}
}

data class TimestampedItem(val item:String, val timestamp:Long)
class ItemCache<T> : Cache<String, T>()
class TimeStampedCache<T> : Cache<TimestampedItem, T>()

private var metaInfoCache = ItemCache<MediaMetadata>()
private var streamsCache = ItemCache<List<Stream>>()
private var subtitlesCache = ItemCache<List<Subtitle>>()
private var thumbnailCache = TimeStampedCache<Bitmap?>()
private var chapterCache = ItemCache<List<Chapter>>()
private var timestampLinkCache = TimeStampedCache<String>()

override fun getRepoInfo() = actualRepository.getRepoInfo()

override suspend fun getMetaInfo(item: String) = metaInfoCache.get(item) {
actualRepository.getMetaInfo(item)
}

override suspend fun getStreams(item: String) = streamsCache.get(item) {
actualRepository.getStreams(item)
}

override suspend fun getSubtitles(item: String) = subtitlesCache.get(item) {
actualRepository.getSubtitles(item)
}

override suspend fun getPreviewThumbnail(item: String, timestampInMs: Long) =
thumbnailCache.get(TimestampedItem(item, timestampInMs)) {
actualRepository.getPreviewThumbnail(item, timestampInMs)
}

override suspend fun getChapters(item: String) = chapterCache.get(item) {
actualRepository.getChapters(item)
}

override suspend fun getTimestampLink(item: String, timestampInSeconds: Long) =
timestampLinkCache.get(TimestampedItem(item, timestampInSeconds)) {
actualRepository.getTimestampLink(item, timestampInSeconds)
}

fun flush() {
metaInfoCache.flush()
streamsCache.flush()
subtitlesCache.flush()
thumbnailCache.flush()
thumbnailCache.flush()
chapterCache.flush()
timestampLinkCache.flush()
}
}

0 comments on commit 91473e8

Please sign in to comment.