|
|
@ -2,18 +2,18 @@ const fs = require("fs"),
|
|
|
|
path = require("path"),
|
|
|
|
path = require("path"),
|
|
|
|
config_path = "./config/caching_dump.json",
|
|
|
|
config_path = "./config/caching_dump.json",
|
|
|
|
cache_dump = JSON.parse(fs.readFileSync(config_path)),
|
|
|
|
cache_dump = JSON.parse(fs.readFileSync(config_path)),
|
|
|
|
cache_dir = "./.cache",
|
|
|
|
cache_dir = "./.cache";
|
|
|
|
cache = {};
|
|
|
|
let cache = {};
|
|
|
|
var logger = require("winston");
|
|
|
|
let logger = require("winston");
|
|
|
|
if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump
|
|
|
|
if (cache_dump != null && cache_dump["last"] != null) cache = cache_dump["last"]; // read the data from the file dump
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Sets the logger for logging
|
|
|
|
* Sets the logger for logging
|
|
|
|
* @param {Winston Logger} newLogger
|
|
|
|
* @param {Object} newLogger
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
exports.setLogger = function (newLogger) {
|
|
|
|
exports.setLogger = function (newLogger) {
|
|
|
|
logger = newLogger;
|
|
|
|
logger = newLogger;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns the data from files that were cached
|
|
|
|
* Returns the data from files that were cached
|
|
|
@ -44,12 +44,14 @@ exports.getCached = function(filename) {
|
|
|
|
exports.cache = function (filename, data) {
|
|
|
|
exports.cache = function (filename, data) {
|
|
|
|
logger.verbose("Creating cache entry for %s", filename);
|
|
|
|
logger.verbose("Creating cache entry for %s", filename);
|
|
|
|
if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache"); // if the cache folder doesn't exist, create it
|
|
|
|
if (!fs.existsSync("./.cache")) fs.mkdirSync("./.cache"); // if the cache folder doesn't exist, create it
|
|
|
|
let cache_fn = filename.replace(/[^\w\.]/g, "__"); // remove invalid path characters
|
|
|
|
let cache_fn = filename.replace(/[^\w.]/g, "__"); // remove invalid path characters
|
|
|
|
let count = 0;
|
|
|
|
let count = 0;
|
|
|
|
while (fs.existsSync(filename + count + ".cache")) count++; // check if a file with the same name already exists and increase count
|
|
|
|
while (fs.existsSync(filename + count + ".cache")) count++; // check if a file with the same name already exists and increase count
|
|
|
|
let cache_path = path.join(cache_dir, cache_fn + count + ".cache"); // create the final file path. Cachedir + cached filename (without invalid) + count + .cache
|
|
|
|
let cache_path = path.join(cache_dir, cache_fn + count + ".cache"); // create the final file path. Cachedir + cached filename (without invalid) + count + .cache
|
|
|
|
logger.debug("Creating file %s", cache_path);
|
|
|
|
logger.debug("Creating file %s", cache_path);
|
|
|
|
fs.writeFile(cache_path, data, (error) => {
|
|
|
|
fs.writeFile(cache_path, data, (err) => {
|
|
|
|
|
|
|
|
if (err !== null) logger.error(err);
|
|
|
|
|
|
|
|
else {
|
|
|
|
logger.debug("Created file cache entry for %s", filename);
|
|
|
|
logger.debug("Created file cache entry for %s", filename);
|
|
|
|
cache[filename] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before)
|
|
|
|
cache[filename] = { // create a cache-entry with the file's path when the file is written (so it won't be accessed before)
|
|
|
|
"path": cache_path, // the last call to the file, the count of calls and an
|
|
|
|
"path": cache_path, // the last call to the file, the count of calls and an
|
|
|
@ -58,22 +60,19 @@ exports.cache = function(filename, data) {
|
|
|
|
"data": null,
|
|
|
|
"data": null,
|
|
|
|
"creation_time": Date.now(),
|
|
|
|
"creation_time": Date.now(),
|
|
|
|
"changed": false
|
|
|
|
"changed": false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
fs.watch(filename, (eventType) => { // watch the file for changes
|
|
|
|
fs.watch(filename, (eventType) => { // watch the file for changes
|
|
|
|
logger.debug("Change detected on %s", filename);
|
|
|
|
logger.debug("Change detected on %s", filename);
|
|
|
|
if (eventType == 'change') cache[filename].changed = true; // if the event change is detected set the change attribute to true
|
|
|
|
if (eventType === 'change') cache[filename].changed = true; // if the event change is detected set the change attribute to true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}); // write the data asynchronously to the file
|
|
|
|
}); // write the data asynchronously to the file
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns if the file is already cached
|
|
|
|
* Returns if the file is already cached. It returns false if changes were detected in the file
|
|
|
|
* @param {String} filename The filename to check
|
|
|
|
* @param {String} filename The filename to check
|
|
|
|
* @return {Boolean} Is it cached or not
|
|
|
|
* @return {Boolean} Is it cached or not
|
|
|
|
* TODO: Use last access or use creation_time property to check if the file might
|
|
|
|
|
|
|
|
* be too old. If the function returns false, a new cache-file will be created which
|
|
|
|
|
|
|
|
* has a different name from the old. On each startup a function could check if
|
|
|
|
|
|
|
|
* there are cache-files that are not listet in the cache_dump and delete them.
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
exports.isCached = function (filename) {
|
|
|
|
exports.isCached = function (filename) {
|
|
|
|
let cached_entry = cache[filename];
|
|
|
|
let cached_entry = cache[filename];
|
|
|
@ -96,4 +95,4 @@ exports.cleanup = function() {
|
|
|
|
logger.verbose("Dumping cache into cache_dump file");
|
|
|
|
logger.verbose("Dumping cache into cache_dump file");
|
|
|
|
cache_dump["last"] = cache; // append the cache to the dump object
|
|
|
|
cache_dump["last"] = cache; // append the cache to the dump object
|
|
|
|
fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file
|
|
|
|
fs.writeFileSync(config_path, JSON.stringify(cache_dump)); // write the dump data to the file
|
|
|
|
}
|
|
|
|
};
|
|
|
|