Last active
December 26, 2015 14:48
-
-
Save riston/7167872 to your computer and use it in GitHub Desktop.
Is cache file fresh or not.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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