feat : invalidate link cache after 20 mins (#875)

- additionaly clear cache if there is player errors or no links found

Co-authored-by: coxju <coxju>
This commit is contained in:
coxju 2024-01-18 02:59:44 +05:30 committed by GitHub
parent 399b28c75b
commit ebb72d6a0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 38 additions and 7 deletions

View File

@ -102,6 +102,10 @@ abstract class AbstractPlayerFragment(
throw NotImplementedError()
}
open fun playerStatusChanged(){
throw NotImplementedError()
}
open fun playerDimensionsLoaded(width: Int, height: Int) {
throw NotImplementedError()
}
@ -431,6 +435,7 @@ abstract class AbstractPlayerFragment(
is StatusEvent -> {
updateIsPlaying(wasPlaying = event.wasPlaying, isPlaying = event.isPlaying)
playerStatusChanged()
}
is PositionEvent -> {

View File

@ -146,6 +146,12 @@ class GeneratorPlayer : FullScreenPlayer() {
}
}
override fun playerStatusChanged() {
if(player.getIsPlaying()){
viewModel.forceClearCache = false
}
}
private fun noSubtitles(): Boolean {
return setSubtitles(null)
}
@ -913,10 +919,15 @@ class GeneratorPlayer : FullScreenPlayer() {
override fun playerError(exception: Throwable) {
Log.i(TAG, "playerError = $currentSelectedLink")
if(!hasNextMirror()){
viewModel.forceClearCache = true
}
super.playerError(exception)
}
private fun noLinksFound() {
viewModel.forceClearCache = true
showToast(R.string.no_links_found_toast, Toast.LENGTH_SHORT)
activity?.popCurrentPage()
}

View File

@ -45,6 +45,8 @@ class PlayerGeneratorViewModel : ViewModel() {
*/
private var currentLoadingEpisodeId: Int? = null
var forceClearCache = false
fun setSubtitleYear(year: Int?) {
_currentSubtitleYear.postValue(year)
}
@ -168,7 +170,7 @@ class PlayerGeneratorViewModel : ViewModel() {
}
}
fun loadLinks(clearCache: Boolean = false, type: LoadType = LoadType.InApp) {
fun loadLinks(type: LoadType = LoadType.InApp) {
Log.i(TAG, "loadLinks")
currentJob?.cancel()
@ -183,7 +185,7 @@ class PlayerGeneratorViewModel : ViewModel() {
// load more data
_loadingLinks.postValue(Resource.Loading())
val loadingState = safeApiCall {
generator?.generateLinks(type = type, clearCache = clearCache, callback = {
generator?.generateLinks(type = type, clearCache = forceClearCache, callback = {
currentLinks.add(it)
// Clone to prevent ConcurrentModificationException
normalSafeApiCall {

View File

@ -2,6 +2,7 @@ package com.lagradost.cloudstream3.ui.player
import android.util.Log
import com.lagradost.cloudstream3.APIHolder.getApiFromNameNull
import com.lagradost.cloudstream3.APIHolder.unixTime
import com.lagradost.cloudstream3.LoadResponse
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.result.ResultEpisode
@ -10,6 +11,12 @@ import com.lagradost.cloudstream3.utils.ExtractorUri
import kotlin.math.max
import kotlin.math.min
data class Cache(
val linkCache: MutableSet<ExtractorLink>,
val subtitleCache: MutableSet<SubtitleData>,
var lastCachedTimestamp: Long = unixTime
)
class RepoLinkGenerator(
private val episodes: List<ResultEpisode>,
private var currentIndex: Int = 0,
@ -17,7 +24,7 @@ class RepoLinkGenerator(
) : IGenerator {
companion object {
const val TAG = "RepoLink"
val cache: HashMap<Pair<String, Int>, Pair<MutableSet<ExtractorLink>, MutableSet<SubtitleData>>> =
val cache: HashMap<Pair<String, Int>, Cache> =
hashMapOf()
}
@ -76,10 +83,10 @@ class RepoLinkGenerator(
val index = currentIndex
val current = episodes.getOrNull(index + offset) ?: return false
val (currentLinkCache, currentSubsCache) = if (clearCache) {
Pair(mutableSetOf(), mutableSetOf())
val (currentLinkCache, currentSubsCache, lastCachedTimestamp) = if (clearCache) {
Cache(mutableSetOf(), mutableSetOf(), unixTime)
} else {
cache[current.apiName to current.id] ?: Pair(mutableSetOf(), mutableSetOf())
cache[current.apiName to current.id] ?: Cache(mutableSetOf(), mutableSetOf(), unixTime)
}
//val currentLinkCache = if (clearCache) mutableSetOf() else linkCache[index].toMutableSet()
@ -89,6 +96,12 @@ class RepoLinkGenerator(
val currentSubsUrls = mutableSetOf<String>() // makes all subs urls unique
val currentSubsNames = mutableSetOf<String>() // makes all subs names unique
val invalidateCache = unixTime - lastCachedTimestamp > 60 * 20 // 20 minutes
if(invalidateCache){
currentLinkCache.clear()
currentSubsCache.clear()
}
currentLinkCache.filter { allowedTypes.contains(it.type) }.forEach { link ->
currentLinks.add(link.url)
callback(link to null)
@ -147,7 +160,7 @@ class RepoLinkGenerator(
}
}
)
cache[Pair(current.apiName, current.id)] = Pair(currentLinkCache, currentSubsCache)
cache[Pair(current.apiName, current.id)] = Cache(currentLinkCache, currentSubsCache, unixTime)
return result
}