Skip to content

Instantly share code, notes, and snippets.

@sTiLL-iLL
Last active August 29, 2015 14:10
Show Gist options
  • Save sTiLL-iLL/96d817c9376f807618fa to your computer and use it in GitHub Desktop.
Save sTiLL-iLL/96d817c9376f807618fa to your computer and use it in GitHub Desktop.
kacher.js A basic static resource cache module for node.js
// kacher.js ... this uses my Signals.js event module as a dependency
var signalCaster = require("./signals").Signals,
fs = require('fs'),
url = require("url"),
path = require("path"),
config = {
cache: [],
cachedFilesMaximum: function (maxFiles) {
var f = maxFiles || 500;
return f;
},
cacheInterval: function (interval) {
var v = interval || 1550; // 1/2 hour
return v;
},
mimeTypes: {
".txt": "text/plain",
".css": "text/css",
".js": "text/javascript",
".htm": "text/html",
".html": "text/html",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".ico": "image/x-icon",
".xml": "text/xml",
".rss": "application/rss+xml",
".json": "application/json",
".zip": "application/zip, application/octet-stream",
".rar": "application/x-rar-compressed, application/octet-stream",
".exe": "application/octet-stream"
}
};
function strt(interval, fileMax) {
config.cacheInterval(interval);
config.cachedFilesMaximum(fileMax);
setInterval(function(){
for (var pth in config.cache) {
performCacheAssessment(pth);
}
}, config.cacheInterval());
}
function addToCache(fPth, dta){
if(config.cache.length < config.cachedFilesMaximum()) {
config.cache[fPth] = {data:dta, mtime:0};
}
return;
}
function performCacheAssessment(pth) {
fs.stat(pth, function(err, sts){
if(!err){
if(config.cache[pth].mtime != sts.mtime.getTime()){
fs.readFile(pth, function(err, dta){
if(!err){
config.cache[pth].data = dta;
config.cache[pth].mtime = sts.mtime.getTime();
}
});
}
}
});
return;
}
// the srv method is passed to the caller when "startCache" is called. That way, serving the file can still -
// be explicitly invoked by the caller to issue the response, but all the cache mechanics are abstracted away.
function serve(pnm, request, response) {
var fn = path.basename(pnm),
xt = path.extname(request.url),
dn = path.dirname(request.url),
fPth = path.join(__dirname+dn, fn);
if(config.cache[fPth] !== undefined) {
response.writeHead(200, { "Content-Type": config.mimeTypes[xt] });
response.end(config.cache[fPth].data);
signalCaster.signal("FileServed_Cache", fPth + ": served from the cache...");
return;
}
else {
fs.readFile(fPth, function(err, dta){
if(!err){
addToCache(fPth, dta, config.mimeTypes[xt]);
signalCaster.signal("FileServed_Disk", fPth + ": served from the disk...");
response.writeHead(200, {"Content-Type":config.mimeTypes[xt]});
response.end(dta);
signalCaster.signal("FileCached", fPth + ": placed in cache...");
}
else{
response.writeHead(404, {"Content-Type": "text/html"});
response.end("file aint here... Sorry you suck.")
console.log(err);
}
});
return;
}
}
exports.startCache = function(i, m) {
strt(i,m);
return serve;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment