Latest 0.2.0
Homepage https://github.com/Pircate/MoyaCache
License MIT
Platforms ios 9.0
Dependencies Moya
Authors

CI Status
Version
License
Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.0
  • Swift 4.2

Installation

MoyaCache is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'MoyaCache'

Usage

  • 实现缓存协议
extension Storable {

    typealias CachedResponse = Moya.Response

    public var allowsStorage: (Moya.Response) -> Bool {
        return { $0.statusCode == 200 }
    }

    public func cachedResponse(for key: CachingKey) throws -> Moya.Response {
        return try Storage<Moya.Response>().object(forKey: key.stringValue)
    }

    public func storeCachedResponse(_ cachedResponse: Moya.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 选择过期时间
extension StoryAPI: Cacheable {

    var expiry: Expiry {
        return .never
    }
}
  • 读取缓存
let cachedResponse = try target.cachedResponse()
  • 需要缓存的请求调用 .cache,普通请求不会缓存
provider.cache.request(target) { result in

}

Author

Pircate, [email protected]

License

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

Latest podspec

{
    "name": "MoyaCache",
    "version": "0.2.0",
    "summary": "A cache protocol for Moya.",
    "homepage": "https://github.com/Pircate/MoyaCache",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Pircate": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Pircate/MoyaCache.git",
        "tag": "0.2.0"
    },
    "platforms": {
        "ios": "9.0"
    },
    "swift_versions": "4.2",
    "source_files": "MoyaCache/Classes/**/*",
    "dependencies": {
        "Moya": []
    }
}

Pin It on Pinterest

Share This