Comparing version 1.0.0 to 1.1.0
89
index.js
module.exports = class MaxCache { | ||
constructor ({ maxSize, maxAge }) { | ||
constructor ({ maxSize, maxAge, createMap }) { | ||
this.maxSize = maxSize | ||
this.maxAge = maxAge | ||
this._latest = new Map() | ||
this._oldest = new Map() | ||
this._createMap = createMap || defaultCreateMap | ||
this._latest = this._createMap() | ||
this._oldest = this._createMap() | ||
this._retained = this._createMap() | ||
this._gced = false | ||
@@ -18,12 +20,18 @@ this._interval = null | ||
[Symbol.iterator] () { | ||
return new Iterator(this._latest[Symbol.iterator](), this._oldest[Symbol.iterator]()) | ||
* [Symbol.iterator] () { | ||
for (const it of [this._latest, this._oldest, this._retained]) { | ||
yield * it | ||
} | ||
} | ||
keys () { | ||
return new Iterator(this._latest.keys(), this._oldest.keys()) | ||
* keys () { | ||
for (const it of [this._latest, this._oldest, this._retained]) { | ||
yield * it.keys() | ||
} | ||
} | ||
values () { | ||
return new Iterator(this._latest.values(), this._oldest.values()) | ||
* values () { | ||
for (const it of [this._latest, this._oldest, this._retained]) { | ||
yield * it.values() | ||
} | ||
} | ||
@@ -38,32 +46,47 @@ | ||
clear () { | ||
this._gc() | ||
this._gc() | ||
this._gced = true | ||
this._latest.clear() | ||
this._oldest.clear() | ||
this._retained.clear() | ||
} | ||
set (k, v) { | ||
if (this._retained.has(k)) return this | ||
this._latest.set(k, v) | ||
this._oldest.delete(k) | ||
this._oldest.delete(k) || this._retained.delete(k) | ||
if (this._latest.size >= this.maxSize) this._gc() | ||
return this | ||
} | ||
retain (k, v) { | ||
this._retained.set(k, v) | ||
this._latest.delete(k) || this._oldest.delete(k) | ||
return this | ||
} | ||
delete (k) { | ||
return this._latest.delete(k) || this._oldest.delete(k) | ||
return this._latest.delete(k) || this._oldest.delete(k) || this._retained.delete(k) | ||
} | ||
has (k) { | ||
return this._latest.has(k) || this._oldest.has(k) || this._retained.has(k) | ||
} | ||
get (k) { | ||
let bump = false | ||
let v = this._latest.get(k) | ||
if (!v) { | ||
v = this._oldest.get(k) | ||
if (!v) return null | ||
bump = true | ||
if (this._latest.has(k)) { | ||
return this._latest.get(k) | ||
} | ||
if (bump) { | ||
if (this._oldest.has(k)) { | ||
const v = this._oldest.get(k) | ||
this._latest.set(k, v) | ||
this._oldest.delete(k) | ||
return v | ||
} | ||
return v | ||
if (this._retained.has(k)) { | ||
return this._retained.get(k) | ||
} | ||
return null | ||
} | ||
@@ -79,24 +102,8 @@ | ||
this._oldest = this._latest | ||
this._latest = new Map() | ||
this._latest = this._createMap() | ||
} | ||
} | ||
class Iterator { | ||
constructor (a, b) { | ||
this.a = a | ||
this.b = b | ||
} | ||
[Symbol.iterator] () { | ||
return this | ||
} | ||
next () { | ||
if (this.a !== null) { | ||
const n = this.a.next() | ||
if (!n.done) return n | ||
this.a = null | ||
} | ||
return this.b.next() | ||
} | ||
function defaultCreateMap () { | ||
return new Map() | ||
} |
{ | ||
"name": "xache", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Yet another auto expiring, max sizable cache", | ||
@@ -8,7 +8,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"standard": "^16.0.3", | ||
"tape": "^5.2.2" | ||
"brittle": "^2.3.1", | ||
"standard": "^16.0.3" | ||
}, | ||
"scripts": { | ||
"test": "standard && tape test.js" | ||
"test": "standard && brittle test.js" | ||
}, | ||
@@ -15,0 +15,0 @@ "repository": { |
@@ -16,3 +16,6 @@ # xache | ||
maxSize: 10, // at max (ish) have 10 entries | ||
maxAge: 100 // auto expire entries after (ish) 100ms | ||
maxAge: 100, // auto expire entries after (ish) 100ms | ||
createMap () { // optional function to create backing storage | ||
return new Map() | ||
} | ||
}) | ||
@@ -19,0 +22,0 @@ |
75
test.js
@@ -1,5 +0,5 @@ | ||
const tape = require('tape') | ||
const Xache = require('./') | ||
const test = require('brittle') | ||
const Xache = require('.') | ||
tape('basic', function (t) { | ||
test('basic', function (t) { | ||
const c = new Xache({ | ||
@@ -14,11 +14,11 @@ maxSize: 4 | ||
t.same([...c], [[1, true], [2, true], [3, true], [4, true]]) | ||
t.alike([...c], [[1, true], [2, true], [3, true], [4, true]]) | ||
c.set(5, true) | ||
t.same([...c], [[5, true], [1, true], [2, true], [3, true], [4, true]], 'bumped the generations') | ||
t.alike([...c], [[5, true], [1, true], [2, true], [3, true], [4, true]], 'bumped the generations') | ||
c.set(2, true) | ||
t.same([...c], [[5, true], [2, true], [1, true], [3, true], [4, true]], 'bumped the key') | ||
t.alike([...c], [[5, true], [2, true], [1, true], [3, true], [4, true]], 'bumped the key') | ||
@@ -28,4 +28,63 @@ c.set(6, true) | ||
t.same([...c], [[5, true], [2, true], [6, true], [7, true]]) | ||
t.end() | ||
t.alike([...c], [[5, true], [2, true], [6, true], [7, true]]) | ||
}) | ||
test('falsy values', function (t) { | ||
const c = new Xache({ | ||
maxSize: 4 | ||
}) | ||
for (const v of [null, undefined, false, 0, NaN, '']) { | ||
c.set(1, v) | ||
t.ok(c.has(1)) | ||
t.alike(c.get(1), v) | ||
} | ||
}) | ||
test('retain', function (t) { | ||
const c = new Xache({ | ||
maxSize: 4 | ||
}) | ||
c.retain(1, true) | ||
for (let i = 2; i < 10; i++) { | ||
c.set(i, true) | ||
} | ||
t.alike([...c], [[6, true], [7, true], [8, true], [9, true], [1, true]]) | ||
}) | ||
test('retain + set + get', function (t) { | ||
const c = new Xache({ | ||
maxSize: 4 | ||
}) | ||
c.retain(1, true) | ||
c.set(1, false) | ||
t.is(c.get(1), true) | ||
}) | ||
test('set + retain + get', function (t) { | ||
const c = new Xache({ | ||
maxSize: 4 | ||
}) | ||
c.set(1, false) | ||
c.retain(1, true) | ||
t.is(c.get(1), true) | ||
}) | ||
test('retain + clear + get', function (t) { | ||
const c = new Xache({ | ||
maxSize: 4 | ||
}) | ||
c.retain(1, true) | ||
t.is(c.get(1), true) | ||
c.clear() | ||
t.is(c.get(1), null) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6525
152
36