UNPKG

cache-storage

Version:

[ABANDONED] Advanced cache storage for node js

100 lines (80 loc) 2.76 kB
// Generated by CoffeeScript 1.6.3 (function() { var Cache, FileStorage, Storage, fs, path, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Storage = require('./Storage'); Cache = require('../../Cache'); fs = null; path = null; FileStorage = (function(_super) { __extends(FileStorage, _super); FileStorage.prototype.directory = null; FileStorage.prototype.allData = null; FileStorage.prototype.data = null; FileStorage.prototype.meta = null; function FileStorage(directory) { this.directory = directory; if (typeof window !== 'undefined') { throw new Error('FileStorage: Can not use this storage in browser'); } fs = require('fs'); path = require('path'); this.directory = path.resolve(this.directory); if (!Cache.getFs().existsSync(this.directory)) { throw new Error('FileStorage: directory ' + this.directory + ' does not exists'); } if (!Cache.getFs().statSync(this.directory).isDirectory()) { throw new Error('FileStorage: path ' + this.directory + ' must be directory'); } } FileStorage.prototype.getFileName = function() { return this.directory + '/__' + this.cache.namespace + '.json'; }; FileStorage.prototype.loadData = function() { var file; if (this.allData === null) { file = this.getFileName(); if (Cache.getFs().existsSync(file)) { this.allData = JSON.parse(Cache.getFs().readFileSync(file, { encoding: 'utf8' })); } else { this.allData = { data: {}, meta: {} }; } } return this.allData; }; FileStorage.prototype.getData = function() { if (this.data === null) { this.data = this.loadData().data; } return this.data; }; FileStorage.prototype.getMeta = function() { if (this.meta === null) { this.meta = this.loadData().meta; } return this.meta; }; FileStorage.prototype.writeData = function(data, meta) { var file; this.data = data; this.meta = meta; this.allData = { data: this.data, meta: this.meta }; file = this.getFileName(); return Cache.getFs().writeFileSync(file, JSON.stringify({ data: this.data, meta: this.meta })); }; return FileStorage; })(Storage); module.exports = FileStorage; }).call(this);