At Flash On The Beach ‘09 I attended Mike Chamber’s session on “Advanced desktop development with Adobe AIR”. As part of the session Mike talked about some utility classes specifically for AIR in the as3corelib utility library – A library filled with many useful classes to aid in application development. One of these classes, ResourceCache, is built to download a file at a given url and same it to a local directory and when asked to get the same file again, it checks if such a file exists in the cache directory and uses that instead. Clearly a very useful class if you want to download assets and keep them available to the AIR application even when you disconnect from the internet and run the AIR app in offline mode.
Someone in the audience raised a question I was about to ask:
Can you set an expiry date for cached files?
Currently the ResourceCache class does not take into account if files should expire and a fresh version should be fetched from the server. Once the file is downloaded, it remains static until the cached file is deleted.
The original version of the class is used like this:
1 2 3 4 5 6 7 8 9 10 11 12 | import com.adobe.air.net.ResourceCache; import com.adobe.air.net.ResourceCacheEvent; var cacheName:String = "MY_APP_CACHE"; var cache:ResourceCache = new ResourceCache( cacheName ); cache.addEventListener( ResourceCacheEvent.ITEM_READY, onItemReady ); cache.retrieve( "url_to_file" ); private function onItemReady( event:ResourceCacheEvent ):void { var file:File = event.file; } |
The retrieve method in ResourceCache accepts a URL as a String and then checks if the file from that URL is stored in the cache directory. If it is, it dispatches an item ready event with the stored file attached. It the url has not been cached, it first downloads the file, writes it to the cache and then dispatches the event.
I made an update to the ResourceCache class to give it support for cached file expiration. After speaking with Mike Chambers at Flash on the Beach about it he recommended I open an issue in the as3corelib issue tracker and post my code in there. So I did: http://code.google.com/p/as3corelib/issues/detail?id=127
Mike said he’ll take a look at adding it into as3corelib.
Here is a quick breakdown of the updates:
Read the rest of this entry


