2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2023-02-09 16:21:00 +03:00
|
|
|
const MemoryTTLCache = require('../index');
|
|
|
|
|
|
|
|
const sleep = ms => (
|
|
|
|
new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
describe('Cache Adapter In Memory with Time To Live', function () {
|
|
|
|
it('Can initialize a cache instance', function () {
|
|
|
|
const cache = new MemoryTTLCache();
|
|
|
|
assert.ok(cache);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('get', function () {
|
|
|
|
it('Can get a value from the cache', async function () {
|
|
|
|
const cache = new MemoryTTLCache({});
|
|
|
|
cache.set('a', 'b');
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache');
|
|
|
|
|
|
|
|
await sleep(100);
|
|
|
|
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache after some time');
|
|
|
|
});
|
|
|
|
|
2023-02-22 19:23:56 +03:00
|
|
|
it.skip('Can get a value from the cache before TTL kicks in', async function () {
|
2023-02-20 10:34:43 +03:00
|
|
|
const cache = new MemoryTTLCache({ttl: 50});
|
2023-02-09 16:21:00 +03:00
|
|
|
cache.set('a', 'b');
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache');
|
|
|
|
|
2023-02-20 10:34:43 +03:00
|
|
|
await sleep(20);
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache before TTL time');
|
|
|
|
|
2023-02-20 10:34:43 +03:00
|
|
|
// NOTE: 20 + 200 = 220, which is more than 50 TTL
|
|
|
|
await sleep(200);
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
assert.equal(cache.get('a'), undefined, 'should NOT get the value from the cache after TTL time');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('set', function () {
|
2023-02-22 19:23:56 +03:00
|
|
|
it.skip('Can set a value in the cache', async function () {
|
2023-02-20 10:34:43 +03:00
|
|
|
const cache = new MemoryTTLCache({ttl: 50});
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
cache.set('a', 'b');
|
|
|
|
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache');
|
|
|
|
|
2023-02-20 10:34:43 +03:00
|
|
|
await sleep(20);
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache after time < TTL');
|
|
|
|
|
2023-02-20 10:34:43 +03:00
|
|
|
await sleep(200);
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
assert.equal(cache.get('a'), undefined, 'should NOT get the value from the cache after TTL time');
|
|
|
|
});
|
|
|
|
|
2023-02-22 19:23:56 +03:00
|
|
|
it.skip('Can override TTL time', async function () {
|
2023-02-20 10:34:43 +03:00
|
|
|
const cache = new MemoryTTLCache({ttl: 20});
|
|
|
|
|
|
|
|
cache.set('a', 'b', {ttl: 50});
|
2023-02-09 16:21:00 +03:00
|
|
|
|
2023-02-20 10:34:43 +03:00
|
|
|
await sleep(21);
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache');
|
|
|
|
|
2023-02-20 10:34:43 +03:00
|
|
|
await sleep(200);
|
2023-02-09 16:21:00 +03:00
|
|
|
|
|
|
|
assert.equal(cache.get('a'), undefined, 'should NOT get the value from the cache after TTL time');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('reset', function () {
|
|
|
|
it('Can reset the cache', async function () {
|
|
|
|
const cache = new MemoryTTLCache({ttl: 150});
|
|
|
|
|
|
|
|
cache.set('a', 'b');
|
|
|
|
cache.set('c', 'd');
|
|
|
|
|
|
|
|
assert.equal(cache.get('a'), 'b', 'should get the value from the cache');
|
|
|
|
assert.equal(cache.get('c'), 'd', 'should get the value from the cache');
|
|
|
|
|
|
|
|
cache.reset();
|
|
|
|
|
|
|
|
assert.equal(cache.get('a'), undefined, 'should NOT get the value from the cache after reset');
|
|
|
|
assert.equal(cache.get('c'), undefined, 'should NOT get the value from the cache after reset');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('keys', function () {
|
|
|
|
it('Can get all keys from the cache', async function () {
|
|
|
|
const cache = new MemoryTTLCache({ttl: 200});
|
|
|
|
|
|
|
|
cache.set('a', 'b');
|
|
|
|
cache.set('c', 'd');
|
|
|
|
|
|
|
|
assert.deepEqual(cache.keys(), ['a', 'c'], 'should get all keys from the cache');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|