Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Using getTimer() as a Cache Killer

Explorer ,
Jan 31, 2011 Jan 31, 2011

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.

TOPICS
ActionScript
749
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 31, 2011 Jan 31, 2011
LATEST

Using anything that won't be the same value twice is sufficient.  Using Date.getTime() should work by itself.  Using Math.random() by itself is usually sufficient as well.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines