Cache and preload Gaia pages

Gaia Flash FrameworkI use Gaia a lot. I think it’s a great and powerful framework to create page-based Flash sites. But there is a problem: Gaia creates a SWF for every page. And every time you visit a page, the SWF is loaded again. Even if you already visited that page. Also it is not possible to preload pages you are not visiting.

To make page caching and page preloading possible, I have created the ‘CacheLoader‘ and ‘CacheURLLoader‘ classes. Both classes make use of the ‘LoaderCache‘ to cache the SWF files used by Gaia. Just add these 3 classes to your project and you only have to adapt 2 Gaia classes to enable this feature:

1) Make the DisplayObjectAsset use the CacheLoader instead of a Loader:  Open  ‘com.gaiaframework.assets.DisplayObjectAsset’ and change line 58 to:

1
_loader = new CacheLoader();

And set EventListeners on this _loader instead of his contentLoaderInfo (line 60)

1
addListeners(_loader);

And update your imports.

2) Make the  ByteArrayAsset use the CacheURLLoader instead of an URLLoader: Open  ‘com.gaiaframework.assets.ByteArrayAsset’ and change line 42 to:

1
loader = new CacheURLLoader();

And update your imports.

Now all pages are cached in the LoaderCache. The SWF files are only loaded once. (Notice that the progress and error events are now dispatched from the CacheLoader and not his contentLoaderInfo.)

To preload pages, just add the page as a ByteArray asset in your site.xml:

1
<asset id="page2_cache" type="bytearray"></asset>

The SWF files are cached as ByteArray. Therefore the pages are normally loaded, constructed and destroyed. So there is no need to adjust your pages to make use of this caching mechanism.

View the example.

Note: use right mouse button to navigate. Page2 is preloaded as bytearray asset.

You can use a proxy tool (like Charles or firebug) to see that all page SWF files are only loaded once. (Note: scaffold.swf is not a page)

Or download the source files.

These classes will soon be available in our Temple library.

Update: The CacheLoader, CacheURLLoader and LoaderCache are improved and available in our Temple library.

13 Responses to “Cache and preload Gaia pages”

  1. Jos Koomen Says:

    Pretty cool, as usual Thijs! I’m going to test it with my next project which will be a Gaia project

  2. tom anthoni Says:

    nice man! great help!

  3. TyZ » Blog Archive » Cache and preload Gaia pages « Ramblings Says:

    […] TyZ » Blog Archive » Cache and preload Gaia pages Filed under: Uncategorized — Tags: dev, flash, gaia, tool — michaelangela @ 7:27 pm I am looking at using Gaia for an upcoming project but not being able to use the cache might be a bit of an issue. I do wonder why it’s not caching at all if the browser is set to cache though. I’ll have to try it out and see what is being requested. I use Gaia a lot. I think it’s a great and powerful framework to create page-based Flash sites. But there is a problem: Gaia creates a SWF for every page. And every time you visit a page, the SWF is loaded again. Even if you already visited that page. Also it is not possible to preload pages you are not visiting.To make page caching and page preloading possible, I have created the ‘CacheLoader‘ and ‘CacheURLLoader‘ classes. TyZ » Blog Archive » Cache and preload Gaia pages […]

  4. christian Says:

    This looks really cool, I will try to implement it my flash framework “FLEB” http://blog.flashcmsframework.com/category/fleb-framework/
    Thanks for sharing this!

  5. Jason Langdon Says:

    Looking at my server logs for when I access my site, the swf files appear to be caching fine (ie. they don’t reload everytime I visit the site as you seem to be suggesting should be the case). The xml assets have the nocache property appended so always reload (which is probably another issue, given that I am using the gaiaSiteVersion property in my site.xml)

    I’m using Firefox on Vista, so am wondering whether the swf loads you’re seeing are occuring on a different browser/platform?

  6. Thijs Says:

    @Jason > This is because SWF files are cached by the browser. If you disable your browser cache, you’ll see that the SWF’s are loaded every time.

    The reason I use this caching method is that it’s faster then the browsers cache and you’ll have the possibility to preload pages.

    The browser cache will vary on each system, so you shouldn’t rely on it.

  7. Jason Langdon Says:

    Great, thanks for clarifying.

  8. Matthew Says:

    Hey– This looks very helpful– thanks for sharing.
    One question though– could this be used (or extended) to pre-cache FLV files? I’m trying to wrap my head around a method to effectively preload FLVs into Gaia, and came across your notes. Be good to know!

  9. Thijs Says:

    @Metthew > You can’t use this for preloading FLV files. Since FLV files are loaded by a NetStream and not by a Loader.

  10. Fleb Framework 1.27 released: Cache and preloading | Turtlebite Flash CMS Framework Says:

    […] time ago I stumbled upon this post and I decided to implement it in the Fleb Framework. It’s about cache and preloading. I also […]

  11. george strnad Says:

    Hi, thanks for the code, nice site… I’m having a problem though.. I’m getting two errors when I compile.:

    1093: Syntax error.
    1071: Syntax error: expected a definition keyword (such as function) after attribute OCTYPE, not html.

    Both of them are on line 4 of CacheLoader.as

  12. Thijs Says:

    @George > could it be that you are compiling the wrong files? Since there is no word ‘html’ in the class. Maybe you are compiling an HTML file?

    Can you send or show me the files you try to compile?

  13. TyZ - Thijs Broerse » Blog Archive » OranjePoule - De grootste WK poule van Nederland Says:

    […] heb ik de ActionScripting gedaan. Hiervoor heb ik gebruik gemaakt van Gaia en de Temple. Ook is het caching systeem ingezet en verbeterd. Voor dit project heb ik een speciale truck bedacht om ActionScript code uit […]

Leave a Reply