Skip to content

Commit

Permalink
Initial refactoring to have a singular ttl property & no timers
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 24, 2018
1 parent c02f7a8 commit 3fc4311
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 217 deletions.
137 changes: 33 additions & 104 deletions lib/tiny-lru.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

(function (global) {
const next = typeof process !== "undefined" ? process.nextTick : arg => setTimeout(arg, 1),
empty = "";
empty = null;

class LRU {
constructor (max, notify, ttl, expire) {
this.expire = expire;
constructor (max, notify, ttl) {
this.max = max;
this.notify = notify;
this.ttl = ttl;
Expand All @@ -33,23 +32,13 @@
return this;
}

clearTimer (key, col = "timers") {
if (this.has(key, col) === true) {
clearTimeout(this[col][key]);
delete this[col][key];
}

return this;
}

delete (key, silent = false) {
return this.remove(key, silent);
}

dump () {
return JSON.stringify({
cache: this.cache,
expire: this.expire,
first: this.first,
last: this.last,
length: this.length,
Expand All @@ -73,15 +62,16 @@
let output;

if (this.has(key) === true) {
output = this.cache[key].value;
this.set(key, output);
const item = this.cache[key];

if (this.ttl > 0) {
this.clearTimer(key).setTimer(key);
}
if (item.expiry === -1 || item.expiry <= Date.now()) {
output = this.cache[key].value;

if (this.notify === true) {
next(this.onchange("get", this.dump()));
if (this.notify === true) {
next(this.onchange("get", this.dump()));
}
} else {
this.remove(key);
}
}

Expand All @@ -92,47 +82,36 @@
return key in this[type];
}

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) {
let result;

if (this.has(key) === true) {
const cached = this.cache[key];
result = this.cache[key];

delete this.cache[key];
this.length--;

if (this.ttl > 0) {
this.clearTimer(key);
}

if (this.expire > 0) {
this.clearTimer(key, "expires");
}

if (this.has(cached.previous) === true) {
this.cache[cached.previous].next = cached.next;

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

if (this.has(cached.next) === true) {
this.cache[cached.next].previous = cached.previous;

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

result = cached;

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

reset () {
if (this.expires !== void 0) {
Object.keys(this.expires).forEach(i => this.clearTimer(i, "expires"));
}

if (this.timers !== void 0) {
Object.keys(this.timers).forEach(i => this.clearTimer(i));
}

this.cache = {};
this.expires = {};
this.first = empty;
this.last = empty;
this.length = 0;
this.timers = {};

return this;
}

set (key, value) {
let first, item;

if (this.has(key) === true) {
item = this.cache[key];
const item = this.cache[key];

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();
}

this.length++;

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

this.cache[key] = {
expiry: this.ttl > 0 ? new Date().getTime() + this.ttl : -1,
next: empty,
previous: this.first,
value: value
};
}

if (this.first !== key && this.has(this.first) === true) {
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()));
}

if (this.ttl > 0) {
this.clearTimer(key).setTimer(key);
}

if (this.expire > 0 && this.has(key, "expires") === false) {
this.setExpire(key);
}

return this;
}

setExpire (key) {
this.expires[key] = setTimeout(() => this.clearTimer(key, "expires").clearTimer(key).remove(key), this.expire);
}

setTimer (key) {
this.timers[key] = setTimeout(() => this.remove(key), this.ttl);
}

update (arg) {
const obj = JSON.parse(arg);

Expand Down
2 changes: 1 addition & 1 deletion lib/tiny-lru.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3fc4311

Please sign in to comment.