Rework: Make RawCacheStrategy class more generic, to use it for other SDK API

This commit is contained in:
Benoit Marty 2020-12-03 13:43:04 +01:00
parent cafe86e675
commit 3e563a37a2
5 changed files with 21 additions and 19 deletions

View File

@ -17,7 +17,7 @@ Translations 🗣:
-
SDK API changes ⚠️:
-
- RawCacheStrategy has been moved and renamed to CacheStrategy
Build 🧱:
-

View File

@ -14,16 +14,16 @@
* limitations under the License.
*/
package org.matrix.android.sdk.api.raw
package org.matrix.android.sdk.api.cache
sealed class RawCacheStrategy {
sealed class CacheStrategy {
// Data is always fetched from the server
object NoCache : RawCacheStrategy()
object NoCache : CacheStrategy()
// Once data is retrieved, it is stored for the provided amount of time.
// In case of error, and if strict is set to false, the cache can be returned if available
data class TtlCache(val validityDurationInMillis: Long, val strict: Boolean) : RawCacheStrategy()
data class TtlCache(val validityDurationInMillis: Long, val strict: Boolean) : CacheStrategy()
// Once retrieved, the data is stored in cache and will be always get from the cache
object InfiniteCache : RawCacheStrategy()
object InfiniteCache : CacheStrategy()
}

View File

@ -16,6 +16,8 @@
package org.matrix.android.sdk.api.raw
import org.matrix.android.sdk.api.cache.CacheStrategy
/**
* Useful methods to fetch raw data from the server. The access token will not be used to fetched the data
*/
@ -23,7 +25,7 @@ interface RawService {
/**
* Get a URL, either from cache or from the remote server, depending on the cache strategy
*/
suspend fun getUrl(url: String, rawCacheStrategy: RawCacheStrategy): String
suspend fun getUrl(url: String, cacheStrategy: CacheStrategy): String
/**
* Specific case for the well-known file. Cache validity is 8 hours

View File

@ -18,7 +18,7 @@ package org.matrix.android.sdk.internal.raw
import com.zhuinden.monarchy.Monarchy
import okhttp3.ResponseBody
import org.matrix.android.sdk.api.raw.RawCacheStrategy
import org.matrix.android.sdk.api.cache.CacheStrategy
import org.matrix.android.sdk.internal.database.model.RawCacheEntity
import org.matrix.android.sdk.internal.database.query.get
import org.matrix.android.sdk.internal.database.query.getOrCreate
@ -32,7 +32,7 @@ import javax.inject.Inject
internal interface GetUrlTask : Task<GetUrlTask.Params, String> {
data class Params(
val url: String,
val rawCacheStrategy: RawCacheStrategy
val cacheStrategy: CacheStrategy
)
}
@ -42,14 +42,14 @@ internal class DefaultGetUrlTask @Inject constructor(
) : GetUrlTask {
override suspend fun execute(params: GetUrlTask.Params): String {
return when (params.rawCacheStrategy) {
RawCacheStrategy.NoCache -> doRequest(params.url)
is RawCacheStrategy.TtlCache -> doRequestWithCache(
return when (params.cacheStrategy) {
CacheStrategy.NoCache -> doRequest(params.url)
is CacheStrategy.TtlCache -> doRequestWithCache(
params.url,
params.rawCacheStrategy.validityDurationInMillis,
params.rawCacheStrategy.strict
params.cacheStrategy.validityDurationInMillis,
params.cacheStrategy.strict
)
RawCacheStrategy.InfiniteCache -> doRequestWithCache(
CacheStrategy.InfiniteCache -> doRequestWithCache(
params.url,
Long.MAX_VALUE,
true

View File

@ -16,7 +16,7 @@
package org.matrix.android.sdk.internal.raw
import org.matrix.android.sdk.api.raw.RawCacheStrategy
import org.matrix.android.sdk.api.cache.CacheStrategy
import org.matrix.android.sdk.api.raw.RawService
import java.util.concurrent.TimeUnit
import javax.inject.Inject
@ -25,15 +25,15 @@ internal class DefaultRawService @Inject constructor(
private val getUrlTask: GetUrlTask,
private val cleanRawCacheTask: CleanRawCacheTask
) : RawService {
override suspend fun getUrl(url: String, rawCacheStrategy: RawCacheStrategy): String {
return getUrlTask.execute(GetUrlTask.Params(url, rawCacheStrategy))
override suspend fun getUrl(url: String, cacheStrategy: CacheStrategy): String {
return getUrlTask.execute(GetUrlTask.Params(url, cacheStrategy))
}
override suspend fun getWellknown(userId: String): String {
val homeServerDomain = userId.substringAfter(":")
return getUrl(
"https://$homeServerDomain/.well-known/matrix/client",
RawCacheStrategy.TtlCache(TimeUnit.HOURS.toMillis(8), false)
CacheStrategy.TtlCache(TimeUnit.HOURS.toMillis(8), false)
)
}