Tuesday, September 1, 2009

flash uses the brower's cache

Try this code on the Timeline:


function loadEm() : void
var loader : Loader;

for ( var i : int = 0; i < 10; i++ )
loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE,
completeHandler );
loader.load( new URLRequest( "http://www.nasa.gov/images/content/382654main_s128e006565_full.jpg" ) );
loader.x = Math.random() * stage.stageWidth;
loader.y = Math.random() * stage.stageHeight;
addChild( loader );

function completeHandler( event : Event ) : void
var loaderInfo : LoaderInfo = event.currentTarget as LoaderInfo;
loaderInfo.removeEventListener( Event.COMPLETE, completeHandler );
loaderInfo.content.width = 100;
loaderInfo.content.height = 100;

It loads the same image over and over and places each copy in a random spot on the stage. It's a big image. On my broadband connection, it takes about five seconds to appear.

I expected the first copy to take a while to load. But I figured it would get cached, and that the subsequent ones would appear instantaneously. But to my surprise, they didn't. When I compiled a test movie, I found I had to wait for each copy to be loaded as it was pulled individually from the sever.

Then I tried loading the swf into Firefox. Once I ran it in the browser, it behaved as expected: five seconds for the first image and then -- bing! -- the others appeared all at once.

Firebug confirmed that the swf only make one call to the server.

For some reason, I thought Flash had its own internal cache. Apparently it doesn't. It uses the brower's cache.

No comments:

Post a Comment