Simple Apollo Offline Caching Example

I have put together a very simple example of how to download and cache items to the files system. This can be useful if your application needs to work offline, or if you want to optimize performance and don’t want to have to keep downloading the same assets over and over.

This example uses some of the caching classes from my Ascension mp3 player, and required the corelib library.

Here is the code for the app:


[code][/code]Basically, the first time you load an image, you get two events. One that the item has been cached, and one that the item is ready to use. If you try to load the same image again, then you only get the item ready event, since the item has already been downloaded and cached.Here is what the app looks like:Here is the main cache class:[code]package com.adobe.apps.ascension.utils{import flash.events.EventDispatcher;import flash.events.IEventDispatcher;import flash.events.Event;import flash.net.FileReference;import flash.net.URLLoader;import flash.net.URLRequest;import flash.filesystem.File;import com.adobe.crypto.MD5;import com.adobe.net.DynamicURLLoader;import flash.events.IOErrorEvent;import flash.filesystem.FileStream;import flash.filesystem.FileMode;import com.adobe.apps.ascension.events.URLCacheEvent;import flash.net.URLLoaderDataFormat;import flash.utils.ByteArray;//todo: add event metadatapublic class URLCache extends EventDispatcher{private var _cacheName:String;//maybe rename to make it clearer it loads datapublic function URLCache(cacheName:String){_cacheName = cacheName;}public function get cacheName():String{return _cacheName;}private function getStorageDir():File{return File.appStorageDirectory.resolve(_cacheName);}public function itemExists(key:String):Boolean{return getItemFile(key).exists;}public function clearCache():void{var cacheDir:File = getStorageDir();try{cacheDir.deleteDirectory(true);}catch (e:IOErrorEvent){// we tried!}}public function getItemFile(key:String):File{var dir:File = getStorageDir();var fName:String = generateKeyHash(key);var file:File = dir.resolve(fName);return file;}public function cacheURL(url:String, key:String):void{var file:File = getItemFile(key);//todo: do we need to check if the dir exists?if(file.exists){var e:URLCacheEvent = new URLCacheEvent(URLCacheEvent.ITEM_READY);e.key = key;e.file = file;dispatchEvent(e);return;}var loader:DynamicURLLoader = new DynamicURLLoader();loader.file = file;loader.key = key;loader.addEventListener(Event.COMPLETE, onDataLoad);loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);loader.dataFormat = URLLoaderDataFormat.BINARY;loader.load(new URLRequest(url));}private function onLoadError(event:IOErrorEvent):void{trace(“onLoadError : could not cache item”);}private function onDataLoad(event:Event):void{var loader:DynamicURLLoader = DynamicURLLoader(event.target);var f:File = File(loader.file);var key:String = String(loader.key);var fileStream:FileStream = new FileStream();fileStream.open(f, FileMode.WRITE);fileStream.writeBytes(loader.data as ByteArray);fileStream.close();var g:URLCacheEvent = new URLCacheEvent(URLCacheEvent.ITEM_CACHED);g.key = key;g.file = f;dispatchEvent(g);var e:URLCacheEvent = new URLCacheEvent(URLCacheEvent.ITEM_READY);e.key = key;e.file = f;dispatchEvent(e);}private function generateKeyHash(key:String):String{return MD5.hash(key);}}}[/code]This works with any data / file types.It still needs to be cleaned up some, but I wanted to get an example online asap.You can download the entire example code here.

6 Responses to Simple Apollo Offline Caching Example

  1. Zach Graves says:

    thanks for that, very useful!

  2. Ashwinee says:

    Last night, after installing Apollo I created a simple mp3 player.The player has 3 buttons i.e. “Open” button to open and browse directory and get the desired mp3 file (using FileOpenPanel component) the other two buttons “Play” and “Stop” to play and stop the song.I tried to pass the native path of the mp3 file selected as the required url to play the song.But it gave me File Stream error whwn I pushed the “Play” button.When I hardcoded the same url in a Flex mp3 application ,It generated the same error.So I am wondering ,if this is a limitation of the alpha release.And if this works out I am thinking of posting a tutorial on flnotes blog for others to follow.Thanks in Advance

  3. Bill Dawson says:

    Mike,Thanks for this and all the tutorials on Lynda.com. This is great information, that will help people realize the power of Apollo.Please keep sharing!

  4. mike chambers says:

    >So I am wondering ,if this is a limitation of the alpha release.use the url property of the File instance.mike chambersmesh@adobe.com

  5. Ashwinee says:

    Hi Mike,Thanks a lot for your reply.Last night I spent a lot of time combing through the Apollo help documents and used as you have suggested url property instead of nativePath property and it worked.Thanks again for the reply.I also have posted it for download in my site(http://www.ashwineedash.com/download/Music%20Tunes.zip)Soon I will be posting the tutorial in my blog.

  6. Mike Henke says:

    Can you give an example of caching a web page? Thanks