2016-08-18 77 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

不錯,我只是很快看了一下,對不起。 –