Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import java.util.concurrent.ConcurrentHashMap
import android.util.LruCache
import com.arflix.tv.util.ParsedCatalogUrl
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -105,21 +106,30 @@ class MediaRepository @Inject constructor(
private data class CacheEntry<T>(val data: T, val timestamp: Long)
private val CACHE_TTL_MS = 5 * 60 * 1000L // 5 minutes

private operator fun <K : Any, V : Any> LruCache<K, V>.set(key: K, value: V) {
put(key, value)
}

// Home categories cache - survives ViewModel recreation
@Volatile var cachedHomeCategories: List<Category> = emptyList()
private set
@Volatile private var homeCategoriesFetchedAt = 0L
private val HOME_CATEGORIES_CACHE_MS = 120_000L // 2 minutes

private val detailsCache = mutableMapOf<String, CacheEntry<MediaItem>>()
private fun <K, V> createBoundedCache(maxSize: Int): MutableMap<K, V> {
return object : java.util.LinkedHashMap<K, V>(maxSize, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<K, V>?): Boolean = size > maxSize
}
}
private val detailsCache = createBoundedCache<String, CacheEntry<MediaItem>>(200)
private val fullDetailsCacheKeys = mutableSetOf<String>()
private val castCache = mutableMapOf<String, CacheEntry<List<CastMember>>>()
private val similarCache = mutableMapOf<String, CacheEntry<List<MediaItem>>>()
private val logoCache = mutableMapOf<String, CacheEntry<String?>>()
private val reviewsCache = mutableMapOf<String, CacheEntry<List<Review>>>()
private val watchProvidersCache = mutableMapOf<String, CacheEntry<StreamingServicesResult?>>()
private val seasonEpisodesCache = mutableMapOf<String, CacheEntry<List<Episode>>>()
private val imdbRatingCache = ConcurrentHashMap<String, CacheEntry<String>>()
private val castCache = createBoundedCache<String, CacheEntry<List<CastMember>>>(150)
private val similarCache = createBoundedCache<String, CacheEntry<List<MediaItem>>>(150)
private val logoCache = createBoundedCache<String, CacheEntry<String?>>(300)
private val reviewsCache = createBoundedCache<String, CacheEntry<List<Review>>>(100)
private val watchProvidersCache = createBoundedCache<String, CacheEntry<StreamingServicesResult?>>(150)
private val seasonEpisodesCache = createBoundedCache<String, CacheEntry<List<Episode>>>(200)
private val imdbRatingCache = java.util.Collections.synchronizedMap(createBoundedCache<String, CacheEntry<String>>(500))
private val imdbEpisodeRatingsCache = ConcurrentHashMap<String, CacheEntry<Map<Pair<Int, Int>, String>>>()
private val imdbRatingsByIdCache = ConcurrentHashMap<String, CacheEntry<String>>()
private val episodeImdbIdCache = ConcurrentHashMap<String, CacheEntry<String>>()
Expand Down