Skip to content

Commit ea5ee26

Browse files
committed
fix(cache): simplified cache logic
1 parent be36b8d commit ea5ee26

2 files changed

Lines changed: 6 additions & 11 deletions

File tree

app/src/main/java/com/lagradost/cloudstream3/ui/APIRepository.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ class APIRepository(val api: MainAPI) {
9595
}
9696

9797
fun getEffectiveHomepageCacheTtl(maxHomepageCacheTimeMs: Long?): Long {
98-
val userCacheTtl = DataStoreHelper.cacheTimeSeconds
99-
if (userCacheTtl <= 0L) return 0L
100-
val providerMaxSeconds = maxHomepageCacheTimeMs?.let { it / 1000L } ?: return userCacheTtl
101-
return if (providerMaxSeconds <= 0L) 0L else minOf(userCacheTtl, providerMaxSeconds)
98+
val userTtl = DataStoreHelper.cacheTimeSeconds.coerceAtLeast(0L)
99+
return maxHomepageCacheTimeMs?.let { minOf(userTtl, it / 1000L).coerceAtLeast(0L) } ?: userTtl
102100
}
103101

104102
fun hasHomePageCache(
@@ -110,13 +108,10 @@ class APIRepository(val api: MainAPI) {
110108
val cacheTtl = getEffectiveHomepageCacheTtl(maxHomepageCacheTimeMs)
111109
if (cacheTtl <= 0L) return false
112110
val lookingForHash = Pair(apiName, Pair(page, nameIndex))
113-
val inRam = homeCache.withLock {
111+
return homeCache.withLock {
114112
homeCache.any { it.hash == lookingForHash && unixTime - it.unixTime < cacheTtl }
115-
}
116-
if (inRam) return true
117-
val diskKey = "${apiName}_${page}_${nameIndex}"
118-
val onDisk = CloudStreamApp.getKey<SavedHomePageResponse>(HOME_CACHE_FOLDER, diskKey)
119-
return onDisk != null && unixTime - onDisk.unixTime < cacheTtl
113+
} || CloudStreamApp.getKey<SavedHomePageResponse>(HOME_CACHE_FOLDER, "${apiName}_${page}_${nameIndex}")
114+
?.let { unixTime - it.unixTime < cacheTtl } == true
120115
}
121116
}
122117

app/src/main/java/com/lagradost/cloudstream3/ui/home/HomeViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class HomeViewModel : ViewModel() {
338338
}
339339

340340

341-
if (forceReload || !APIRepository.hasHomePageCache(api.name, api.maxHomepageCacheTime, 1, null)) {
341+
if (forceReload || !APIRepository.hasHomePageCache(api.name, api.maxHomepageCacheTime)) {
342342
_page.postValue(Resource.Loading())
343343
}
344344

0 commit comments

Comments
 (0)