Skip to content

Commit

Permalink
Returning state logic, updating test
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 24, 2018
1 parent d91f0c2 commit 25088eb
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 60 deletions.
70 changes: 50 additions & 20 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@

if (item.expiry === -1 || item.expiry <= Date.now()) {
output = item.value;
//this.link(key, "first");

if (this.first !== empty) {
this.cache[this.first].next = key;
}

this.first = key;

if (this.notify === true) {
next(this.onchange("get", this.dump()));
Expand All @@ -83,25 +88,6 @@
return key in this.cache;
}

link (key, pos = "first") {
if (this[pos] !== key) {
if (pos === "first") {
const item = this.cache[key];

item.next = empty;

if (this.last === key && item.previous !== empty) {
this.last = item.previous;
}

item.previous = this.first;
this.first = key;
}/* else {
}*/
}
}

onchange () {}

remove (key, silent = false) {
Expand All @@ -113,6 +99,26 @@
delete this.cache[key];
this.length--;

if (result.previous !== empty) {
this.cache[result.previous].next = result.next;

if (this.first === key) {
this.first = result.previous;
}
} else if (this.first === key) {
this.first = empty;
}

if (result.next !== empty) {
this.cache[result.next].previous = result.previous;

if (this.last === key) {
this.last = result.next;
}
} else if (this.last === key) {
this.last = empty;
}

if (silent === false && this.notify === true) {
next(this.onchange("remove", this.dump()));
}
Expand All @@ -136,6 +142,14 @@

item.value = value;
item.next = empty;

if (this.first !== key) {
item.previous = this.first;
}

if (this.last === key && item.previous !== empty) {
this.last = item.previous;
}
} else {
if (this.length === this.max) {
this.evict();
Expand All @@ -148,8 +162,24 @@
previous: this.first,
value: value
};

if (this.length === 1) {
this.last = key;
}
}

if (this.first !== empty && this.first !== key) {
const first = this.cache[this.first];

first.next = key;

if (first.previous === key) {
first.previous = empty;
}
}

this.first = key;

if (this.notify === true) {
next(this.onchange("set", this.dump()));
}
Expand Down
70 changes: 50 additions & 20 deletions src/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@

if (item.expiry === -1 || item.expiry <= Date.now()) {
output = item.value;
//this.link(key, "first");

if (this.first !== empty) {
this.cache[this.first].next = key;
}

this.first = key;

if (this.notify === true) {
next(this.onchange("get", this.dump()));
Expand All @@ -68,25 +73,6 @@
return key in this.cache;
}

link (key, pos = "first") {
if (this[pos] !== key) {
if (pos === "first") {
const item = this.cache[key];

item.next = empty;

if (this.last === key && item.previous !== empty) {
this.last = item.previous;
}

item.previous = this.first;
this.first = key;
}/* else {
}*/
}
}

onchange () {}

remove (key, silent = false) {
Expand All @@ -98,6 +84,26 @@
delete this.cache[key];
this.length--;

if (result.previous !== empty) {
this.cache[result.previous].next = result.next;

if (this.first === key) {
this.first = result.previous;
}
} else if (this.first === key) {
this.first = empty;
}

if (result.next !== empty) {
this.cache[result.next].previous = result.previous;

if (this.last === key) {
this.last = result.next;
}
} else if (this.last === key) {
this.last = empty;
}

if (silent === false && this.notify === true) {
next(this.onchange("remove", this.dump()));
}
Expand All @@ -121,6 +127,14 @@

item.value = value;
item.next = empty;

if (this.first !== key) {
item.previous = this.first;
}

if (this.last === key && item.previous !== empty) {
this.last = item.previous;
}
} else {
if (this.length === this.max) {
this.evict();
Expand All @@ -133,8 +147,24 @@
previous: this.first,
value: value
};

if (this.length === 1) {
this.last = key;
}
}

if (this.first !== empty && this.first !== key) {
const first = this.cache[this.first];

first.next = key;

if (first.previous === key) {
first.previous = empty;
}
}

this.first = key;

if (this.notify === true) {
next(this.onchange("set", this.dump()));
}
Expand Down
22 changes: 2 additions & 20 deletions test/lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,13 @@ exports.suite = {
const cache = this.cache;

cache.ttl = 25;
test.expect(3);
test.equal(cache.set("1", "a").length, 1, "Should be '1'");
setTimeout(function () {
test.equal(cache.length, 1, "Should be '1'");
cache.get("1");
setTimeout(function () {
test.equal(cache.length, 0, "Should be '0'");
test.done();
}, 25);
}, 10);
},
expire: function (test) {
const cache = this.cache;

cache.expire = 25;
test.expect(3);
test.expect(2);
test.equal(cache.set("1", "a").length, 1, "Should be '1'");
setTimeout(function () {
cache.get("1");
test.equal(cache.length, 1, "Should be '1'");
}, 20);
setTimeout(function () {
test.equal(cache.length, 0, "Should be '0'");
test.done();
}, 25);
}, 30);
},
evict: function (test) {
function populate (arg, start = 0) {
Expand Down

0 comments on commit 25088eb

Please sign in to comment.