UNPKG

cache-storage

Version:

[ABANDONED] Advanced cache storage for node js

190 lines (164 loc) 4.6 kB
// Generated by CoffeeScript 1.6.3 (function() { var Cache, isFunction; isFunction = function(obj) { return Object.prototype.toString.call(obj) === '[object Function]'; }; Cache = (function() { Cache.FILES = 'files'; Cache.TAGS = 'tags'; Cache.EXPIRE = 'expire'; Cache.ITEMS = 'items'; Cache.PRIORITY = 'priority'; Cache.ALL = 'all'; Cache.TIME_FORMAT = 'YYYY-MM-DD HH:mm'; Cache.fs = null; Cache.prototype.storage = null; Cache.prototype.async = null; Cache.prototype.namespace = null; function Cache(storage, namespace) { this.storage = storage; this.namespace = namespace; if (!(this.storage instanceof require('./Storage/Storage'))) { throw new Error('Cache: storage must be instance of cache-storage/Storage/Storage'); } this.async = this.storage.async; this.storage.cache = this; } Cache.mockFs = function(tree, info) { var FS; if (tree == null) { tree = {}; } if (info == null) { info = {}; } FS = require('fs-mock'); Cache.fs = new FS(tree, info); return Cache.fs; }; Cache.restoreFs = function() { if (typeof window !== 'undefined') { throw new Error('Testing with fs module is not allowed in browser.'); } return Cache.fs = require('fs'); }; Cache.getFs = function() { if (Cache.fs === null) { Cache.restoreFs(); } return Cache.fs; }; Cache.prototype.generateKey = function(key) { var ch, hash, i, max, _i; hash = 0; if (key.length === 0) { return hash; } max = key.length - 1; for (i = _i = 0; 0 <= max ? _i <= max : _i >= max; i = 0 <= max ? ++_i : --_i) { ch = key.charCodeAt(i); hash = ((hash << 5) - hash) + ch; hash |= 0; } return hash; }; Cache.prototype.load = function(key, fallback, fn) { var data, _this = this; if (fallback == null) { fallback = null; } if (fn == null) { fn = null; } if (this.async && arguments.length === 2) { fn = fallback; fallback = null; } if (this.async) { return this.storage.read(this.generateKey(key), function(err, data) { if (err) { return fn(err, null); } else if (data === null && fallback !== null) { return _this.save(key, fallback, function(err, data) { return fn(err, data); }); } else { return fn(null, data); } }); } else { data = this.storage.read(this.generateKey(key)); if (data === null && fallback !== null) { return this.save(key, fallback); } return data; } }; Cache.prototype.save = function(key, data, dependencies, fn) { var _this = this; if (dependencies == null) { dependencies = {}; } if (fn == null) { fn = null; } if (isFunction(dependencies)) { fn = dependencies; dependencies = {}; } key = this.generateKey(key); if (isFunction(data)) { data = data(); } if (this.async) { if (data === null) { this.storage.remove(key, function(err) { if (err) { return fn(err, null); } else { return fn(null, data); } }); } else { this.storage.parseDependencies(dependencies, function(err, dependencies) { if (err) { return fn(err, null); } else { return _this.storage.write(key, data, dependencies, function(err) { if (err) { return fn(err, null); } else { return fn(null, data); } }); } }); } } else { if (data === null) { this.storage.remove(key); } else { this.storage.write(key, data, this.storage.parseDependencies(dependencies)); } } return data; }; Cache.prototype.remove = function(key, fn) { if (fn == null) { fn = null; } return this.save(key, null, fn); }; Cache.prototype.clean = function(conditions, fn) { if (fn == null) { fn = null; } this.storage.clean(conditions, fn); return this; }; return Cache; })(); module.exports = Cache; }).call(this);