From 73aa304e6434e56bd08e73d9286611117adff0a4 Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Wed, 31 Jan 2024 13:58:47 -0500 Subject: [PATCH] move SendCache class into it's own file, not exported in index.ts --- packages/api/src/record.ts | 27 +--------------- packages/api/src/send-cache.ts | 25 +++++++++++++++ packages/api/tests/record.spec.ts | 41 +----------------------- packages/api/tests/send-cache.spec.ts | 45 +++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 66 deletions(-) create mode 100644 packages/api/src/send-cache.ts create mode 100644 packages/api/tests/send-cache.spec.ts diff --git a/packages/api/src/record.ts b/packages/api/src/record.ts index 92c6d6ebf..ee0a1fd1a 100644 --- a/packages/api/src/record.ts +++ b/packages/api/src/record.ts @@ -11,32 +11,7 @@ import { DwnInterfaceName, DwnMethodName } from '@tbd54566975/dwn-sdk-js'; import type { ResponseStatus } from './dwn-api.js'; import { dataToBlob } from './utils.js'; - -export class SendCache { - private static cache = new Map>(); - static sendCacheLimit = 100; - - static set(id: string, target: string): void { - let targetCache = SendCache.cache.get(id) || new Set(); - SendCache.cache.delete(id); - SendCache.cache.set(id, targetCache); - if (this.cache.size > SendCache.sendCacheLimit) { - const firstRecord = SendCache.cache.keys().next().value; - SendCache.cache.delete(firstRecord); - } - targetCache.delete(target); - targetCache.add(target); - if (targetCache.size > SendCache.sendCacheLimit) { - const firstTarget = targetCache.keys().next().value; - targetCache.delete(firstTarget); - } - } - - static check(id: string, target: string): boolean { - let targetCache = SendCache.cache.get(id); - return targetCache ? targetCache.has(target) : false; - } -} +import { SendCache } from './send-cache.js'; /** * Options that are passed to Record constructor. diff --git a/packages/api/src/send-cache.ts b/packages/api/src/send-cache.ts new file mode 100644 index 000000000..a8bc761bd --- /dev/null +++ b/packages/api/src/send-cache.ts @@ -0,0 +1,25 @@ +export class SendCache { + private static cache = new Map>(); + static sendCacheLimit = 100; + + static set(id: string, target: string): void { + let targetCache = SendCache.cache.get(id) || new Set(); + SendCache.cache.delete(id); + SendCache.cache.set(id, targetCache); + if (this.cache.size > SendCache.sendCacheLimit) { + const firstRecord = SendCache.cache.keys().next().value; + SendCache.cache.delete(firstRecord); + } + targetCache.delete(target); + targetCache.add(target); + if (targetCache.size > SendCache.sendCacheLimit) { + const firstTarget = targetCache.keys().next().value; + targetCache.delete(firstTarget); + } + } + + static check(id: string, target: string): boolean { + let targetCache = SendCache.cache.get(id); + return targetCache ? targetCache.has(target) : false; + } +} \ No newline at end of file diff --git a/packages/api/tests/record.spec.ts b/packages/api/tests/record.spec.ts index fe4285ada..e9c9f37b2 100644 --- a/packages/api/tests/record.spec.ts +++ b/packages/api/tests/record.spec.ts @@ -22,7 +22,7 @@ import { KeyDerivationScheme, } from '@tbd54566975/dwn-sdk-js'; -import { Record, SendCache } from '../src/record.js'; +import { Record } from '../src/record.js'; import { DwnApi } from '../src/dwn-api.js'; import { dataToBlob } from '../src/utils.js'; import { testDwnUrl } from './utils/test-config.js'; @@ -2369,43 +2369,4 @@ describe('Record', () => { expect(await storedRecord.data.text()).to.equal(updatedText); }); }); -}); - -describe('SendCache', () => { - it('sets and checks an item in the cache', async () => { - // checks for 'id' and 'target', returns false because we have not set them yet - expect(SendCache.check('id', 'target')).to.equal(false); - - // set 'id' and 'target, and then check - SendCache.set('id', 'target'); - expect(SendCache.check('id', 'target')).to.equal(true); - - // check for 'id' with a different target - expect(SendCache.check('id', 'target2')).to.equal(false); - }); - - it('purges the first item in teh cache when the cache is full (100 items)', async () => { - const recordId = 'id'; - // set 100 items in the cache to the same id - for (let i = 0; i < 100; i++) { - SendCache.set(recordId, `target-${i}`); - } - - // check that the first item is in the cache - expect(SendCache.check(recordId, 'target-0')).to.equal(true); - - // set another item in the cache - SendCache.set(recordId, 'target-new'); - - // check that the first item is no longer in the cache but the one after it is as well as the new one. - expect(SendCache.check(recordId, 'target-0')).to.equal(false); - expect(SendCache.check(recordId, 'target-1')).to.equal(true); - expect(SendCache.check(recordId, 'target-new')).to.equal(true); - - // add another item - SendCache.set(recordId, 'target-new2'); - expect(SendCache.check(recordId, 'target-1')).to.equal(false); - expect(SendCache.check(recordId, 'target-2')).to.equal(true); - expect(SendCache.check(recordId, 'target-new2')).to.equal(true); - }); }); \ No newline at end of file diff --git a/packages/api/tests/send-cache.spec.ts b/packages/api/tests/send-cache.spec.ts new file mode 100644 index 000000000..d2645d950 --- /dev/null +++ b/packages/api/tests/send-cache.spec.ts @@ -0,0 +1,45 @@ +import chai, { expect } from 'chai'; +import chaiAsPromised from 'chai-as-promised'; + +import { SendCache } from '../src/send-cache.js'; + +chai.use(chaiAsPromised); + +describe('SendCache', () => { + it('sets and checks an item in the cache', async () => { + // checks for 'id' and 'target', returns false because we have not set them yet + expect(SendCache.check('id', 'target')).to.equal(false); + + // set 'id' and 'target, and then check + SendCache.set('id', 'target'); + expect(SendCache.check('id', 'target')).to.equal(true); + + // check for 'id' with a different target + expect(SendCache.check('id', 'target2')).to.equal(false); + }); + + it('purges the first item in teh cache when the cache is full (100 items)', async () => { + const recordId = 'id'; + // set 100 items in the cache to the same id + for (let i = 0; i < 100; i++) { + SendCache.set(recordId, `target-${i}`); + } + + // check that the first item is in the cache + expect(SendCache.check(recordId, 'target-0')).to.equal(true); + + // set another item in the cache + SendCache.set(recordId, 'target-new'); + + // check that the first item is no longer in the cache but the one after it is as well as the new one. + expect(SendCache.check(recordId, 'target-0')).to.equal(false); + expect(SendCache.check(recordId, 'target-1')).to.equal(true); + expect(SendCache.check(recordId, 'target-new')).to.equal(true); + + // add another item + SendCache.set(recordId, 'target-new2'); + expect(SendCache.check(recordId, 'target-1')).to.equal(false); + expect(SendCache.check(recordId, 'target-2')).to.equal(true); + expect(SendCache.check(recordId, 'target-new2')).to.equal(true); + }); +}); \ No newline at end of file