Created
June 19, 2018 10:50
-
-
Save ppicas/0f6e43fbb931472e7d512fbc9e065ab6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface JobCache { | |
fun getJob(jobId: String): Maybe<Job> | |
fun putJob(job: Job) | |
} | |
class CachedJobRepository( | |
private val delegate: JobRepository, | |
private val jobCache: JobCache | |
): JobRepository by delegate { | |
override fun getJob(jobId: String): Observable<Job> { | |
return jobCache.getJob(jobId) | |
.switchIfEmpty(delegate.getJob(jobId) | |
.firstOrError() | |
.doOnSuccess { jobCache.putJob(it) }) | |
.toObservable() | |
} | |
override fun getJobs(page: Int): Single<List<Job>> { | |
return delegate.getJobs(status, page).doOnSuccess { jobs -> | |
jobs.forEach { job -> jobCache.putJob(job) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment