Disable Cache for WWW

In my IPhone project, XML files are loaded via the WWW class and parsed by my game, this XML files will be updated every now and then. Sometimes after making a change to one of this xml’s, uploading it, checking it was uploaded and then trying to access it in the game, the WWW returns the old file it had downloaded instead of the new one. How can I force the WWW class to download the new files everytime?

Append a random number or timestamp to your url:

url = "http://myserver/myXMLscript.php?t=" + Random.value;

That will force the url to be different each time, but your script ignores the ‘t’ parameter. So if it does cache, it caches something that never is accessed again.

Note that for plain text files,

http://yourServer.com/project/controlFile.txt

on most (all?) web servers you can indeed add an “argument” on the end

http://yourServer.com/project/controlFile.txt?p=123123123

which is harmless and (may, probably) defeat OS-level caching strategies on devices.


Here’s a simple extension that does that in a simple and consistent way:

public static string URLAntiCacheRandomizer(this string url)
	{
	string r = "";
	r += UnityEngine.Random.Range(
                  1000000,8000000).ToString();
	r += UnityEngine.Random.Range(
                  1000000,8000000).ToString();
	string result = url + "?p=" + r;
	return result;
	}

Just use like this …

	WWW w = new WWW( fullUrl.URLAntiCacheRandomizer() );
	yield return w;

Works 100.0% perfectly on both iOS and Android, on high-volume apps.


Just a reminder these days 2016+ iOS pretty much demands an ssl https URL for such things, life’s easier if you have your server https.

I tried making a unique URL to avoid the caching problem but that didn’t work for me. Instead I had to manually delete any cookies and cache files when the app is loaded.

void Awake() {
		string dir = Application.persistentDataPath;
		dir = dir.Replace("/Documents", "/Library/Cookies");
		if (Directory.Exists(dir)) {
			string[] filePaths = Directory.GetFiles(dir);
			for (int i = 0; i < filePaths.Length; i++) {
				print (filePaths*);*

_ print (File.GetCreationTime(“/private” + filePaths*));_
_ File.Delete(“/private” + filePaths);
}
}_
dir = dir.Replace(“/Library/Cookies”, “/Library/Caches/com.test.YOUR_BUNDLE_IDENTIFIER/”);
_ if (Directory.Exists(dir)) {
string filePaths = Directory.GetFiles(dir);
for (int i = 0; i < filePaths.Length; i++) {
if (filePaths.Contains(“Cache.”)) {
print (filePaths);
print (File.GetCreationTime(“/private” + filePaths));
File.Delete(“/private” + filePaths);
}
}
}
}*_

Not sure why this is happening, but the WWW class doesn’t let you do many normal HTTP things. UniWeb provides control for caching in HTTP.

Hi all,
This is my test result:
On PC and on Android, results are the same:
I use WWW with GET method, and there is no cache problem.

Based on my experience in developing flash projects, DaveA’s solution works.
I hope it helps!

Maybe Caching.CleanCache() in Start/Awake would work?

see Scripting API

I think this might be a step in the direction you’re wanting, at least for iOS.

https://docs.unity3d.com/Manual/iosCustomWWWRequest.html

It’s worked for disabling cookies for me. I actually need and use cookies, but Unity provides no way to delete them in UnityWebRequest as of this writing after they’re automatically created in iOS so I am handling them manually. I lucked out on finding this in the manual.