Files
orbitdb/src/Cache.js
2016-04-27 12:45:41 +02:00

30 lines
703 B
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const logger = require('logplease').create("orbit-db.Cache");
const defaultFilepath = path.resolve('./orbit-db-cache.json');
let filePath = defaultFilepath;
let cache = {};
class Cache {
static set(key, value) {
cache[key] = value;
fs.writeFileSync(filePath, JSON.stringify(cache, null, 2) + "\n");
}
static get(key) {
return cache[key];
}
static loadCache(cacheFile) {
filePath = cacheFile ? cacheFile : defaultFilepath;
if(fs.existsSync(filePath)) {
logger.debug('Load cache from ' + filePath);
cache = JSON.parse(fs.readFileSync(filePath));
}
}
}
module.exports = Cache;