RxCacheMap
Description
Cache/memoize the output of RxSwift.Observables
using cacheMap, cacheFlatMap, cacheFlatMapLatest and cacheFlatMapUntilExpired.
Usage
Aside from caching all functions work like their non-cache Rx-counterparts.
events.cacheMap { x -> Result in
// Closure executed once per unique `x`, replayed when not unique.
expensiveOperation(x)
}
events.cacheMap(whenExceeding: .seconds(1)) { x -> Result in
// Closure executed once per unique `x`, replayed when operation of unique value took
// longer than specified duration.
expensiveOperation(x)
}
events.cacheFlatMapInvalidatingOn { x -> Observable<(Result, Date)> in
// Closure executed once per unique `x`, replayed when input not unique. Cache
// invalidated when date returned is greater than or equal to date of event.
expensiveOperation(x).map { output in
return (output, Date() + hours(1))
}
}
// You can provide your own cache (disk, in-memory, etc.). NSCache is the default.
events.cacheMap(cache: MyCache()) { x -> Result in
expensiveOperation(x)
}
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
RxCacheMap is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RxCacheMap'
Author
License
RxCacheMap is available under the MIT license. See the LICENSE file for more info.