1 Reply Latest reply on Jan 31, 2011 5:09 AM by Ned Murphy

    Using getTimer() as a Cache Killer

    Kwangle Level 1

      Hi All,

       

      I have recently been working on a Flash file that dynamically loads a movie based on the contents of a simple XML file. I was concerned about caching issues so tried to set up the code to avoid the XML file being cached causing the wrong content to be loaded. I did this by adding a dummy query string to the file link which is a numerical value generated by getTimer().

       

      In practice the system mostly worked but occasionally old content would load even though the XML had been changed. The code runs a setInterval that checks the XML every 20 seconds and checks if the loaded file specfied has changed. The code loads the XML with the following code:

       

      holderXML.load("http://www.mysite/myscripts/myxml.xml?cachekill="+getTimer());

       

       

      The getTimer() functions starts counting in milliseconds as soon as the movie runs so there is a chance that the same query string is assigned to more than one file, meaning that an old cached version is loaded instead of the new, live file. I am now thinking about ways to improve this code and generate unique values for the query string. I thought using the epoch time - the time in milliseconds from which Flash calculates time and dates which is set to Jan 01 1970 - would generate a new value every millisecond and maybe adding an additional random number to that value to further reduce the chance of the same query string being generated more than once.

       

       

       

      this.onEnterFrame = function () {

      var nowDate:Date = new Date();

      nowTime = nowDate.getTime();

      randomNum = Math.round(Math.random()*10000);

      holderXML.load("http://www.mysite/myscripts/myxml.xml?cachekill="+nowTime+"rnd"+randomNum);

      }

       

       

      Is there a better way of avoiding cache issues in Flash? Any comments and advice would be appreciated.