Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexookah opened this issue Oct 26, 2017 · 9 comments
Open

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

alexookah opened this issue Oct 26, 2017 · 9 comments

Comments

@alexookah
Copy link
Contributor

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
Copy link
Owner

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
Copy link
Contributor Author

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
Copy link
Owner

Yes, that's correct.

@alexookah
Copy link
Contributor Author

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
Copy link
Contributor Author

alexookah commented Oct 27, 2017

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
Copy link
Contributor Author

why cache is not rewriting stored file ?

@evermeer
Copy link
Owner

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
Copy link
Contributor Author

alexookah commented Oct 30, 2017

@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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants