From c3e6676d533e875b020d231075aac04e4b885677 Mon Sep 17 00:00:00 2001 From: Paul J R Date: Sat, 19 Jan 2013 15:38:59 +1100 Subject: [PATCH] adding cache control --- .gitignore | 1 + lib/cache.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ lib/config.js | 11 ++++++++++- lib/router.js | 10 ++++------ proxy.js | 6 +++++- repos.conf | 3 +++ 6 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 .gitignore create mode 100644 lib/cache.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14d86ad --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/cache diff --git a/lib/cache.js b/lib/cache.js new file mode 100644 index 0000000..18d0045 --- /dev/null +++ b/lib/cache.js @@ -0,0 +1,44 @@ +var fs = require("fs"); + + +function maintainCache() { + // TODO i should check that im already running here and exit if i am + console.log("Cache maintainence routine starting..."); + console.log("Cache maintainence routine ended..."); +} + +exports.startTimer = function() { + // our once-a-day cache maintainer + var cacheTimer = global.repoproxy.scancache*3600*1000; + //var cacheTimer = global.repoproxy.scancache*100; + setInterval(maintainCache, cacheTimer); +} + + +// the service file routine .... PLEASE KILL ME! +function serviceFile(reqpath, res, range) { + + // for now, ignore range. + + fs.exists(reqpath, function(exists) { + if(exists) { + var inp = fs.createReadStream(reqpath); + inp.setEncoding("utf8"); + inp.on("data", function(data) { + res.write(data); + }); + + inp.on("end", function(closed) { + res.end(); + }); + } else { + + // TODO, we need to send this upstream, if its upstream we go up. + res.writeHead(404, {"Content-Type": "text/plain"}); + res.write("404 Not Found\n"); + res.end(); + } + }); +} + +exports.serviceFile = serviceFile; diff --git a/lib/config.js b/lib/config.js index 8d5de4b..dd13be4 100644 --- a/lib/config.js +++ b/lib/config.js @@ -6,6 +6,7 @@ exports.loadConfig = function (conffile) { global.repoproxy.listenPort = 8008; global.repoproxy.cacheDir = "./cache"; global.repoproxy.repo = new Object(); + global.repoproxy.scancache = 1; var confFileData = fs.readFileSync(conffile, "utf8"); @@ -40,9 +41,17 @@ exports.loadConfig = function (conffile) { console.log("Port set to: ", line_real[1]); global.repoproxy.listenPort = line_real[1]; break; + case "cachescan": + console.log("Set cache scan rate to: '%s' hours", line_real[1]); + global.repoproxy.scancache = parseInt(line_real[1]); + if(global.repoproxy.scancache == 0) { + console.log("Cache scan rate didnt make sense, it was 0, and should be at least 1 - it is set to 24, but you should check this setting"); + global.repoproxy.scancache = 24; + } + break; default: if(line_real[0] != "") { - console.log("Invalid line in configuration file ignored: '%s'", line_one); + console.log("WARNING Invalid line in configuration file ignored: '%s'", line_one); } } } diff --git a/lib/router.js b/lib/router.js index ae8408c..96ca844 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1,9 +1,11 @@ var url = require("url"); var fs = require("fs"); +var cache = require("./cache.js"); exports.routeRequest = function(req, res) { // first, strip a /pub/ off the front if it exists var originalurl = url.parse(req.url); + var range = 0; thisurl = originalurl.pathname.replace(/^\/pub/, ""); @@ -32,16 +34,12 @@ exports.routeRequest = function(req, res) { } } else { if(stat.isFile()) { - fs.readFile(reqpath, "utf8", function(err, data) { - res.write(data); - res.end(); - }); + cache.serviceFile(reqpath, res, range); } } } else { // go upstream.. - res.write("here we need to go upstream"); - res.end(); + cache.serviceFile(reqpath, res, range); } }); } diff --git a/proxy.js b/proxy.js index 770e7dc..65cf726 100644 --- a/proxy.js +++ b/proxy.js @@ -1,12 +1,16 @@ var http = require("http"); var config = require("./lib/config.js"); var router = require("./lib/router.js"); +var cache = require("./lib/cache.js"); // first we load the config... +console.log("Loading configuration"); config.loadConfig("./repos.conf"); -console.log("globals: ", global.repoproxy); + +console.log("Starting cache maintenance timer"); +cache.startTimer(); // next we start our main request loop http.createServer(router.routeRequest).listen(global.repoproxy.listenPort); diff --git a/repos.conf b/repos.conf index 90bd646..e98a603 100644 --- a/repos.conf +++ b/repos.conf @@ -1,6 +1,9 @@ # cachedir is where it'll store files cachedir:./cache +# how often we run thru the cache directory and refresh files, delete old files or perform maintenance on repo data (hours) +cachescan:24 + # port to listen on for requests listenport:8008 -- 1.7.0.4