See the new trikot repository for releases starting from 3.0.0.
Incubating
Multiplaform cache layers abstraction.
- Interfaces unifying accessing cachable data
- Abstract implementation for implementers
- Simple memory cache without invalidation
- 1 datasource by data type to allow cache tuning
- Cascading approach
MasterDataSource
->FirstCacheLayer
->SecondCacheLayer
(Ex:Http
->Memory
->Disk
) - Request sent to datasource can specify if cache can be used, if not, cache will only be use if cache refresh fails
- Simple invalidation process
data class MyDataSourceRequest<T>(
val query: MySpecialQueryType,
override val cachableId: String,
override val requestType: DataSourceRequest.Type = DataSourceRequest.Type.USE_CACHE
) : DataSourceRequest
class MyDataSource<T>(
cacheDataSource: DataSource<MyDataSourceRequest<T>, T>? = null
) : BaseDataSource<MyDataSourceRequest<T>, T>(cacheDataSource) {
override fun internalRead(request: MyDataSourceRequest<T>): ExecutablePublisher<T> {
val myExecutablePublisher = new MyExecutablePublisher(request.query)
return myExecutablePublisher
}
override fun save(request: MyDataSourceRequest<T>, data: T?) {
// Save content to cache if needed
}
}
In this example we will use memory cache
val myDataSource = MyDataSource<String>(MemoryCacheDataSource())
In this example we will use memory cache
val request = myDataSource.myMyDataSource.read(MyDataSourceRequest(query, "queryCacheId"))
request.subscribe(cancelableManager) {
print(it)
}
To invalidate data, simply send a get request using REFRESH_CACHE. No subscription is needed for the invalidation to happens
myDataSource.myMyDataSource.read(MyDataSourceRequest(query, "queryCacheId", DataSourceRequest.Type.REFRESH_CACHE))
InvalidatableDataSource
is a special wrapper around a MasterDataSource
that invalidate all cached elements when the associated publishers publish true
.
val invalidatingPublisher = Publishers.behaviorSubject()
val invalidatableDatasource = InvalidatableDataSource(myDataSource, invalidatingPublisher)
invalidatingPublisher.value = true // Will invalidate the whole myDataSource cache
api "com.mirego.trikot:datasources:$trikot_datasources_version"
jvm "com.mirego.trikot:datasources-jvm:$trikot_datasources_version"
js "com.mirego.trikot:datasources-js:$trikot_datasources_version"
iosx64 "com.mirego.trikot:datasources-iosx64:$trikot_datasources_version"
iosarm64 "com.mirego.trikot:datasources-iosarm64:$trikot_datasources_version"
Trikot.streams is © 2019-2020 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md
file.
Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We’re a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.
We also love open-source software and we try to give back to the community as much as we can.