使用Moya缓存处理

时间:2018-01-30 08:16:41

标签: swift alamofire moya

我们在项目中实施了Moya,RxSwift和Alamofire作为pod。

有没有人知道如何使用此技术控制每个网址请求的缓存策略?

我已经阅读了Moya GitHub页面上的一些问题但仍无法找到任何内容。还尝试使用实际的json响应存储为sampleData的文件,如下所示:

var sampleData: Data {
    guard let path = Bundle.main.path(forResource: "SampleAggregate", ofType: "txt") else {
        return "sampleData".utf8Encoded
    }
    let sample = try? String(contentsOfFile: path, encoding: String.Encoding.utf8)
    return sample!.utf8Encoded

}

提前感谢任何专业提示:)

4 个答案:

答案 0 :(得分:7)

至于我的理解,"最干净的"解决这个问题的方法,就是使用自定义的Moya插件。这是一个实现:

protocol CachePolicyGettable {
    var cachePolicy: URLRequest.CachePolicy { get }
}  

final class CachePolicyPlugin: PluginType {
    public func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
        if let cachePolicyGettable = target as? CachePolicyGettable {
            var mutableRequest = request
            mutableRequest.cachePolicy = cachePolicyGettable.cachePolicy
            return mutableRequest
        }

        return request
    }
}

要实际使用此插件,还有两个必需步骤:

  1. 应该将插件添加到您的Moya提供程序中,如下所示:

    let moyaProvider = MoyaProvider<YourMoyaTarget>(plugins: [CachePolicyPlugin()])
    
  2. YourMoyaTarget应符合CachePolicyGettable,从而为每个目标定义缓存策略:

    extension YourMoyaTarget: CachePolicyGettable {
        var cachePolicy: URLRequest.CachePolicy {
            switch self {
            case .sampleTarget, .someOtherSampleTarget:
                return .reloadIgnoringLocalCacheData
    
            default:
                return .useProtocolCachePolicy
            }
        }
    }
    
  3. 注意:此方法使用协议将缓存策略与目标类型相关联;也可以通过传递给插件的闭包来实现这一点。这样的闭包将决定使用哪个缓存策略,具体取决于作为闭包的输入参数传递的目标类型。

答案 1 :(得分:2)

子类MoyaProvider并撰写requestClosure

它应该类似于:

final class MyProvider<Target: TargetType>: MoyaProvider<Target> {

    public init(
        endpointClosure: @escaping EndpointClosure = MoyaProvider.defaultEndpointMapping,
        requestClosure: @escaping RequestClosure = MoyaProvider.defaultRequestMapping,
        stubClosure: @escaping StubClosure = MoyaProvider.neverStub,
        manager: Manager = MoyaProvider<Target>.defaultAlamofireManager(),
        plugins: [PluginType] = [],
        trackInflights: Bool = false
    ) {
        super.init(
            endpointClosure: endpointClosure,
            requestClosure: { endpoint, closure in
                var request = try! endpoint.urlRequest() //Feel free to embed proper error handling
                if request.url == URL(string: "http://google.com")! {
                    request.cachePolicy = .returnCacheDataDontLoad
                } else {
                    request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
                }
                closure(.success(request))
            },
            stubClosure: stubClosure,
            manager: manager,
            plugins: plugins,
            trackInflights: trackInflights
        )
    }

}

答案 2 :(得分:2)

基于@fredpi的答案,我对Moya的缓存插件进行了一些改进。以下是我的版本:

import Foundation
import Moya

protocol CachePolicyGettable {
    var cachePolicy: URLRequest.CachePolicy { get }
}

final class NetworkDataCachingPlugin: PluginType {

    init (configuration: URLSessionConfiguration, inMemoryCapacity: Int, diskCapacity: Int, diskPath: String?) {
        configuration.urlCache = URLCache(memoryCapacity: inMemoryCapacity, diskCapacity: diskCapacity, diskPath: diskPath)
    }

    func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
        if let cacheableTarget = target as? CachePolicyGettable {
            var mutableRequest = request
            mutableRequest.cachePolicy = cacheableTarget.cachePolicy
            return mutableRequest
        }
        return request
    }
}

extension NetworkApiService: CachePolicyGettable {
    var cachePolicy: URLRequest.CachePolicy {
        switch self {
        case .getUserProfile:
            return .returnCacheDataElseLoad
        default:
            return .useProtocolCachePolicy
        }
    }
}

为了清除缓存,您需要访问urlRequest对象。如何在following主题中找到如何查询Moya路线的urlRequest。

要清除缓存,可以使用以下代码:

public func clearCache(urlRequests: [URLRequest] = []) {
    let provider = ... // your Moya provider
    guard let urlCache = provider.manager.session.configuration.urlCache else { return }
    if urlRequests.isEmpty {
        urlCache.removeAllCachedResponses()
    } else {
        urlRequests.forEach { urlCache.removeCachedResponse(for: $0) }
    }
}

答案 3 :(得分:0)

如果您也想禁用存储的cookie

request.httpShouldHandleCookies = false