Skip to content

Instantly share code, notes, and snippets.

@ppicas
Created June 19, 2018 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppicas/0f6e43fbb931472e7d512fbc9e065ab6 to your computer and use it in GitHub Desktop.
Save ppicas/0f6e43fbb931472e7d512fbc9e065ab6 to your computer and use it in GitHub Desktop.
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