- Home /
Always get older version of file when downloading it from server?
Greetings,
There's a text file on our server with some config/settings. I'm trying to download it and read the values in the game. Here's my download routine:
IEnumerator DownloadRoutine()
{
string url = "http://path/to/directory/settings.txt";
using (var www = new WWW(url))
{
yield return www;
if (www.error != null)
throw new Exception("WWW download had an error:" + www.error);
print(www.text);
}
}
So I create the settings file with an initial version, say containing the line "data1 = value1" The download routine works perfectly, and on the 'print' I see the settings.
The problem is if I then change the settings file by adding another line "data2 = value2", I still get the previous version of the file so the printout prints "data1 = value1" as if I didn't add the second line.
I'm not an expert on networking/webstuff so I'm not sure what I'm missing.
I'm on Windows 7 Professional accessing the server and adding/modifying the files via WinSCP. There are options in it to clear cache but it doesn't seem to have any effect. So I'm not sure if the older values are being cached somehow or not...
Any ideas?
Thanks.
Answer by Magius96 · Feb 22, 2016 at 07:35 PM
I've had this issue before. Here's a simple solution to a difficult problem. Change your URL to include a random string.
// Just create some random string here
string randomString = new GUID.ToString();
string url = string.Format("http://path/to/directory/settings.txt?c={0}", randomString);
The problem is that web servers cache data, so you have to kind of force it to ignore the cache settings. The random string causes it to do this because it now caches the results with the random string at the end.
Awesome! $$anonymous$$any thanks sir! What's the ?c= stand for? (the 'c' part). Also, shouldn't there be an option to tell the server not to cache the file? cause now with this solution the server caches multiple stuff cause of the random string (which I don't know if it's super bad or no)
The 'c' can be anything you want there. If you are running your own server, then there should be a way to tell the server to not cache certain pages or anything at all. That is not done through code, but through the server's configurations.
The server caching multiple copies isn't going to hurt the server or affect its performance. Web servers have a set amount of cache space available to them, when that space gets filled, it simply starts clearing out the oldest cache items. Cached items also only exist in the cache for a set period of time, so its not like it's gonna fill up your memory or hard drive.
What we're doing with my method above is forcing the server to think that each request is supposed to have a different result by passing in query string parameters that are different each time. In this case, it doesn't matter what we call the parameter, though we're calling it 'c', and it doesn't matter what the value of that parameter is, as long as its different each time. On the server side, nothing is being done with the parameter that we're passing, nothing at all. But because its there it forces the server software to believe this is a different request than all the others so it sends you an updated copy of the file.