Skip to content

Commit

Permalink
feat : invalidate link cache after 20 mins (recloudstream#875)
Browse files Browse the repository at this point in the history
- additionaly clear cache if there is player errors or no links found

Co-authored-by: coxju <coxju>
  • Loading branch information
coxju authored Jan 17, 2024
1 parent 399b28c commit ebb72d6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ abstract class AbstractPlayerFragment(
throw NotImplementedError()
}

open fun playerStatusChanged(){
throw NotImplementedError()
}

open fun playerDimensionsLoaded(width: Int, height: Int) {
throw NotImplementedError()
}
Expand Down Expand Up @@ -431,6 +435,7 @@ abstract class AbstractPlayerFragment(

is StatusEvent -> {
updateIsPlaying(wasPlaying = event.wasPlaying, isPlaying = event.isPlaying)
playerStatusChanged()
}

is PositionEvent -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class GeneratorPlayer : FullScreenPlayer() {
}
}

override fun playerStatusChanged() {
if(player.getIsPlaying()){
viewModel.forceClearCache = false
}
}

private fun noSubtitles(): Boolean {
return setSubtitles(null)
}
Expand Down Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class PlayerGeneratorViewModel : ViewModel() {
*/
private var currentLoadingEpisodeId: Int? = null

var forceClearCache = false

fun setSubtitleYear(year: Int?) {
_currentSubtitleYear.postValue(year)
}
Expand Down Expand Up @@ -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()

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -10,14 +11,20 @@ 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,
val page: LoadResponse? = null,
) : 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()
}

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit ebb72d6

Please sign in to comment.