Moya: Add a CachePolicyPlugin

Created on 6 Jun 2018  路  5Comments  路  Source: Moya/Moya

Reasoning

Maybe it's just me, but I believe that Moya should by default include a plugin to control cache policy. Depending on the web interface Moya is used to interact with, this may be a required thing quite often.

I'm not familiar with the philosophy behind what plugins should be included by default, so correct me, if it isn't a good fit.

Proposed Implementation

While the credentials plugin is e. g. realised with a closure being passed to the plugin on initialisation, for the cache policy, I think this approach would undermine the concept of keeping "definitions" what a target type does in a separate scope. This leads me to the following suggestion for an implementation:

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
    }
}

One could then make the Moya target type conform to CachePolicyGettable and define cache policies for each individual target.

What do you think?

enhancement

All 5 comments

Hey @fredpi 馃憢,

This was discussed in:

1251, #713, and #197

In my opinion, caching is too client specific to be in scope for Moya. It would be better to let a caching library focus on this end of things. I think something great about Moya is that it makes things like this easy to do with the PluginType system as you demo'd above.

@SD10 Thanks for your fast reply!

Maybe I'm taking an odd stance, but for me, setting the cache policy is not the same as real cache handling as discussed in the attached issues. Actually, this plugin is just about associating a cache policy which is already built-in into URLRequest with a Moya target. As such, it doesn't differentiate much from the authentication plugin natively integrated into Moya.

Do you agree in that the plugin discussed here isn't a "real" caching extension with custom logic? If yes, where should one draw the line between what URLRequest configuration plugins should be integrated within Moya and which plugins should be created for each individual project?

@fredpi Thanks for clarifying, I agree with you 馃憤 In my opinion, I just want to keep the Moya codebase small. I feel that Moya is in the business of making something like plugins possible, and consumers of the library should be responsible for writing them. Otherwise, we're just going to end up maintaining a bunch of different plugins.

If it was a plugin that was really difficult to write and widely used, I'd consider voting to add it to Moya, but I don't see that in this situation. Maybe another Moya contributor has thoughts on this issue.

@SD10 Only including plugins that are very widely used and / or are very difficult to write makes sense to me as a definition as to what plugins to include 馃憤

This being said, the CachePolicyPlugin doesn't really fit here. Closing...

@SD10 totally agree with you that less is more and the codebase should be kept small. But as you said, the cache policy plugin is pretty straightforward to implement and will not require lot of maintenance. So the cost of including it is low.
But I see the benefit which was not mentioned here. You said that it's not widely used plugin. Well, that's sad. Caching improves UX in may ways (saves user's time and traffic). By reducing the friction of implementing the caching Moya will promote this feature among developers and will contribute into overall user experience.

Was this page helpful?
0 / 5 - 0 ratings