mirror of
https://github.com/NuSkooler/enigma-bbs.git
synced 2025-06-08 13:44:39 +02:00
* JSONCache for caching JSON data (vs reading + parsing every time)
* Minor changes
This commit is contained in:
parent
306e84b323
commit
89adc83fc6
9 changed files with 149 additions and 52 deletions
65
core/json_cache.js
Normal file
65
core/json_cache.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
/* jslint node: true */
|
||||
'use strict';
|
||||
|
||||
var Config = require('./config.js').config;
|
||||
var Log = require('./logger.js').log;
|
||||
|
||||
var paths = require('path');
|
||||
var fs = require('fs');
|
||||
var Gaze = require('gaze').Gaze;
|
||||
var stripJsonComments = require('strip-json-comments');
|
||||
var assert = require('assert');
|
||||
|
||||
module.exports = exports = new JSONCache();
|
||||
|
||||
function JSONCache() {
|
||||
|
||||
var self = this;
|
||||
this.cache = {}; // filePath -> JSON
|
||||
this.gaze = new Gaze();
|
||||
|
||||
this.reCacheJSONFromFile = function(filePath, cb) {
|
||||
fs.readFile(filePath, { encoding : 'utf-8' }, function fileRead(err, data) {
|
||||
try {
|
||||
self.cache[filePath] = JSON.parse(stripJsonComments(data));
|
||||
cb(null, self.cache[filePath]);
|
||||
} catch(e) {
|
||||
cb(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
this.gaze.on('error', function gazeErr(err) {
|
||||
|
||||
});
|
||||
|
||||
this.gaze.on('changed', function fileChanged(filePath) {
|
||||
assert(filePath in self.cache);
|
||||
|
||||
Log.info( { filePath : filePath }, 'JSON file changed; recaching');
|
||||
|
||||
self.reCacheJSONFromFile(filePath, function reCached(err) {
|
||||
if(err) {
|
||||
Log.error( { error : err, filePath : filePath } , 'Error recaching JSON');
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
JSONCache.prototype.getJSON = function(fileName, cb) {
|
||||
var self = this;
|
||||
var filePath = paths.join(Config.paths.mods, fileName);
|
||||
console.log(filePath)
|
||||
|
||||
if(filePath in this.cache) {
|
||||
cb(null, this.cache[filePath]);
|
||||
} else {
|
||||
this.reCacheJSONFromFile(filePath, function fileCached(err, json) {
|
||||
if(!err) {
|
||||
self.gaze.add(filePath);
|
||||
}
|
||||
cb(err, json);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue