Download cache

122 views
Skip to first unread message

Ben Copsey

unread,
May 4, 2010, 9:39:45 AM5/4/10
to asihttp...@googlegroups.com
There's an initial implementation of a download cache for ASIHTTPRequest available in this branch on github:

http://github.com/pokeb/asi-http-request/tree/downloadcache

You can download it here: http://github.com/pokeb/asi-http-request/zipball/downloadcache

In short, there's a new ASICacheDelegate protocol that allows you to write your own cache, and the ASIDownloadCache class provides an implementation that will probably be sufficient for most people.

ASIDownloadCache caches responses for GET requests that have '200 OK' response status codes. It should work out of the box for statically served resources, but you should be able to get it to cache dynamic responses if you tweak the response headers on the server.

The body of a response is stored on disk, along with the response headers from the server. When you perform a GET request for a resource that is already cached, ASIHTTPRequest changes the request to a conditional GET request transparently. If the data in the cache is still current, requests have their response headers and body / downloadDestinationPath set to those from the cache.

Cached responses can be stored for the duration of the session, or permanently. You can also configure the cache to ignore cache-control headers from the server, and / or set your own expiry time for cached responses.

Using the download cache is easy:

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];

See the options in ASICacheDelegate.h for details on how to configure the download cache to work as you want it. ASIHTTPRequests have new properties to allow you to configure how they will be cached on a per-request basis, see ASIHTTPRequest.h for more.

For certain applications, using a download cache may be enough to eliminate the need for your own local storage altogether.

I would be interested in any feedback on / bug reports for the current implementation. If all goes well, this should be in v1.7 fairly soon.

Best

Ben

--
You received this message because you are subscribed to the Google Groups "ASIHTTPRequest" group.
To post to this group, send email to asihttp...@googlegroups.com.
To unsubscribe from this group, send email to asihttpreques...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/asihttprequest?hl=en.

Matt Long

unread,
May 4, 2010, 11:22:22 AM5/4/10
to asihttp...@googlegroups.com
Sounds great, Ben. Nice addition to the library!!

So just to clarify, this idea of a conditional GET works for images in web pages? In other words, what if I want to load the cached web page into a web view and have cached images display as well. Is this supported? How would this work?

Thanks.

-Matt

seif

unread,
May 4, 2010, 12:45:16 PM5/4/10
to asihttp...@googlegroups.com

 hi

thank you very much 




2010/5/4 Ben Copsey <b...@allseeing-i.com>

Ben Copsey

unread,
May 5, 2010, 6:28:44 AM5/5/10
to asihttp...@googlegroups.com
Hi Matt

> So just to clarify, this idea of a conditional GET works for images in web pages? In other words, what if I want to load the cached web page into a web view and have cached images display as well. Is this supported? How would this work?

If you're using a UIWebView and it is loading the images itself, then no, I'm afraid this probably won't help.

One trick might be to parse the html yourself, find all links to external resources, download all the external resources with ASIHTTPRequest, then replace the urls in the html with the data inline (see here: http://stackoverflow.com/questions/345432/is-it-possible-to-cache-resources-loaded-in-an-iphone-uiwebview). If you wrote the html back to the cache file for the html page manually, the second time needed to show the page, it wouldn't need to download any data. The whole thing could be wrapped in an ASIHTTPRequest subclass, you'd just take its response data and load it with the webview. Probably a bit of work, though... :)
Reply all
Reply to author
Forward
0 new messages