From 5734a7654623a05dab3221b8080e48fb4229c101 Mon Sep 17 00:00:00 2001 From: Frank Hinek Date: Thu, 15 Feb 2024 16:44:06 -0500 Subject: [PATCH] Add TTL Cache to common package Signed-off-by: Frank Hinek --- package-lock.json | 131 +++++++++++++++++++++++++++- packages/common/package.json | 3 +- packages/common/src/cache.ts | 2 + packages/common/src/index.ts | 1 + packages/common/tests/cache.spec.ts | 28 ++++++ 5 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 packages/common/src/cache.ts create mode 100644 packages/common/tests/cache.spec.ts diff --git a/package-lock.json b/package-lock.json index 3c4d6eb52..1647b3a4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1358,6 +1358,14 @@ "url": "https://github.com/chalk/strip-ansi?sponsor=1" } }, + "node_modules/@isaacs/ttlcache": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/@isaacs/ttlcache/-/ttlcache-1.4.1.tgz", + "integrity": "sha512-RQgQ4uQ+pLbqXfOmieB91ejmLwvSgv9nLx6sT6sD83s7umBypgg+OIBOBbEUiJXrfpnp9j0mRhYYdzp9uqq3lA==", + "engines": { + "node": ">=12" + } + }, "node_modules/@istanbuljs/schema": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.3.tgz", @@ -14336,6 +14344,19 @@ } } }, + "packages/agent/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/agent/node_modules/@web5/crypto": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/@web5/crypto/-/crypto-0.2.2.tgz", @@ -14733,6 +14754,19 @@ } } }, + "packages/api/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/api/node_modules/@web5/crypto": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/@web5/crypto/-/crypto-0.2.2.tgz", @@ -14986,9 +15020,10 @@ }, "packages/common": { "name": "@web5/common", - "version": "0.2.3", + "version": "0.2.4", "license": "Apache-2.0", "dependencies": { + "@isaacs/ttlcache": "1.4.1", "level": "8.0.0", "multiformats": "11.0.2", "readable-stream": "4.4.2" @@ -15292,7 +15327,7 @@ }, "packages/credentials": { "name": "@web5/credentials", - "version": "0.4.1", + "version": "0.4.2", "license": "Apache-2.0", "dependencies": { "@sphereon/pex": "2.1.0", @@ -15394,6 +15429,19 @@ } } }, + "packages/credentials/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/credentials/node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -15730,6 +15778,20 @@ } } }, + "packages/crypto-aws-kms/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dev": true, + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/crypto-aws-kms/node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -15990,6 +16052,19 @@ } } }, + "packages/crypto/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/crypto/node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -16293,6 +16368,19 @@ } } }, + "packages/dids/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/dids/node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -16621,6 +16709,19 @@ } } }, + "packages/identity-agent/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/identity-agent/node_modules/@web5/crypto": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/@web5/crypto/-/crypto-0.2.2.tgz", @@ -17008,6 +17109,19 @@ } } }, + "packages/proxy-agent/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/proxy-agent/node_modules/@web5/crypto": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/@web5/crypto/-/crypto-0.2.2.tgz", @@ -17395,6 +17509,19 @@ } } }, + "packages/user-agent/node_modules/@web5/common": { + "version": "0.2.3", + "resolved": "https://registry.npmjs.org/@web5/common/-/common-0.2.3.tgz", + "integrity": "sha512-WTbIS6l5inrQTS5cwOoQP5KEuUHZqOWCKCDAA62qGUn4QyySolog9Gt2HCNUEClIzzk/d5DHcpBgLCadHoJ6rQ==", + "dependencies": { + "level": "8.0.0", + "multiformats": "11.0.2", + "readable-stream": "4.4.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, "packages/user-agent/node_modules/@web5/crypto": { "version": "0.2.2", "resolved": "https://registry.npmjs.org/@web5/crypto/-/crypto-0.2.2.tgz", diff --git a/packages/common/package.json b/packages/common/package.json index 8779f29da..b2133b44a 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -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", @@ -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" diff --git a/packages/common/src/cache.ts b/packages/common/src/cache.ts new file mode 100644 index 000000000..1b883cd42 --- /dev/null +++ b/packages/common/src/cache.ts @@ -0,0 +1,2 @@ +import TTLCache from '@isaacs/ttlcache'; +export { TTLCache as TtlCache }; \ No newline at end of file diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index 9766464cf..1a6095561 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -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'; diff --git a/packages/common/tests/cache.spec.ts b/packages/common/tests/cache.spec.ts new file mode 100644 index 000000000..a7aef412b --- /dev/null +++ b/packages/common/tests/cache.spec.ts @@ -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); + }); +});