diff options
author | isaacs <i@izs.me> | 2010-08-25 10:37:05 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2010-08-25 16:21:53 +0400 |
commit | 0bfc4254eaa94b9cf47acff1b6b4b6baebe85725 (patch) | |
tree | c6d891515e2a765cf39c18ab12366137c9fd0154 | |
parent | 41c547d1e554d0479db4d55e4fa4febb0fb3c8ff (diff) |
Cache json files for cases where they're read repeatedly.
-rw-r--r-- | lib/utils/read-json.js | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/lib/utils/read-json.js b/lib/utils/read-json.js index 8b71971b2..974123655 100644 --- a/lib/utils/read-json.js +++ b/lib/utils/read-json.js @@ -5,10 +5,17 @@ readJson.processJson = processJson var fs = require("./graceful-fs") , semver = require("./semver") , path = require("path") - + , log = require("./log") + , cache = {} + , timers = {} function readJson (jsonFile, opts, cb) { if (typeof cb !== "function") cb = opts, opts = {} + if (cache.hasOwnProperty(jsonFile)) { + log.verbose(jsonFile, "from cache") + return cb(null, cache[jsonFile]) + } fs.readFile(path.join(path.dirname(jsonFile), "wscript"), function (er, data) { + opts.file = jsonFile if (er) opts.wscript = false else opts.wscript = data.toString().match(/(^|\n)def build\b/) && data.toString().match(/(^|\n)def configure\b/) @@ -122,6 +129,16 @@ function processObject (opts, cb) { return function (er, json) { json._id = json.name+"-"+json.version json = testEngine(json) json = parsePeople(json) + if (opts.file) { + log.verbose(opts.file, "caching") + cache[opts.file] = json + // just in case this is a long-running process... + clearTimeout(timers[opts.file]) + timers[opts.file] = setTimeout(function () { + delete cache[opts.file] + delete timers[opts.file] + }, 1000 * 60 * 60 * 24) + } if (cb) cb(null,json) return json }} |