diff --git a/scheduler-utils/src/client/in-memory.js b/scheduler-utils/src/client/in-memory.js index fe3a8131e..1bd91dd2c 100644 --- a/scheduler-utils/src/client/in-memory.js +++ b/scheduler-utils/src/client/in-memory.js @@ -1,14 +1,7 @@ import { LRUCache } from 'lru-cache' -/** - * @type {LRUCache} - */ -let internalCache -let internalSize export function createLruCache ({ size }) { - if (internalCache) return internalCache - internalSize = size - internalCache = new LRUCache({ + const cache = new LRUCache({ /** * number of entries */ @@ -25,33 +18,45 @@ export function createLruCache ({ size }) { sizeCalculation: (v) => JSON.stringify(v).length, allowStale: true }) - return internalCache + return cache } -export function getByProcessWith ({ cache = internalCache }) { +/** + * @param {{ cache: LRUCache }} params + */ +export function getByProcessWith ({ cache }) { return async (process) => { - if (!internalSize) return + if (!cache.max) return return cache.get(process) } } -export function setByProcessWith ({ cache = internalCache }) { +/** + * @param {{ cache: LRUCache }} params + */ +export function setByProcessWith ({ cache }) { return async (process, { url, address }, ttl) => { - if (!internalSize) return + if (!cache.max) return return cache.set(process, { url, address }, { ttl }) } } -export function getByOwnerWith ({ cache = internalCache }) { +/** + * @param {{ cache: LRUCache }} params + */ +export function getByOwnerWith ({ cache }) { return async (owner) => { - if (!internalSize) return + if (!cache.max) return return cache.get(owner) } } -export function setByOwnerWith ({ cache = internalCache }) { +/** + * @param {{ cache: LRUCache }} params + */ +export function setByOwnerWith ({ cache }) { return async (owner, url, ttl) => { - if (!internalSize) return + if (!cache.max) return return cache.set(owner, { url, address: owner }, { ttl }) } } diff --git a/scheduler-utils/src/client/in-memory.test.js b/scheduler-utils/src/client/in-memory.test.js index 03b49d71c..c0df219ab 100644 --- a/scheduler-utils/src/client/in-memory.test.js +++ b/scheduler-utils/src/client/in-memory.test.js @@ -17,36 +17,62 @@ describe('in-memory', () => { }) describe('getByProcessWith', () => { - const getByProcess = getByProcessWith({ cache }) test('returns the url if in cache', async () => { + const getByProcess = getByProcessWith({ cache }) assert.equal(await getByProcess(PROCESS), undefined) cache.set(PROCESS, { url: DOMAIN, address: SCHEDULER }) assert.deepStrictEqual(await getByProcess(PROCESS), { url: DOMAIN, address: SCHEDULER }) }) + + test('returns undefined if cache size is set to 0', async () => { + const getByProcess = getByProcessWith({ cache: createLruCache({ size: 0 }) }) + assert.equal(await getByProcess(PROCESS), undefined) + cache.set(PROCESS, { url: DOMAIN, address: SCHEDULER }) + assert.deepStrictEqual(await getByProcess(PROCESS), undefined) + }) }) describe('getByOwnerWith', () => { - const getByOwner = getByOwnerWith({ cache }) test('returns the url if in cache', async () => { + const getByOwner = getByOwnerWith({ cache }) assert.equal(await getByOwner(SCHEDULER), undefined) cache.set(SCHEDULER, { url: DOMAIN, address: SCHEDULER }) assert.deepStrictEqual(await getByOwner(SCHEDULER), { url: DOMAIN, address: SCHEDULER }) }) + + test('returns undefined if cache size is set to 0', async () => { + const getByOwner = getByOwnerWith({ cache: createLruCache({ size: 0 }) }) + assert.equal(await getByOwner(SCHEDULER), undefined) + cache.set(SCHEDULER, { url: DOMAIN, address: SCHEDULER }) + assert.deepStrictEqual(await getByOwner(SCHEDULER), undefined) + }) }) describe('setByProcessWith', () => { - const setByProcess = setByProcessWith({ cache }) test('sets the value in cache', async () => { + const setByProcess = setByProcessWith({ cache }) await setByProcess(PROCESS, DOMAIN, TEN_MS) assert.ok(cache.has(PROCESS)) }) + + test('does nothing if cache size is set to 0', async () => { + const setByProcess = setByProcessWith({ cache: createLruCache({ size: 0 }) }) + await setByProcess(PROCESS, DOMAIN, TEN_MS) + assert.ok(!cache.has(PROCESS)) + }) }) describe('setByOwnerWith', () => { - const setByOwner = setByOwnerWith({ cache }) test('sets the value in cache', async () => { + const setByOwner = setByOwnerWith({ cache }) await setByOwner(SCHEDULER, DOMAIN, TEN_MS) assert.ok(cache.has(SCHEDULER)) }) + + test('does nothing if cache size is set to 0', async () => { + const setByOwner = setByOwnerWith({ cache: createLruCache({ size: 0 }) }) + await setByOwner(SCHEDULER, DOMAIN, TEN_MS) + assert.ok(!cache.has(SCHEDULER)) + }) }) })