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 signature = ""
|
||||||
private var keyPairID = ""
|
private var keyPairID = ""
|
||||||
|
|
||||||
private val browsingCache = arrayListOf<Item>()
|
private val browsingCache = hashMapOf<String, BrowseResult>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the pai token, see:
|
* Load the pai token, see:
|
||||||
|
@ -286,18 +286,29 @@ object Crunchyroll {
|
||||||
parameters.add("categories" to categories.joinToString(",") { it.str })
|
parameters.add("categories" to categories.joinToString(",") { it.str })
|
||||||
}
|
}
|
||||||
|
|
||||||
val browseResult: BrowseResult = try {
|
// fetch result if not already cached
|
||||||
requestGet(browseEndpoint, parameters)
|
if (browsingCache.contains(parameters.toString())) {
|
||||||
}catch (ex: SerializationException) {
|
Log.d(TAG, "browse result cached: $parameters")
|
||||||
Log.e(TAG, "SerializationException in browse().", ex)
|
} else {
|
||||||
NoneBrowseResult
|
Log.d(TAG, "browse result not cached, fetching: $parameters")
|
||||||
|
val browseResult: BrowseResult = try {
|
||||||
|
requestGet(browseEndpoint, parameters)
|
||||||
|
}catch (ex: SerializationException) {
|
||||||
|
Log.e(TAG, "SerializationException in browse().", ex)
|
||||||
|
NoneBrowseResult
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
}
|
||||||
|
|
||||||
|
// add results to cache
|
||||||
|
browsingCache[parameters.toString()] = browseResult
|
||||||
}
|
}
|
||||||
|
|
||||||
// add results to cache TODO improve
|
return browsingCache[parameters.toString()] ?: NoneBrowseResult
|
||||||
browsingCache.clear()
|
|
||||||
browsingCache.addAll(browseResult.items)
|
|
||||||
|
|
||||||
return browseResult
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue