Updated on 2026-08-14
This commit is contained in:
parent
a57d077863
commit
11f4759544
13 changed files with 273 additions and 0 deletions
|
|
@ -0,0 +1,20 @@
|
|||
package com.tangem.datasource.di
|
||||
|
||||
import com.tangem.datasource.local.cache.CacheKeysStore
|
||||
import com.tangem.datasource.local.cache.RuntimeCacheKeysStore
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
internal object CacheKeysStoreModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideCacheKeysStore(): CacheKeysStore {
|
||||
return RuntimeCacheKeysStore()
|
||||
}
|
||||
}
|
||||
14
core/datasource/src/main/java/com/tangem/datasource/local/cache/CacheKeysStore.kt
vendored
Normal file
14
core/datasource/src/main/java/com/tangem/datasource/local/cache/CacheKeysStore.kt
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package com.tangem.datasource.local.cache
|
||||
|
||||
import com.tangem.datasource.local.cache.model.CacheKey
|
||||
|
||||
interface CacheKeysStore {
|
||||
|
||||
fun get(id: String): CacheKey?
|
||||
|
||||
fun addOrReplace(key: CacheKey)
|
||||
|
||||
fun remove(id: String)
|
||||
|
||||
fun clear()
|
||||
}
|
||||
25
core/datasource/src/main/java/com/tangem/datasource/local/cache/RuntimeCacheKeysStore.kt
vendored
Normal file
25
core/datasource/src/main/java/com/tangem/datasource/local/cache/RuntimeCacheKeysStore.kt
vendored
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package com.tangem.datasource.local.cache
|
||||
|
||||
import com.tangem.datasource.local.cache.model.CacheKey
|
||||
import com.tangem.datasource.local.store.RuntimeStore
|
||||
|
||||
internal class RuntimeCacheKeysStore : CacheKeysStore {
|
||||
|
||||
private val store = RuntimeStore(keyProvider = CacheKey::id)
|
||||
|
||||
override fun get(id: String): CacheKey? {
|
||||
return store.getSync { it.id == id }.firstOrNull()
|
||||
}
|
||||
|
||||
override fun addOrReplace(key: CacheKey) {
|
||||
store.addOrReplace(key)
|
||||
}
|
||||
|
||||
override fun remove(id: String) {
|
||||
store.remove { it.id == id }
|
||||
}
|
||||
|
||||
override fun clear() {
|
||||
store.clear()
|
||||
}
|
||||
}
|
||||
10
core/datasource/src/main/java/com/tangem/datasource/local/cache/model/CacheKey.kt
vendored
Normal file
10
core/datasource/src/main/java/com/tangem/datasource/local/cache/model/CacheKey.kt
vendored
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package com.tangem.datasource.local.cache.model
|
||||
|
||||
import org.joda.time.Duration
|
||||
import org.joda.time.LocalDateTime
|
||||
|
||||
data class CacheKey(
|
||||
val id: String,
|
||||
val updatedAt: LocalDateTime,
|
||||
val expiresIn: Duration,
|
||||
)
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
package com.tangem.datasource.local.store
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.update
|
||||
|
||||
internal class RuntimeStore<Key, Data>(private val keyProvider: (Data) -> Key) {
|
||||
|
||||
private val store = MutableStateFlow<HashMap<Key, Data>>(hashMapOf())
|
||||
|
||||
fun get(selector: (Data) -> Boolean = { true }): Flow<List<Data>> {
|
||||
return store.map { getInternal(it, selector) }
|
||||
}
|
||||
|
||||
fun getSync(selector: (Data) -> Boolean = { true }): List<Data> {
|
||||
return getInternal(store.value, selector)
|
||||
}
|
||||
|
||||
// TODO: Uncomment if needed
|
||||
// fun addOrReplace(items: Collection<Data>) {
|
||||
// if (items.isEmpty()) return
|
||||
//
|
||||
// val storeValue = store.value
|
||||
//
|
||||
// items.forEach { item ->
|
||||
// storeValue[keyProvider(item)] = item
|
||||
// }
|
||||
//
|
||||
// store.value = storeValue
|
||||
// }
|
||||
|
||||
fun addOrReplace(item: Data) {
|
||||
val storeValue = store.value
|
||||
storeValue[keyProvider(item)] = item
|
||||
|
||||
store.value = storeValue
|
||||
}
|
||||
|
||||
fun remove(selector: (Data) -> Boolean) {
|
||||
val storeValue = store.value
|
||||
|
||||
storeValue.forEach { (key, item) ->
|
||||
if (selector(item)) {
|
||||
storeValue.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
store.value = storeValue
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
store.update { hashMapOf() }
|
||||
}
|
||||
|
||||
private fun getInternal(store: HashMap<Key, Data>, selector: (Data) -> Boolean): List<Data> {
|
||||
return store.values.filter { selector(it) }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue