1 var test = require("tap").test
4 test("basic", function (t) {
5 var cache = new LRU({max: 10})
6 cache.set("key", "value")
7 t.equal(cache.get("key"), "value")
8 t.equal(cache.get("nada"), undefined)
9 t.equal(cache.length, 1)
10 t.equal(cache.max, 10)
14 test("least recently set", function (t) {
15 var cache = new LRU(2)
19 t.equal(cache.get("c"), "C")
20 t.equal(cache.get("b"), "B")
21 t.equal(cache.get("a"), undefined)
25 test("lru recently gotten", function (t) {
26 var cache = new LRU(2)
31 t.equal(cache.get("c"), "C")
32 t.equal(cache.get("b"), undefined)
33 t.equal(cache.get("a"), "A")
37 test("del", function (t) {
38 var cache = new LRU(2)
41 t.equal(cache.get("a"), undefined)
45 test("max", function (t) {
46 var cache = new LRU(3)
48 // test changing the max, verify that the LRU items get dropped.
50 for (var i = 0; i < 100; i ++) cache.set(i, i)
51 t.equal(cache.length, 100)
52 for (var i = 0; i < 100; i ++) {
53 t.equal(cache.get(i), i)
56 t.equal(cache.length, 3)
57 for (var i = 0; i < 97; i ++) {
58 t.equal(cache.get(i), undefined)
60 for (var i = 98; i < 100; i ++) {
61 t.equal(cache.get(i), i)
64 // now remove the max restriction, and try again.
66 for (var i = 0; i < 100; i ++) cache.set(i, i)
67 t.equal(cache.length, 100)
68 for (var i = 0; i < 100; i ++) {
69 t.equal(cache.get(i), i)
71 // should trigger an immediate resize
73 t.equal(cache.length, 3)
74 for (var i = 0; i < 97; i ++) {
75 t.equal(cache.get(i), undefined)
77 for (var i = 98; i < 100; i ++) {
78 t.equal(cache.get(i), i)
83 test("reset", function (t) {
84 var cache = new LRU(10)
88 t.equal(cache.length, 0)
89 t.equal(cache.max, 10)
90 t.equal(cache.get("a"), undefined)
91 t.equal(cache.get("b"), undefined)
96 test("basic with weighed length", function (t) {
99 length: function (item) { return item.size }
101 cache.set("key", {val: "value", size: 50})
102 t.equal(cache.get("key").val, "value")
103 t.equal(cache.get("nada"), undefined)
104 t.equal(cache.lengthCalculator(cache.get("key")), 50)
105 t.equal(cache.length, 50)
106 t.equal(cache.max, 100)
111 test("weighed length item too large", function (t) {
112 var cache = new LRU({
114 length: function (item) { return item.size }
116 t.equal(cache.max, 10)
118 // should fall out immediately
119 cache.set("key", {val: "value", size: 50})
121 t.equal(cache.length, 0)
122 t.equal(cache.get("key"), undefined)
126 test("least recently set with weighed length", function (t) {
127 var cache = new LRU({
129 length: function (item) { return item.length }
133 cache.set("c", "CCC")
134 cache.set("d", "DDDD")
135 t.equal(cache.get("d"), "DDDD")
136 t.equal(cache.get("c"), "CCC")
137 t.equal(cache.get("b"), undefined)
138 t.equal(cache.get("a"), undefined)
142 test("lru recently gotten with weighed length", function (t) {
143 var cache = new LRU({
145 length: function (item) { return item.length }
149 cache.set("c", "CCC")
152 cache.set("d", "DDDD")
153 t.equal(cache.get("c"), undefined)
154 t.equal(cache.get("d"), "DDDD")
155 t.equal(cache.get("b"), "BB")
156 t.equal(cache.get("a"), "A")
160 test("lru recently updated with weighed length", function (t) {
161 var cache = new LRU({
163 length: function (item) { return item.length }
167 cache.set("c", "CCC")
168 t.equal(cache.length, 6) //CCC BB A
170 t.equal(cache.length, 7) //+A CCC BB
171 cache.set("b", "++BB")
172 t.equal(cache.length, 6) //++BB +A
173 t.equal(cache.get("c"), undefined)
175 cache.set("c", "oversized")
176 t.equal(cache.length, 6) //++BB +A
177 t.equal(cache.get("c"), undefined)
179 cache.set("a", "oversized")
180 t.equal(cache.length, 4) //++BB
181 t.equal(cache.get("a"), undefined)
182 t.equal(cache.get("b"), "++BB")
186 test("set returns proper booleans", function(t) {
187 var cache = new LRU({
189 length: function (item) { return item.length }
192 t.equal(cache.set("a", "A"), true)
194 // should return false for max exceeded
195 t.equal(cache.set("b", "donuts"), false)
197 t.equal(cache.set("b", "B"), true)
198 t.equal(cache.set("c", "CCCC"), true)
202 test("drop the old items", function(t) {
203 var cache = new LRU({
210 setTimeout(function () {
212 t.equal(cache.get("a"), "A")
215 setTimeout(function () {
218 t.notOk(cache.get("a"))
221 setTimeout(function () {
222 t.notOk(cache.get("b"))
223 t.equal(cache.get("c"), "C")
226 setTimeout(function () {
227 t.notOk(cache.get("c"))
232 test("individual item can have it's own maxAge", function(t) {
233 var cache = new LRU({
238 cache.set("a", "A", 20)
239 setTimeout(function () {
240 t.notOk(cache.get("a"))
245 test("individual item can have it's own maxAge > cache's", function(t) {
246 var cache = new LRU({
251 cache.set("a", "A", 50)
252 setTimeout(function () {
253 t.equal(cache.get("a"), "A")
258 test("disposal function", function(t) {
260 var cache = new LRU({
262 dispose: function (k, n) {
277 test("disposal function on too big of item", function(t) {
279 var cache = new LRU({
281 length: function (k) {
284 dispose: function (k, n) {
290 t.equal(disposed, false)
291 cache.set("obj", obj)
292 t.equal(disposed, obj)
296 test("has()", function(t) {
297 var cache = new LRU({
302 cache.set('foo', 'bar')
303 t.equal(cache.has('foo'), true)
304 cache.set('blu', 'baz')
305 t.equal(cache.has('foo'), false)
306 t.equal(cache.has('blu'), true)
307 setTimeout(function() {
308 t.equal(cache.has('blu'), false)
313 test("stale", function(t) {
314 var cache = new LRU({
319 cache.set('foo', 'bar')
320 t.equal(cache.get('foo'), 'bar')
321 t.equal(cache.has('foo'), true)
322 setTimeout(function() {
323 t.equal(cache.has('foo'), false)
324 t.equal(cache.get('foo'), 'bar')
325 t.equal(cache.get('foo'), undefined)
330 test("lru update via set", function(t) {
331 var cache = LRU({ max: 2 });
339 t.equal(cache.get('foo'), undefined)
340 t.equal(cache.get('bar'), undefined)
341 t.equal(cache.get('baz'), 3)
342 t.equal(cache.get('qux'), 4)
346 test("least recently set w/ peek", function (t) {
347 var cache = new LRU(2)
350 t.equal(cache.peek("a"), "A")
352 t.equal(cache.get("c"), "C")
353 t.equal(cache.get("b"), "B")
354 t.equal(cache.get("a"), undefined)
358 test("pop the least used item", function (t) {
359 var cache = new LRU(3)
366 t.equal(cache.length, 3)
367 t.equal(cache.max, 3)
369 // Ensure we pop a, c, b
373 t.equal(last.key, "a")
374 t.equal(last.value, "A")
375 t.equal(cache.length, 2)
376 t.equal(cache.max, 3)
379 t.equal(last.key, "c")
380 t.equal(last.value, "C")
381 t.equal(cache.length, 1)
382 t.equal(cache.max, 3)
385 t.equal(last.key, "b")
386 t.equal(last.value, "B")
387 t.equal(cache.length, 0)
388 t.equal(cache.max, 3)
392 t.equal(cache.length, 0)
393 t.equal(cache.max, 3)