Latest 0.2.1
Homepage https://github.com/Pircate/RxMoyaCache
License MIT
Platforms ios 9.0
Dependencies Moya/RxSwift
Authors

CI Status
Version
Carthage compatible
License
Platform

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 or Carthage. To install
it, simply add the following line to your Podfile or Cartfile:

CocoaPods

pod 'RxMoyaCache'

Carthage

github "Pircate/RxMoyaCache"

Usage

Import

import RxMoyaCache

Snippet

Storable 添加默认实现,具体实现请看 Demo

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<Moya.Response>().object(forKey: key.stringValue)
    }

    public func storeCachedResponse(_ cachedResponse: Response, for key: CachingKey) throws {
        try Storage<Moya.Response>().setObject(cachedResponse, forKey: key.stringValue)
    }

    public func removeCachedResponse(for key: CachingKey) throws {
        try Storage<Moya.Response>().removeObject(forKey: key.stringValue)
    }

    public func removeAllCachedResponses() throws {
        try Storage<Moya.Response>().removeAll()
    }
}

target 遵循 Cacheable 协议

enum StoryAPI: TargetType, Cacheable {
    case latest
}

读取缓存

let provider = MoyaProvider<StoryAPI>()
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, [email protected]

License

RxMoyaCache is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "RxMoyaCache",
    "version": "0.2.1",
    "summary": "A network cache library based on RxSwift/Moya/Cache.",
    "homepage": "https://github.com/Pircate/RxMoyaCache",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Pircate": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Pircate/RxMoyaCache.git",
        "tag": "0.2.1"
    },
    "platforms": {
        "ios": "9.0"
    },
    "swift_version": "4.2",
    "source_files": "RxMoyaCache/Classes/**/*",
    "dependencies": {
        "Moya/RxSwift": []
    }
}

Pin It on Pinterest

Share This