Browse Source

implement runtime cache for Crunchyroll.browse()

develop
Jannik 3 months ago
parent
commit
7d6c300f7e
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
  1. 33
      app/src/main/java/org/mosad/teapod/parser/crunchyroll/Crunchyroll.kt

33
app/src/main/java/org/mosad/teapod/parser/crunchyroll/Crunchyroll.kt

@ -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,18 +286,29 @@ object Crunchyroll {
parameters.add("categories" to categories.joinToString(",") { it.str })
}
val browseResult: BrowseResult = try {
requestGet(browseEndpoint, parameters)
}catch (ex: SerializationException) {
Log.e(TAG, "SerializationException in browse().", ex)
NoneBrowseResult
}
// 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) {
Log.e(TAG, "SerializationException in browse().", ex)
NoneBrowseResult
}
// add results to cache TODO improve
browsingCache.clear()
browsingCache.addAll(browseResult.items)
// 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
}
return browseResult
return browsingCache[parameters.toString()] ?: NoneBrowseResult
}
/**

Loading…
Cancel
Save

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close