cache-storage
Version:
[ABANDONED] Advanced cache storage for node js
254 lines (231 loc) • 8.37 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var BaseStorage, Cache, Storage, isWindow, moment, path, _ref,
__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; };
isWindow = typeof window === 'undefined' ? false : true;
if (!isWindow) {
path = require('path');
}
BaseStorage = require('../Storage');
moment = require('moment');
Cache = require('../../Cache');
Storage = (function(_super) {
__extends(Storage, _super);
function Storage() {
_ref = Storage.__super__.constructor.apply(this, arguments);
return _ref;
}
Storage.prototype.async = false;
Storage.prototype.read = function(key) {
var data;
data = this.getData();
if (typeof data[key] === 'undefined') {
return null;
} else {
if (this.verify(this.findMeta(key))) {
return data[key];
} else {
this.remove(key);
return null;
}
}
};
Storage.prototype.write = function(key, data, dependencies) {
var all, meta;
if (dependencies == null) {
dependencies = {};
}
all = this.getData();
all[key] = data;
meta = this.getMeta();
meta[key] = dependencies;
return this.writeData(all, meta);
};
Storage.prototype.remove = function(key) {
var data, meta;
data = this.getData();
meta = this.getMeta();
if (typeof data[key] !== 'undefined') {
delete data[key];
delete meta[key];
}
return this.writeData(data, meta);
};
Storage.prototype.removeAll = function() {
return this.writeData({}, {});
};
Storage.prototype.clean = function(conditions) {
var key, tag, type, typeFn, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _results;
typeFn = Object.prototype.toString;
type = typeFn.call(conditions);
if (conditions === Cache.ALL) {
return this.removeAll();
} else if (type === '[object Object]') {
if (typeof conditions[Cache.TAGS] !== 'undefined') {
if (typeFn(conditions[Cache.TAGS]) === '[object String]') {
conditions[Cache.TAGS] = [conditions[Cache.TAGS]];
}
_ref1 = conditions[Cache.TAGS];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
tag = _ref1[_i];
_ref2 = this.findKeysByTag(tag);
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
key = _ref2[_j];
this.remove(key);
}
}
}
if (typeof conditions[Cache.PRIORITY] !== 'undefined') {
_ref3 = this.findKeysByPriority(conditions[Cache.PRIORITY]);
_results = [];
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
key = _ref3[_k];
_results.push(this.remove(key));
}
return _results;
}
}
};
Storage.prototype.findMeta = function(key) {
var meta;
meta = this.getMeta();
if (typeof meta[key] !== 'undefined') {
return meta[key];
} else {
return null;
}
};
Storage.prototype.findKeysByTag = function(tag) {
var key, meta, metas, result;
metas = this.getMeta();
result = [];
for (key in metas) {
meta = metas[key];
if (typeof meta[Cache.TAGS] !== 'undefined' && meta[Cache.TAGS].indexOf(tag) !== -1) {
result.push(key);
}
}
return result;
};
Storage.prototype.findKeysByPriority = function(priority) {
var key, meta, metas, result;
metas = this.getMeta();
result = [];
for (key in metas) {
meta = metas[key];
if (typeof meta[Cache.PRIORITY] !== 'undefined' && meta[Cache.PRIORITY] <= priority) {
result.push(key);
}
}
return result;
};
Storage.prototype.verify = function(meta) {
var file, item, mtime, time, typefn, _i, _len, _ref1, _ref2, _ref3;
typefn = Object.prototype.toString;
if (typefn.call(meta) === '[object Object]') {
if (typeof meta[Cache.EXPIRE] !== 'undefined') {
if (moment().valueOf() >= meta[Cache.EXPIRE]) {
return false;
}
}
if (typeof meta[Cache.ITEMS] !== 'undefined') {
_ref1 = meta[Cache.ITEMS];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
item = _ref1[_i];
item = this.findMeta(item);
if ((item === null) || (item !== null && this.verify(item) === false)) {
return false;
}
}
}
if (typeof meta[Cache.FILES] !== 'undefined') {
this.checkFilesSupport();
if (isWindow) {
_ref2 = meta[Cache.FILES];
for (file in _ref2) {
time = _ref2[file];
mtime = window.require.getStats(file).mtime;
if (mtime === null) {
throw new Error('File stats are disabled in your simq configuration. Can not get stats for ' + file + '.');
}
if (window.require.getStats(file).mtime.getTime() !== time) {
return false;
}
}
} else {
_ref3 = meta[Cache.FILES];
for (file in _ref3) {
time = _ref3[file];
if ((new Date(Cache.getFs().statSync(file).mtime)).getTime() !== time) {
return false;
}
}
}
}
}
return true;
};
Storage.prototype.parseDependencies = function(dependencies) {
var file, files, item, mtime, result, time, typefn, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3;
typefn = Object.prototype.toString;
result = {};
if (typefn.call(dependencies) === '[object Object]') {
if (typeof dependencies[Cache.PRIORITY] !== 'undefined') {
result[Cache.PRIORITY] = dependencies[Cache.PRIORITY];
}
if (typeof dependencies[Cache.TAGS] !== 'undefined') {
result[Cache.TAGS] = dependencies[Cache.TAGS];
}
if (typeof dependencies[Cache.ITEMS] !== 'undefined') {
result[Cache.ITEMS] = [];
_ref1 = dependencies[Cache.ITEMS];
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
item = _ref1[_i];
result[Cache.ITEMS].push(this.cache.generateKey(item));
}
}
if (typeof dependencies[Cache.EXPIRE] !== 'undefined') {
switch (typefn.call(dependencies[Cache.EXPIRE])) {
case '[object String]':
time = moment(dependencies[Cache.EXPIRE], Cache.TIME_FORMAT);
break;
case '[object Object]':
time = moment().add(dependencies[Cache.EXPIRE]);
break;
default:
throw new Error('Expire format is not valid');
}
result[Cache.EXPIRE] = time.valueOf();
}
if (typeof dependencies[Cache.FILES] !== 'undefined') {
this.checkFilesSupport();
files = {};
if (isWindow) {
_ref2 = dependencies[Cache.FILES];
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
file = _ref2[_j];
mtime = window.require.getStats(file).mtime;
if (mtime === null) {
throw new Error('File stats are disabled in your simq configuration. Can not get stats for ' + file + '.');
}
file = window.require.resolve(file);
files[file] = mtime.getTime();
}
} else {
_ref3 = dependencies[Cache.FILES];
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
file = _ref3[_k];
file = path.resolve(file);
files[file] = (new Date(Cache.getFs().statSync(file).mtime)).getTime();
}
}
result[Cache.FILES] = files;
}
}
return result;
};
return Storage;
})(BaseStorage);
module.exports = Storage;
}).call(this);