1 var test = require('tap').test
2 var LRU = require('../')
4 test('dump', function (t) {
7 t.equal(cache.dump().length, 0, "nothing in dump for empty cache")
11 t.deepEqual(cache.dump(), [
12 { k: "b", v: "B", e: 0 },
13 { k: "a", v: "A", e: 0 }
17 t.deepEqual(cache.dump(), [
18 { k: "a", v: "A", e: 0 },
19 { k: "b", v: "B", e: 0 }
23 t.deepEqual(cache.dump(), [
24 { k: "b", v: "B", e: 0 },
25 { k: "a", v: "A", e: 0 }
29 t.deepEqual(cache.dump(), [
30 { k: "b", v: "B", e: 0 }
36 test("do not dump stale items", function(t) {
45 setTimeout(function () {
54 setTimeout(function () {
63 setTimeout(function () {
65 cache.set("d", "D", 40)
72 setTimeout(function () {
78 setTimeout(function () {
85 test("load basic cache", function(t) {
86 var cache = new LRU(),
92 copy.load(cache.dump())
93 t.deepEquals(cache.dump(), copy.dump())
99 test("load staled cache", function(t) {
100 var cache = new LRU({maxAge: 50}),
101 copy = new LRU({maxAge: 50}),
106 setTimeout(function () {
110 t.equal(arr.length, 2)
113 setTimeout(function () {
115 t.equal(copy.get("a"), undefined)
116 t.equal(copy.get("b"), "B")
119 setTimeout(function () {
120 t.equal(copy.get("b"), undefined)
125 test("load to other size cache", function(t) {
126 var cache = new LRU({max: 2}),
127 copy = new LRU({max: 1})
132 copy.load(cache.dump())
133 t.equal(copy.get("a"), undefined)
134 t.equal(copy.get("b"), "B")
136 //update the last read from original cache
138 copy.load(cache.dump())
139 t.equal(copy.get("a"), "A")
140 t.equal(copy.get("b"), undefined)
146 test("load to other age cache", function(t) {
147 var cache = new LRU({maxAge: 50}),
148 aged = new LRU({maxAge: 100}),
154 //a would be valid till 0 + 50
156 setTimeout(function () {
158 //b would be valid till 20 + 50
160 //b would be valid till 20 + 70
161 cache.set("c", "C", 70)
163 t.equal(arr.length, 3)
166 setTimeout(function () {
167 t.equal(cache.get("a"), undefined)
168 t.equal(cache.get("b"), "B")
169 t.equal(cache.get("c"), "C")
172 t.equal(aged.get("a"), undefined)
173 t.equal(aged.get("b"), "B")
174 t.equal(aged.get("c"), "C")
177 t.equal(simple.get("a"), undefined)
178 t.equal(simple.get("b"), "B")
179 t.equal(simple.get("c"), "C")
182 setTimeout(function () {
183 t.equal(cache.get("a"), undefined)
184 t.equal(cache.get("b"), undefined)
185 t.equal(cache.get("c"), "C")
188 t.equal(aged.get("a"), undefined)
189 t.equal(aged.get("b"), undefined)
190 t.equal(aged.get("c"), "C")
193 t.equal(simple.get("a"), undefined)
194 t.equal(simple.get("b"), undefined)
195 t.equal(simple.get("c"), "C")
198 setTimeout(function () {
199 t.equal(cache.get("a"), undefined)
200 t.equal(cache.get("b"), undefined)
201 t.equal(cache.get("c"), undefined)
204 t.equal(aged.get("a"), undefined)
205 t.equal(aged.get("b"), undefined)
206 t.equal(aged.get("c"), undefined)
209 t.equal(simple.get("a"), undefined)
210 t.equal(simple.get("b"), undefined)
211 t.equal(simple.get("c"), undefined)