summaryrefslogtreecommitdiff
path: root/node_modules/node-file-cache/tests/basic-test.js
blob: ea690ef1c4c1e237f5950d7ae0137cfecb61419c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';

const assert = require('assert');
const cache = require('../lib/index').create();

// cleanup
cache.clear();
assert.equal(cache.size(), 0);

// add record
const item1 = { yo: 'dawg' };
const key1 = 'mittenz';
cache.set(key1, item1);

const result1 = cache.get(key1);
assert.deepEqual(item1, result1);
assert.equal(cache.size(), 1);

// add same record
cache.set(key1, item1);
assert.deepEqual(item1, result1);
assert.equal(cache.size(), 1);

// add next record
const item2 = {
    foo: 'bar'
};
const key2 = 'kittenz'; 
cache.set(key2, item2);

const result2 = cache.get(key2);
assert.deepEqual(item2, result2);
assert.equal(cache.size(), 2);

// add record with tags
const item3 = {
    bar: 'baz'
};
const key3 = 'bat';
const tags3 = ['bat-3'];
cache.set(key3, item3, {
    tags: tags3
});

const result3 = cache.get(key3);
assert.deepEqual(item3, result3);
assert.equal(cache.size(), 3);

// expire by tags
cache.expire(tags3);
assert.equal(cache.size(), 2);

// add record with lifespan
const item4 = {
    bar: 'baziliero'
};
const key4 = 'batbaz';
const span = 1;
cache.set(key4, item4, {
    life: span
});

setTimeout(() => {
    const result4 = cache.get(key4);
    assert.equal(result4, null);
    assert.equal(cache.size(), 2);
}, 3000);