docs/Examples/CachePolicyPlugin.md
Cache Policies are the rules of how a URL load should be loaded. These rules are determined by the client cache requirements, the server's content expiration requirements, and the server's revalidation requirements.
Moya automatically handles the policies on client's side based on server response policies. There are many times that we want to manually handle the client's side policies either in all our request or on specific targets.
Let's define a CachePolicyGettableType protocol to implement in our Targets:
protocol CachePolicyGettableType {
var cachePolicy: URLRequest.CachePolicy? { get }
}
Let's define our plugin:
final class CachePolicyPlugin: PluginType {
func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
guard let policyGettable = target as? CachePolicyGettableType, let policy = policyGettable.policy else {
return request
}
var mutableRequest = request
mutableRequest.cachePolicy = policy
return mutableRequest
}
}
We need to add the CachePolicyPlugin to our MoyaProvider:
let provider = MoyaProvider<RequestTarget>(plugins: [CachePolicyPlugin()])
Also, our targets need to conform to our CachePolicyGettableType protocol:
extension RequestTarget: CachePolicyGettableType {
var cachePolicy: URLRequest.CachePolicy? {
.reloadIgnoringLocalCacheData
}
}
It's important to keep in mind that if we are using MultiTarget in our Moya Provider to use any target, we need to extend MultiTarget for it to conform our CachePolicyGettableType:
extension MultiTarget: CachePolicyGettableType {
public var policy: URLRequest.CachePolicy? {
// Validates if target conforms CachePolicyGettableType protocol
guard let policyTarget = target as? CachePolicyGettableType else {
return nil
}
return policyTarget.policy
}
}
This example is based on Frederick Pietschmann CachePolicyPlugin proposal in the Issue #1679