Skip to content
This repository has been archived by the owner on Feb 13, 2021. It is now read-only.

Commit

Permalink
refactored tests for mocha and tweaked some internals
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed May 4, 2016
1 parent f80da6f commit 3f33196
Show file tree
Hide file tree
Showing 8 changed files with 588 additions and 675 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
coverage
artifacts
.DS_Store
.DS_Store?
11 changes: 11 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
reporting:
reports:
- lcov
- text
- text-summary
check:
global:
statements: 100
lines: 100
branches: 100
functions: 100
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ language: node_js
node_js:
- "0.8"
- "0.10"
- "0.11"
before_install: npm install -g npm
- "0.12"
- "4"
- "6"
before_install: npm install -g npm@latest-2
72 changes: 33 additions & 39 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var CacheObject = function (conf) {

this.count = 0;
this.data = {};
/*istanbul ignore next*/
var next = global.setImmediate || process.nextTick;

this.set = function (key, value, callback) {
Expand Down Expand Up @@ -68,59 +69,52 @@ var CacheObject = function (conf) {
self.count = self.count + 1;
}
}

if (callback) {
callback(null, value);
}
/* jshint -W030 */
callback && callback(null, value);
});
};

this.get = function (key, callback) {
var self = this;
if (!callback) {
throw('cache.get callback is required.');
}

if (!this.data[key]) {
return callback(null, undefined);
}
next(function () {
if (self.data[key]) {
if (conf.ttl !== 0 && (Date.now() - self.data[key].ts) >= self.ttl) {
if (self.data[key].newer) {
if (self.data[key].older) {
// in the middle of the list
self.data[key].newer.older = self.data[key].older;
self.data[key].older.newer = self.data[key].newer;
} else {
// tail
self.tail = self.data[key].newer;
delete self.tail.older;
}
var value;
if (conf.ttl !== 0 && (Date.now() - self.data[key].ts) >= self.ttl) {
if (self.data[key].newer) {
if (self.data[key].older) {
// in the middle of the list
self.data[key].newer.older = self.data[key].older;
self.data[key].older.newer = self.data[key].newer;
} else {
// the first item
if (self.data[key].older) {
self.head = self.data[key].older;
delete self.head.newer;
} else {
// 1 items
delete self.head;
delete self.tail;
}
}

delete self.data[key];
self.count = self.count - 1;

if (callback) {
callback(null, undefined);
// tail
self.tail = self.data[key].newer;
delete self.tail.older;
}
} else {
self.data[key].hit = self.data[key].hit + 1;

if (callback) {
callback(null, self.data[key].val);
// the first item
if (self.data[key].older) {
self.head = self.data[key].older;
delete self.head.newer;
} else {
// 1 items
delete self.head;
delete self.tail;
}
}

delete self.data[key];
self.count = self.count - 1;
} else {
if (callback) {
callback(null, undefined);
}
self.data[key].hit = self.data[key].hit + 1;
value = self.data[key].val;
}
callback(null, value);
});
};
};
Expand Down
Loading

0 comments on commit 3f33196

Please sign in to comment.