Welcome! Log In Create A New Profile

Advanced

Force re-cache after file has been modified

Posted by Dillybob 
Force re-cache after file has been modified
May 04, 2015 09:35AM
Under nginx obviously.

Let's say I have a npcdata.txt file that is getting ajax requested every time a new game is created on my online game. It's nice to cache that file using nginx, but if I make changes to npcdata, a user will not get that new information from that .txt file UNLESS they do a HARD reload on chrome.

Is there a way to have it so nginx automatically uncache the file if the file has been modified? And no, I do not want to add the ?=timestamp at the end of the url. That would defeat the whole purpose of having a cache in the first place :P

Thanks ~
Re: Force re-cache after file has been modified
May 04, 2015 12:51PM
Use curl to find out exactly what the timestamp is before/after caching, it might be as simple as chrome not doing something it should.

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Force re-cache after file has been modified
May 04, 2015 01:18PM
itpp2012 Wrote:
-------------------------------------------------------
> Use curl to find out exactly what the timestamp is before/after
> caching, it might be as simple as chrome not doing something it
> should.



Crafted up this to get the last modified timestamp

function fetchHeader(url, wch) {
try {
var req=new XMLHttpRequest();
req.open("HEAD", url+'?='+Date.now(), false);
req.send(null);
if(req.status== 200){
return req.getResponseHeader(wch);
}
else return false;
} catch(er) {
return er.message;
}
}

Then I do

lastModified = fetchHeader(u,'Last-Modified');

if(lastModified != getCookie(u)){
fc = '?='+Date.now()+'';
document.cookie=u+'='+lastModified+'';
}


and apply the fc variable to the main url in my MAIN ajax request to get the new data and then re-set the cookie.

For getcookie im using:

function getCookie(name) {
var value = "; " + document.cookie;
var parts = value.split("; " + name + "=");
if (parts.length == 2){return parts.pop().split(";").shift()}else{return''}
}

Works perfect so far. And If i have 2000 KB's of data in my npcdata.txt only 409 bytes are used to check the last modified date, no performance issue except for 1 extra request each time just to check. (And it's only 409 bytes!)

This works great for my game as I need to store a TON of NPC text, etc for the game worlds and if I need to edit any they'll be updated live.



Edited 1 time(s). Last edit at 05/04/2015 01:19PM by Dillybob.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 109
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready