generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move SendCache class into it's own file, not exported in index.ts
- Loading branch information
1 parent
4efdc38
commit 73aa304
Showing
4 changed files
with
72 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export class SendCache { | ||
private static cache = new Map<string, Set<string>>(); | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |