@@ -9,11 +9,14 @@ public struct CacheMode {
9
9
public let returnCachedResponse : Bool
10
10
/// If true, HttpClient will invoke request
11
11
public let invokeRequest : Bool
12
+ /// Specifies which HTTP methods should be cached
13
+ public let cacheHttpMethods : Set < HttpMethod >
12
14
13
- public init ( cacheResponse: Bool = true , returnCachedResponse: Bool = true , invokeRequest: Bool = true ) {
15
+ public init ( cacheResponse: Bool = true , returnCachedResponse: Bool = true , invokeRequest: Bool = true , cacheHttpMethods : [ HttpMethod ] = [ . get ] ) {
14
16
self . cacheResponse = cacheResponse
15
17
self . returnCachedResponse = returnCachedResponse
16
18
self . invokeRequest = invokeRequest
19
+ self . cacheHttpMethods = Set ( cacheHttpMethods)
17
20
}
18
21
19
22
/// Only cached response will be returned
@@ -26,6 +29,14 @@ public struct CacheMode {
26
29
public static let `default` = CacheMode ( cacheResponse: true , returnCachedResponse: true , invokeRequest: true )
27
30
}
28
31
32
+ extension CacheMode {
33
+ func shouldCache( _ request: URLRequest ) -> Bool {
34
+ guard let raw = request. httpMethod else { return false }
35
+ guard let method = HttpMethod ( rawValue: raw) else { return false }
36
+ return cacheHttpMethods. contains ( method)
37
+ }
38
+ }
39
+
29
40
public extension HttpClientType {
30
41
/**
31
42
Creates StreamDataTask
@@ -165,9 +176,9 @@ public extension HttpClientType {
165
176
let dataCacheProvider = MemoryDataCacheProvider ( uid: UUID ( ) . uuidString)
166
177
// variable for response with error
167
178
var errorResponse : HTTPURLResponse ? = nil
168
-
179
+
169
180
let cachedRequest : Observable < Data > = {
170
- if urlRequest . httpMethod == HttpMethod . get . rawValue , requestCacheMode. returnCachedResponse, let url = urlRequest. url, let cached = urlRequestCacheProvider? . load ( resourceUrl: url) {
181
+ if requestCacheMode . shouldCache ( urlRequest ) , requestCacheMode. returnCachedResponse, let url = urlRequest. url, let cached = urlRequestCacheProvider? . load ( resourceUrl: url) {
171
182
// return cached response
172
183
return Observable . just ( cached)
173
184
}
@@ -199,7 +210,7 @@ public extension HttpClientType {
199
210
guard let errorResponse = errorResponse else {
200
211
let requestData = dataCacheProvider. getData ( )
201
212
202
- if urlRequest . httpMethod == HttpMethod . get . rawValue , requestCacheMode. cacheResponse, let url = urlRequest. url {
213
+ if requestCacheMode . shouldCache ( urlRequest ) , requestCacheMode. cacheResponse, let url = urlRequest. url {
203
214
// sache response
204
215
self ? . urlRequestCacheProvider? . save ( resourceUrl: url, data: requestData)
205
216
}
0 commit comments