2016-08-18 67 views
2

我使用EVURLCache即使缓存控制和附注头被定义POST请求响应缓存:如何防止EVURLCache使用Cache-Control标头缓存请求?

Headers: [ 
    Access-Control-Allow-Headers : Content-Type, Accept, X-Requested-With, remember-me, x-auth-token 
    Content-Type : application/json;charset=UTF-8 
    Access-Control-Max-Age : 3600 
    Cache-Control : no-cache, no-store, max-age=0, must-revalidate 
    Server : nginx/1.9.15 
    Connection : keep-alive 
    Transfer-Encoding : Identity 
    X-XSS-Protection : 1; mode=block 
    X-Content-Type-Options : nosniff 
    Expires : 0 
    X-Application-Context : application:dev,docker:8080 
    Pragma : no-cache 
    Access-Control-Allow-Methods : POST, GET, OPTIONS, DELETE, PUT 
    Date : Thu, 18 Aug 2016 13:57:15 GMT 
    Access-Control-Allow-Origin : * 
    Access-Control-Allow-Credentials : true 
] 

如何强制EVURLCache不缓存这样的要求?

我用下面的代码初始化缓存在我的应用程序委托:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    // configuring cache 
    EVURLCache.LOGGING = true // We want to see all caching actions 
    EVURLCache.activate() 

    return true 
} 

注意,我知道过滤器,但我不知道,如果我们可以告诉它跟随的响应头建议缓存行为

+0

用相关代码更新您的问题。 – rmaddy

+0

更新为AppDelegate代码 –

+2

如果发生完全相同的问题,请与EVURLCache的创建者打开一个问题 https://github.com/evermeer/EVURLCache/issues – SoundShock

回答

1

我创建了一个更新(尚未推送到GitHub)并希望得到您的意见。为定为这的代码是:

var shouldSkipCache: String? = nil 

    // check if caching is allowed according to the request 
    if request.cachePolicy == NSURLRequestCachePolicy.ReloadIgnoringCacheData { 
     shouldSkipCache = "request cache policy" 
    } 

    // check if caching is allowed according to the response Cache-Control or Pragma header 
    if let httpResponse = cachedResponse.response as? NSHTTPURLResponse { 
     if let cacheControl = httpResponse.allHeaderFields["Cache-Control"] as? String { 
      if cacheControl.lowercaseString.containsString("no-cache") || cacheControl.lowercaseString.containsString("no-store") { 
       shouldSkipCache = "response cache control" 
      } 
     } 

     if let cacheControl = httpResponse.allHeaderFields["Pragma"] as? String { 
      if cacheControl.lowercaseString.containsString("no-cache") { 
       shouldSkipCache = "response pragma" 
      } 
     } 
    } 

    if shouldSkipCache != nil { 
     // If the file is in the PreCache folder, then we do want to save a copy in case we are without internet connection 
     let storagePath = EVURLCache.storagePathForRequest(request, rootPath: EVURLCache._preCacheDirectory) ?? "" 
     if !NSFileManager.defaultManager().fileExistsAtPath(storagePath) { 
      EVURLCache.debugLog("CACHE not storing file, it's not allowed by the \(shouldSkipCache) : \(request.URL)") 
      return 
     } 
     EVURLCache.debugLog("CACHE file in PreCache folder, overriding \(shouldSkipCache) : \(request.URL)") 
    } 

总之,这意味着:

  • 如果在指示缓存的数据应该beignored则响应不会被写入到高速缓存中的请求创建
  • 如果响应的Cache-Control头包含no-cache或no-store,则不会将响应写入缓存
  • 如果响应的Pragma头包含no-cache,则响应将会不会写入缓存
  • 如果文件已经在缓存中,则以上所有将被忽略。 (那么至少应该更新)
+0

我会尽快检查。我认为这是一个好的开始,但可能需要做更多的工作才能完全符合http cache headers规范,特别是关于未在此处检查的缓存TTL。 –

+1

已在cachedResponseForRequest方法中检查TTL。请参阅cacheItemExpired函数。 –

+0

不错,我只是很快看了一下,对不起。 –