Skip to content

Commit

Permalink
move SendCache class into it's own file, not exported in index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Jan 31, 2024
1 parent 4efdc38 commit 73aa304
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
27 changes: 1 addition & 26 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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;
}
}
import { SendCache } from './send-cache.js';

/**
* Options that are passed to Record constructor.
Expand Down
25 changes: 25 additions & 0 deletions packages/api/src/send-cache.ts
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);
}

Check warning on line 12 in packages/api/src/send-cache.ts

View check run for this annotation

Codecov / codecov/patch

packages/api/src/send-cache.ts#L10-L12

Added lines #L10 - L12 were not covered by tests
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;
}
}
41 changes: 1 addition & 40 deletions packages/api/tests/record.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
45 changes: 45 additions & 0 deletions packages/api/tests/send-cache.spec.ts
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);
});
});

0 comments on commit 73aa304

Please sign in to comment.