Updated on 2026-08-14

This commit is contained in:
Tangem 2023-09-21 20:15:54 +03:00
parent 613e07734e
commit 3fc34fac64
103 changed files with 403 additions and 297 deletions

View file

@ -1 +0,0 @@
<manifest package="com.tangem.data.common" />

View file

@ -4,6 +4,7 @@ import com.tangem.datasource.local.cache.CacheKeysStore
import com.tangem.datasource.local.cache.model.CacheKey
import org.joda.time.Duration
import org.joda.time.LocalDateTime
import timber.log.Timber
internal class DefaultCacheRegistry(
private val cacheKeysStore: CacheKeysStore,
@ -18,10 +19,12 @@ internal class DefaultCacheRegistry(
}
override suspend fun invalidate(key: String) {
Timber.d("Invalidate the cache key: $key")
cacheKeysStore.remove(key)
}
override suspend fun invalidateAll() {
Timber.d("Invalidate all cache keys")
cacheKeysStore.clear()
}
@ -29,11 +32,19 @@ internal class DefaultCacheRegistry(
key: String,
skipCache: Boolean,
expireIn: Duration,
block: suspend () -> Unit,
action: suspend () -> Unit,
) {
val isExpired = isExpired(key) || skipCache
if (!isExpired) return
try {
Timber.d("Invoke the action associated with the cache key: $key")
action()
} catch (e: Throwable) {
Timber.w(e, "The action related to the cache key has failed: $key")
throw e
}
cacheKeysStore.store(
key = CacheKey(
id = key,
@ -41,12 +52,5 @@ internal class DefaultCacheRegistry(
expiresIn = expireIn,
),
)
try {
block()
} catch (e: Throwable) {
invalidate(key)
throw e
}
}
}