CombineCacheMap
描述
缓存/memoize Combine.Publishers
的输出。也适用于 RxSwift:https://github.com/BrianSemiglia/RxCacheMap
使用
除了缓存,所有功能与它们的非缓存 Combine 对应函数表现相同。
events.cacheMap { x -> Value in
// Closure executed once per unique `x`, replayed when not unique.
expensiveOperation(x)
}
events.cacheMap(whenExceeding: .seconds(1)) { x -> Value in
// Closure executed once per unique `x`, replayed when operation of unique value took
// longer than specified duration.
expensiveOperation(x)
}
events.cacheFlatMap(cache: .memoryRefreshingAfter()) { x -> AnyPublisher<Expiring<Value>, Failure> 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 map execution.
expensiveOperation(x).map { output in
Expiring(
value: output,
expiration: Date() + hours(1)
)
}
}
// Use your own cache or the included .memory and .disk stores.
events.cacheMap(cache: MyCache()) { x -> Value in
expensiveOperation(x)
}
安装
CombineCacheMap 以 Swift Package 的形式提供。
作者
许可
CombineCacheMap 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。