Cache and preload Gaia pages
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. 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.
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)
These classes will soon be available in our Temple library.
Update: The CacheLoader, CacheURLLoader and LoaderCache are improved and available in our Temple library.
January 25th, 2010 at 17:19
Pretty cool, as usual Thijs! I’m going to test it with my next project which will be a Gaia project
January 31st, 2010 at 4:29
nice man! great help!
February 4th, 2010 at 21:27
[…] 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 […]
February 16th, 2010 at 20:54
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!
February 17th, 2010 at 3:44
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?
February 17th, 2010 at 10:53
@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.
February 17th, 2010 at 13:59
Great, thanks for clarifying.
March 11th, 2010 at 9:14
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!
March 11th, 2010 at 10:05
@Metthew > You can’t use this for preloading FLV files. Since FLV files are loaded by a NetStream and not by a Loader.
March 15th, 2010 at 22:30
[…] 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 […]
May 27th, 2010 at 0:22
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
May 27th, 2010 at 13:17
@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?
June 7th, 2010 at 17:04
[…] 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 […]