evermeer / EVURLCache

a NSURLCache subclass for handling all web requests that use NSURLRequest
Other
297 stars 49 forks source link

Question: How to force reload request and expire existing cache? #47

Open alexookah opened 7 years ago

alexookah commented 7 years ago

Hi, i am using this in my app and i would like to know how can i force reload a request to skip any existing caches and update the content view with the latest content?

evermeer commented 7 years ago

Hi,

You have a lot of options here...

If you want to clean up the cache and remove old items, then you can use the cleanExpiredCaches function.

You could temporarily disable the cache by resetting it to the default. URLCache.shared = URLCache()

You could create a NSMutableURLRequest with the correct caching policy. For more info see https://developer.apple.com/documentation/foundation/nsmutableurlrequest and https://developer.apple.com/documentation/foundation/nsurlrequest.cachepolicy

You could disable the cache for a session using:

let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
config.urlCache = nil
let session = URLSession.init(configuration: config)

You could verride the willCacheResponse function for the NSURLConnectionDelegate and return nil. See: https://developer.apple.com/documentation/foundation/nsurlconnectiondatadelegate/1414834-connection

You could just remove the cached file by getting the path from EVURLCache and then removing that file. You could do that by using something like:

let path = EVURLRequest.storagePathForRequest(theRequest)
try? Filemanager.default.removeItem(atPath: path)
alexookah commented 7 years ago

i ended up using with alamofire .reloadIgnoringCacheData. https://stackoverflow.com/questions/32199494/how-to-disable-caching-in-alamofire

this would try to fetch a request by ignoring cache and if there is internet connection it would reload latest content and rewrite cache to the latest content, correct?

evermeer commented 7 years ago

Yes, that's correct.

alexookah commented 7 years ago

i am making the request ignoring cache but it still is showing my cached content. do i have to delete the cache as well?

alexookah commented 7 years ago

EVURLCache.swift(145) cachedResponse(for:): Returning cached data from .......index.html

EVURLCache.swift(237) storeCachedResponse(_:for:): CACHE not rewriting stored file

EVURLCache.swift(215) storeCachedResponse(_:for:): CACHE not storing file, it's not allowed by the Optional("request cache policy") : url

alexookah commented 7 years ago

why cache is not rewriting stored file ?

evermeer commented 7 years ago

Thats strange. when returning data there is a clear check for request.cachePolicy == NSURLRequest.CachePolicy.reloadIgnoringCacheData So it should not return cached data. Could you place a breakpoint there to see what the actual cachePolicy is?

but you are right that it's also checked the moment the data is returned and it is checked if it should be written. but it will still be written if the file already exists. If it does not exist, then it will also not be written.

My guess is that the actual request does not have the correct cachePolicy

alexookah commented 7 years ago

@evermeer I finally found what was causing the issue. you were right. the request did not have the correct cachePolicy.

now i have one more question.

the response expiration is set to 2 minutes. when making a new request after 2 minutes. shouldnt the cache be skipped and get the new content to overwrite?

alexookah commented 7 years ago

The request headers:

Accept-Ranges:bytes Age:6 Connection:keep-alive Content-Encoding:gzip Content-Type:application/json;charset=UTF-8 Date:Mon, 30 Oct 2017 22:11:50 GMT Expires:Mon, 30 Oct 2017 22:13:50 GMT Set-Cookie:uasdaai; path=/; Max-Age=900 Set-Cookie:utaasz; path=/; Max-Age=900 Set-Cookie:___utmvbss: Ktr; path=/; Max-Age=900