Skip to content

Instantly share code, notes, and snippets.

@riston
Last active December 26, 2015 14:48
Show Gist options
  • Save riston/7167872 to your computer and use it in GitHub Desktop.
Save riston/7167872 to your computer and use it in GitHub Desktop.
Is cache file fresh or not.
var fs = require('fs');
var Cache = {
isFresh: function (file, time, cb) {
fs.exists(file, function (exists) {
if (!exists) {
return cb(null, false);
}
fs.stat(file, function(err, stat) {
var fileTimestamp = stat.mtime.getTime(),
currentTime = new Date().getTime();
if (err) {
return cb(err);
} else {
if ((currentTime - fileTimestamp) > time) {
return cb(null, false);
} else {
return cb(null, true);
}
}
});
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment