Skip to content

Commit

Permalink
Merge pull request #13 from SaurusXI/feat/improvements
Browse files Browse the repository at this point in the history
Feat/improvements
  • Loading branch information
SaurusXI authored Jan 11, 2023
2 parents b4d8916 + bdbd86a commit 2ed4203
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
on:
push:
branches: [main, tests]
pull_request:
branches: [main]

jobs:
tests:
Expand Down
18 changes: 16 additions & 2 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,26 @@ export class SugarCache {
* This is an expensive operation (since it operates on all keys inside a namespace) and should be used with care
*/
public clear = async () => {
if (this.redis instanceof Cluster) {
const deletionCandidateKeysMap = await Promise.all((this.redis as Cluster).nodes('master')
.map(async (node) => {
return node.keys(`${this.namespace}*`);

}));
const deletionCandidateKeys = [].concat(...deletionCandidateKeysMap);
this.logger.debug(`[SugarCache:${this.namespace}] Deletion candidate keys - ${deletionCandidateKeys}`);
await Promise.all(deletionCandidateKeys.map(k => this.redis.del(k)));
this.logger.debug(`[SugarCache:${this.namespace}] Deletion keys removed`);
return;
}
const deletionCandidateKeys = await this.redis.keys(`${this.namespace}*`);

this.logger.debug(`[SugarCache:${this.namespace}] Deletion candidate keys - ${deletionCandidateKeys}`);
if (!deletionCandidateKeys.length) return;

await this.redis.del(...deletionCandidateKeys);

await this.redis.del(deletionCandidateKeys);

this.logger.debug(`[SugarCache:${this.namespace}] Deletion keys removed`);
}

// ----------- Decorator Methods -----------
Expand Down
10 changes: 5 additions & 5 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Functional tests', () => {
it('TTL based eviction', async () => {
await cacheBasic.set(mockKey, mockVal, ttl);
await new Promise((resolve) => {
setTimeout(resolve, ttl)
setTimeout(resolve, ttl * 1.1)
});
const cachedVal = await cacheBasic.get(mockKey);
expect(cachedVal).toBeNull();
Expand Down Expand Up @@ -90,13 +90,13 @@ describe('Functional tests', () => {
const resourceCategory = 'org-uuid';
const resourceId = 'resource-uuid';

it('getOrSet first call', async () => {
it('cacheFnResult first call', async () => {
await cacheBasic.clear();
const response = await controller.read(resourceId, resourceCategory);
expect(response).toStrictEqual({ res: resourceCategory + resourceId });
});

it('getOrSet cached call', async () => {
it('cacheFnResult cached call', async () => {
const response = await controller.read(resourceId, resourceCategory);
expect(response).toStrictEqual({ res: resourceCategory + resourceId });
}, (mockLatency / 2));
Expand All @@ -108,13 +108,13 @@ describe('Functional tests', () => {
expect(cachedValue).toBeNull();
})

it('compound getOrSet first call', async () => {
it('compound cacheFnResult first call', async () => {
await secondCacheBasic.clear();
const response = await controller.compoundRead(resourceId, resourceCategory);
expect(response).toStrictEqual({ res: resourceCategory + resourceId });
});

it('compound getOrSet cached call', async () => {
it('compound cacheFnResult cached call', async () => {
const response = await controller.compoundRead(resourceId, resourceCategory);
expect(response).toStrictEqual({ res: resourceCategory + resourceId });
}, mockLatency);
Expand Down

0 comments on commit 2ed4203

Please sign in to comment.