Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pircate/moyacache
https://github.com/pircate/moyacache
Last synced: 26 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/pircate/moyacache
- Owner: Pircate
- License: mit
- Created: 2019-04-23T03:08:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-07-06T05:57:35.000Z (over 5 years ago)
- Last Synced: 2024-11-18T04:44:36.725Z (about 1 month ago)
- Language: Swift
- Size: 292 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MoyaCache
[![CI Status](https://img.shields.io/travis/Pircate/MoyaCache.svg?style=flat)](https://travis-ci.org/Pircate/MoyaCache)
[![Version](https://img.shields.io/cocoapods/v/MoyaCache.svg?style=flat)](https://cocoapods.org/pods/MoyaCache)
[![License](https://img.shields.io/cocoapods/l/MoyaCache.svg?style=flat)](https://cocoapods.org/pods/MoyaCache)
[![Platform](https://img.shields.io/cocoapods/p/MoyaCache.svg?style=flat)](https://cocoapods.org/pods/MoyaCache)
[![codebeat badge](https://codebeat.co/badges/2a844bc6-5a32-4f9c-9585-f34aae5ab6c3)](https://codebeat.co/projects/github-com-pircate-moyacache-master)## 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](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'MoyaCache'
```## Usage
* 实现缓存协议
```swift
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().object(forKey: key.stringValue)
}
public func storeCachedResponse(_ cachedResponse: Moya.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` 选择过期时间
```swift
extension StoryAPI: Cacheable {
var expiry: Expiry {
return .never
}
}
```* 读取缓存
```swift
let cachedResponse = try target.cachedResponse()
```* 需要缓存的请求调用 `.cache`,普通请求不会缓存
```swift
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.