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.myDataSource.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.myDataSource.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
dependencies {
maven { url("https://s3.amazonaws.com/mirego-maven/public") }
}
ios() {
binaries {
framework {
export "com.mirego.trikot:datasources-streams:$trikot_version"
}
}
}
sourceSets {
commonMain {
dependencies {
implementation "com.mirego.trikot:datasources-streams:$trikot_version"
}
}
}