Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Commit

Permalink
1.4.0: (Hopefully!) fixed some bugs that hit us in Node 5.0. Also, de…
Browse files Browse the repository at this point in the history
…precated the key-value cache function since we no longer use it.
  • Loading branch information
J. T. L committed Nov 20, 2015
1 parent 4b26bc4 commit e8b8eab
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
20 changes: 20 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"curly": true,
"esnext": true,
"eqeqeq": true,
"forin": true,
"freeze": true,
"futurehostile": true,
"latedef": true,
"maxlen": 79,
"mocha": true,
"noarg": true,
"nocomma": true,
"node": true,
"nonbsp": true,
"nonew": true,
"sub": true,
"strict": true,
"undef": true,
"unused": "vars"
}
44 changes: 25 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

exports.once = function(func) {
var f, handler;

Expand Down Expand Up @@ -135,46 +137,50 @@ exports.sizeBasedKeyValue = function(func, size) {
}

var cache = [],
callbacks = {}
callbacks = {};

size += size
size += size;

return function(key, callback) {
/* Look up in the cache. */
var i
var i;

for(i = 0; i !== cache.length; i += 2)
for(i = 0; i !== cache.length; i += 2) {
if(cache[i] === key) {
if(i !== 0)
Array.prototype.unshift.apply(cache, cache.splice(i, 2))
if(i !== 0) {
Array.prototype.unshift.apply(cache, cache.splice(i, 2));
}

return callback(null, cache[1])
return callback(null, cache[1]);
}
}

/* Somebody else is already polling the backend. Get notified when they're
* done, and bail. */
if(callbacks[key]) {
callbacks[key].push(callback)
return
callbacks[key].push(callback);
return;
}

callbacks[key] = [callback]
callbacks[key] = [callback];

/* Call the backing store. */
return func(key, function(err, value) {
/* Successful result? Then add it to the cache. */
if(!err) {
if(cache.length === size)
cache.length -= 2
if(cache.length === size) {
cache.length -= 2;
}

cache.unshift(key, value)
cache.unshift(key, value);
}

/* Notify all the saved callbacks, and then clean up. */
while(callbacks[key].length)
callbacks[key].pop()(err, value)
while(callbacks[key].length) {
callbacks[key].pop()(err, value);
}

delete callbacks[key]
})
}
}
delete callbacks[key];
});
};
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cache-helpers",
"version": "1.3.1",
"version": "1.4.0",
"description": "caching convenience functions",
"keywords": [
"caching"
Expand All @@ -16,9 +16,10 @@
"dependencies": {
},
"devDependencies": {
"mocha": "*"
"mocha": "*",
"jshint": "*"
},
"scripts": {
"test": "NODE_ENV=test ./node_modules/.bin/mocha test"
"test": "./node_modules/.bin/jshint *.js && NODE_ENV=test ./node_modules/.bin/mocha test"
}
}
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("cache", function() {
"simultaneously", function(done) {
var cinc, m;

cinc = cache.timeBasedWithGrace(inc, 100, 1000),
cinc = cache.timeBasedWithGrace(inc, 100, 1000);
m = 0;

cinc(0, function(err, n) {
Expand Down Expand Up @@ -140,7 +140,7 @@ describe("cache", function() {
"simultaneously in soft timeout state", function(done) {
var cinc;

cinc = cache.timeBasedWithGrace(inc, 100, 1000),
cinc = cache.timeBasedWithGrace(inc, 100, 1000);

cinc(0, function(err, n) {
var m;
Expand Down Expand Up @@ -195,7 +195,7 @@ describe("cache", function() {
n = 0;
});

it("should cache for each key, until you fill the space", function() {
it("should cache for each key, until you fill the space", function(done) {
var f = cache.sizeBasedKeyValue(inc, 2);

f(20, function(err, value) {
Expand Down

0 comments on commit e8b8eab

Please sign in to comment.