Skip to content

Commit

Permalink
Removed promises and lodash requirement and changed interface to use …
Browse files Browse the repository at this point in the history
…call backs to keep it lean.
  • Loading branch information
jtwebman committed Oct 8, 2015
1 parent d88dc6d commit 5178128
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 138 deletions.
122 changes: 70 additions & 52 deletions cache.js
Expand Up @@ -6,9 +6,6 @@ var DEFAULT_TTL = 300000;
// 30 seconds in ms is the default load timeout
var DEFAULT_TIMEOUT = 30000;

var BPromise = require('bluebird');
var _ = require('lodash');

function handleError(cache, err) {
cache.lastLoadError = err;
if (cache.options.errorCallback) {
Expand All @@ -18,54 +15,75 @@ function handleError(cache, err) {
return cache;
}

function callLoader(cache) {
try {
return BPromise.resolve(cache.options.loader());
} catch(ex) {
return BPromise.reject(ex);
function load(cache, cb) {
if (!cache.loading) {
cache.loading = true;

var timeoutProtect = setTimeout(function() {
timeoutProtect = null;
cache.loading = false;
handleError(cache, new Error('Cache loading timeout hit at ' + cache.options.timeout + 'ms.'));
}, cache.options.timeout);

try {
cache.options.loader(function(err, loadData) {
if (timeoutProtect) {
clearTimeout(timeoutProtect);
cache.loading = false;
if (err) {
handleError(cache, err);
if (cb) cb(err);
} else {
cache.data = loadData;
if (cb) cb(null, loadData);
}
}
});
} catch (err) {
clearTimeout(timeoutProtect);
cache.loading = false;
handleError(cache, err);
if (cb) cb(err);
}
}
}

function load(cache) {
if (!cache.loadPromise || !cache.loadPromise.isPending()) {
var startTime = process.hrtime();

cache.loadPromise = callLoader(cache)
.timeout(cache.options.timeout)
.then(function(loadData) {
cache.lastLoadRunTime = process.hrtime(startTime)[1]/1000000;
cache.data = loadData;
return cache;
}).catch(function(err) {
cache = handleError(cache, err);
return cache;
});
}
return cache.loadPromise;
}

function getData(cache, args) {
if (!cache.data) {
return cache.loadPromise.then(function(loadedCache) {
return callGetter(loadedCache, args);
});
}

return BPromise.resolve(cache).then(function(loadedCache) {
return callGetter(loadedCache, args);
});
}

function callGetter(cache, args) {
if (cache.options.getter) {
args.unshift(cache.data);
return cache.options.getter.apply(null, args);
} else {
return cache.data;
var cb = null;
try {
cb = args.pop();

if (typeof cb !== 'function') {
throw new Error('Last parameter needs to be a callback function.');
}

var waitLock = setInterval(function() {
if (!cache.loading) {
clearInterval(waitLock);

if (!cache.data && cache.lastLoadError) {
cb(cache.lastLoadError);
} else {
if (cache.options.getter) {
args.unshift(cache.data);
args.push(cb);
return cache.options.getter.apply(null, args);
} else {
return cb(null, cache.data);
}
}
}
}, 0);

} catch(err) {
if (cb) {
cb(err);
} else {
throw(err);
}
}
}


function Cache(options) {
var cache = {};

Expand Down Expand Up @@ -94,16 +112,16 @@ function Cache(options) {
load(cache);
cache.intervalId = setInterval(function() { load(cache); }, options.ttl);

cache.get = function() {
var args = [].slice.call(arguments);
return getData(cache, args);
};
return {
get: function() {
var args = [].slice.call(arguments);
return getData(cache, args);
},

cache.reload = function(cb) {
return load(cache);
reload: function(cb) {
return load(cache, cb);
}
};

return cache;
}

module.exports = Cache;
7 changes: 2 additions & 5 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "refresh-cache",
"version": "1.0.2",
"version": "2.0.0-beta",
"description": "A simple Javascript cache that refreshes by calling the load function at a set refresh time.",
"main": "cache.js",
"scripts": {
Expand All @@ -26,12 +26,9 @@
"devDependencies": {
"coveralls": "^2.11.4",
"istanbul": "^0.3.21",
"lodash": "^3.10.1",
"mocha": "^2.3.3",
"mocha-lcov-reporter": "^1.0.0",
"must": "^0.13.1"
},
"dependencies": {
"bluebird": "^2.10.2",
"lodash": "^3.10.1"
}
}

0 comments on commit 5178128

Please sign in to comment.