Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add HTTP request caching to an application using ASIHTTPRequests?

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?

like image 931
smokey_the_bear Avatar asked Dec 05 '09 23:12

smokey_the_bear


3 Answers

ASIHTTPRequest now supports caching - check out ASIDownloadCache ie.

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]
like image 123
adam Avatar answered Oct 26 '22 08:10

adam


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.

like image 1
Mike Avatar answered Oct 26 '22 10:10

Mike


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.

like image 1
jkp Avatar answered Oct 26 '22 08:10

jkp