Add caching of the compiled js templates
authorNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 04:10:32 +0000 (15:10 +1100)
committerNick Downing <downing.nick@gmail.com>
Sun, 7 Oct 2018 04:10:32 +0000 (15:10 +1100)
ndserver.js
package.json

index 417b6da..a6ced88 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env node
 
 var assert = require('assert')
+var BuildCache = require('build_cache')
 var commander = require('commander')
 var fs = require('fs')
 var http = require('http')
@@ -40,6 +41,8 @@ var mime_types = JSON.parse(fs.readFileSync('config/mime_types.json'))
 var mime_type_default = 'application/octet-stream'
 var mime_type_css = mime_types['css'] || mime_type_default
 var mime_type_html = mime_types['html'] || mime_type_default
+var build_cache_js = new BuildCache()
+var build_cache_less = new BuildCache()
 
 var serve = function(res, status, mime_type, data) {
   res.statusCode = status
@@ -207,15 +210,20 @@ var app = async function(req, res, protocol) {
           root: site_root
         }
         var out = str => {buffers.push(Buffer.from(str))}
-        var req = async str => await (
-          await jstemplate(
-            (
-              str.length > 0 && str.charAt(0) === '/' ?
-              site_root :
-              site_root + dir_name + '/'
-            ) + str
-          )
-        )(env, out, req)
+        var req = async str => {
+          var key = (
+            str.length > 0 && str.charAt(0) === '/' ?
+            site_root :
+            site_root + dir_name + '/'
+          ) + str
+          var value = await build_cache_js.get(key)
+          if (value === undefined) {
+            console.log(site, 'js compile', key)
+            value = await jstemplate(key)
+            build_cache_js.set(key, value)
+          }
+          return await value(env, out, req)
+        }
         await req(temp)
         data = Buffer.concat(buffers)
         console.log(
index 93c179b..da2d9d8 100644 (file)
@@ -5,6 +5,7 @@
   "main": "ndserver.js",
   "directories": {},
   "dependencies": {
+    "build_cache": "file:../build_cache.git/build_cache-1.0.0.tgz",
     "commander": "^2.18.0",
     "fs": "^0.0.1-security",
     "http": "^0.0.0",