Skip to content

Commit

Permalink
Add TTL Cache to common package
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Hinek <[email protected]>
  • Loading branch information
frankhinek committed Feb 15, 2024
1 parent c72b442 commit 5734a76
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 3 deletions.
131 changes: 129 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web5/common",
"version": "0.2.3",
"version": "0.2.4",
"type": "module",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
Expand Down Expand Up @@ -68,6 +68,7 @@
"node": ">=18.0.0"
},
"dependencies": {
"@isaacs/ttlcache": "1.4.1",
"level": "8.0.0",
"multiformats": "11.0.2",
"readable-stream": "4.4.2"
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import TTLCache from '@isaacs/ttlcache';
export { TTLCache as TtlCache };
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type * from './types.js';

export * from './cache.js';
export * from './convert.js';
export * from './multicodec.js';
export * from './object.js';
Expand Down
28 changes: 28 additions & 0 deletions packages/common/tests/cache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { expect } from 'chai';

import { TtlCache } from '../src/cache.js';

describe('TTLCache', function () {
it('should store and retrieve string values', function () {
const cache = new TtlCache({ max: 10000, ttl: 1000 });
cache.set('key1', 'value1');

expect(cache.has('key1')).to.be.true;
expect(cache.get('key1')).to.equal('value1');

expect(cache.has('key1')).to.be.true;
expect(cache.get('key1')).to.equal('value1');
});

it('should store and retrieve object values', function () {
const cache = new TtlCache({ max: 10000, ttl: 1000 });
const value = { prop: 'value' };
cache.set('key2', value);

expect(cache.has('key2')).to.be.true;
expect(cache.get('key2')).to.deep.equal(value);

expect(cache.has('key2')).to.be.true;
expect(cache.get('key2')).to.deep.equal(value);
});
});

0 comments on commit 5734a76

Please sign in to comment.