Skip to content

Commit

Permalink
chore(scheduler-utils): do not share cache between separate invocatio…
Browse files Browse the repository at this point in the history
…ns of connect
  • Loading branch information
TillaTheHun0 committed Apr 3, 2024
1 parent af9df6a commit 2cce57f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
39 changes: 22 additions & 17 deletions scheduler-utils/src/client/in-memory.js
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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 })
}
}
34 changes: 30 additions & 4 deletions scheduler-utils/src/client/in-memory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})
})

0 comments on commit 2cce57f

Please sign in to comment.