I'm using ASIHttpRequests
and an ASINetworkQueue
in an iphone app to retrieve some 100k XML files and a lot of thumbnails from a web service. I'd like to cache the requests in the style of NSURLCache
. ASI doesn't seem to support caching as is, and I looked at the code and it drops to C to create the requests, so inserting the NSURLCache
layer seemed tricky.
What's the best way to implement this?
ASIHTTPRequest now supports caching - check out ASIDownloadCache ie.
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]
You could provide your own caching before dropping down into ASI code.
Wrap your ASI code in a class that has a method:
-(NSString *)getContentFor:(NSURL *)url
That method first checks an internal NSDictionary to see if it has a key present for the specified url. If it does, it returns the object stored with the key.
If it doesn't, it performs the normal ASIRequest. When the request is received from the server, it stores it as a string in your dictionary with the key of the url.
Of course, you'll need to handle asynchronous requests and expiring of old requests with care.
Anyone asking how they can do this with ASIHTTPRequest directly may be interested in this branch of the code that adds support for this feature as an option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With