https://github.com/pircate/rxmoyacache
https://github.com/pircate/rxmoyacache
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pircate/rxmoyacache
- Owner: Pircate
- License: mit
- Created: 2018-09-10T14:44:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-06T06:54:32.000Z (over 7 years ago)
- Last Synced: 2025-05-07T01:06:31.309Z (about 1 year ago)
- Language: Swift
- Size: 441 KB
- Stars: 13
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RxMoyaCache
[](https://travis-ci.org/Pircate/RxMoyaCache)
[](https://cocoapods.org/pods/RxMoyaCache)
[](https://github.com/Carthage/Carthage)
[](https://cocoapods.org/pods/RxMoyaCache)
[](https://cocoapods.org/pods/RxMoyaCache)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## Requirements
## Installation
RxMoyaCache is available through [CocoaPods](https://cocoapods.org) or [Carthage](https://github.com/Carthage/Carthage). To install
it, simply add the following line to your Podfile or Cartfile:
### CocoaPods
```ruby
pod 'RxMoyaCache'
```
### Carthage
```ruby
github "Pircate/RxMoyaCache"
```
## Usage
### Import
```swift
import RxMoyaCache
```
### Snippet
给 `Storable` 添加默认实现,具体实现请看 Demo
```swift
extension Storable where Self: TargetType {
public var allowsStorage: (Response) -> Bool {
return MoyaCache.shared.storagePolicyClosure
}
public func cachedResponse(for key: CachingKey) throws -> Response {
return try Storage().object(forKey: key.stringValue)
}
public func storeCachedResponse(_ cachedResponse: Response, for key: CachingKey) throws {
try Storage().setObject(cachedResponse, forKey: key.stringValue)
}
public func removeCachedResponse(for key: CachingKey) throws {
try Storage().removeObject(forKey: key.stringValue)
}
public func removeAllCachedResponses() throws {
try Storage().removeAll()
}
}
```
`target` 遵循 `Cacheable` 协议
```swift
enum StoryAPI: TargetType, Cacheable {
case latest
}
```
读取缓存
```swift
let provider = MoyaProvider()
provider.rx.cache
.request(.latest)
.map(StoryListModel.self)
.subscribe(onNext: { object in
debugPrint("onNext:", object.topStories[0].title)
}).disposed(by: disposeBag)
// or
provider.rx.onCache(.latest, type: StoryListModel.self) { object in
debugPrint("onCache", object.topStories[0].title)
}.request()
.subscribe(onSuccess: { object in
debugPrint("onSuccess", object.topStories[0].title)
}).disposed(by: disposeBag)
```
## Author
Pircate, gao497868860@163.com
## License
RxMoyaCache is available under the MIT license. See the LICENSE file for more info.