implement runtime cache for Crunchyroll.browse()
This commit is contained in:
parent
1ebc1194e6
commit
7d6c300f7e
|
@ -67,7 +67,7 @@ object Crunchyroll {
|
|||
private var signature = ""
|
||||
private var keyPairID = ""
|
||||
|
||||
private val browsingCache = arrayListOf<Item>()
|
||||
private val browsingCache = hashMapOf<String, BrowseResult>()
|
||||
|
||||
/**
|
||||
* Load the pai token, see:
|
||||
|
@ -286,6 +286,11 @@ object Crunchyroll {
|
|||
parameters.add("categories" to categories.joinToString(",") { it.str })
|
||||
}
|
||||
|
||||
// fetch result if not already cached
|
||||
if (browsingCache.contains(parameters.toString())) {
|
||||
Log.d(TAG, "browse result cached: $parameters")
|
||||
} else {
|
||||
Log.d(TAG, "browse result not cached, fetching: $parameters")
|
||||
val browseResult: BrowseResult = try {
|
||||
requestGet(browseEndpoint, parameters)
|
||||
}catch (ex: SerializationException) {
|
||||
|
@ -293,11 +298,17 @@ object Crunchyroll {
|
|||
NoneBrowseResult
|
||||
}
|
||||
|
||||
// add results to cache TODO improve
|
||||
// if the cache has more than 100 entries clear it, so it doesn't become a memory problem
|
||||
// Note: this value is totally guessed and should be replaced by a properly researched value
|
||||
if (browsingCache.size > 100) {
|
||||
browsingCache.clear()
|
||||
browsingCache.addAll(browseResult.items)
|
||||
}
|
||||
|
||||
return browseResult
|
||||
// add results to cache
|
||||
browsingCache[parameters.toString()] = browseResult
|
||||
}
|
||||
|
||||
return browsingCache[parameters.toString()] ?: NoneBrowseResult
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue