From 912d594a36d603636e1376bc463dda0b113a4aad Mon Sep 17 00:00:00 2001 From: Alberto Chamorro Date: Fri, 11 Feb 2022 12:36:57 +0100 Subject: [PATCH 1/2] chore: add test:watch script --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 2b066e8..7fc2798 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "build:packages": "parcel build packages/arch packages/utils", "watch": "parcel watch packages/arch packages/utils", "test": "jest", + "test:watch": "jest --watchAll", "test:coverage": "jest --coverage", "test:ci": "jest --ci", "format": "prettier --write --ignore-path .gitignore .", From 59e01d54fa749554d4f3d918ab73dacce9bee312 Mon Sep 17 00:00:00 2001 From: Alberto Chamorro Date: Fri, 11 Feb 2022 13:22:13 +0100 Subject: [PATCH 2/2] fix: add clean cache for each cache key --- packages/arch/src/cache/cache-manager.spec.ts | 19 +++++++++++++++++++ packages/arch/src/cache/cache-manager.ts | 2 +- packages/arch/src/cache/cache.ts | 1 + packages/arch/src/cache/lru-cache.ts | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/arch/src/cache/cache-manager.spec.ts b/packages/arch/src/cache/cache-manager.spec.ts index 8f04652..a50401f 100644 --- a/packages/arch/src/cache/cache-manager.spec.ts +++ b/packages/arch/src/cache/cache-manager.spec.ts @@ -51,6 +51,7 @@ describe('CacheManager', () => { it('should use custom cache', () => { MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z')) const cache = mock() + when(cache.create()).thenReturn(instance(cache)) when(cache.get(anyString())).thenReturn({ createdAt: 1, returns: 1 }) const { cacheManager } = setup({ cache: instance(cache) }) let count = 0 @@ -71,6 +72,7 @@ describe('CacheManager', () => { MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z')) const mockedDatetime = Datetime.now().toMillis() - 1 const cache = mock() + when(cache.create()).thenReturn(instance(cache)) when(cache.get(anyString())).thenReturn({ createdAt: mockedDatetime, returns: 1 }) const { cacheManager } = setup({ cache: instance(cache), ttl: 0 }) let count = 0 @@ -89,6 +91,7 @@ describe('CacheManager', () => { MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z')) const mockedDatetime = Datetime.now().toMillis() - 1 const cache = mock() + when(cache.create()).thenReturn(instance(cache)) when(cache.get(anyString())).thenReturn({ createdAt: mockedDatetime, returns: 1 }) const { cacheManager } = setup({ cache: instance(cache), ttl: 1 }) let count = 0 @@ -102,6 +105,22 @@ describe('CacheManager', () => { verify(cache.delete(anyString())).never() }) + + it('should use different caches for different keys', () => { + MockDatetime.mock(Datetime.fromIsoString('2020-01-01T00:00:00Z')) + const mockedDatetime = Datetime.now().toMillis() - 1 + const cache = mock() + when(cache.create()).thenReturn(instance(cache)) + when(cache.get(anyString())).thenReturn({ createdAt: mockedDatetime, returns: 1 }) + const { cacheManager } = setup({ cache: instance(cache) }) + + const fn = (): string => 'foo' + + cacheManager.set('foo', () => fn()) + cacheManager.set('bar', () => fn()) + + verify(cache.create()).twice() + }) }) function setup(cacheOptions?: Partial) { diff --git a/packages/arch/src/cache/cache-manager.ts b/packages/arch/src/cache/cache-manager.ts index 57f7d1b..32e105c 100644 --- a/packages/arch/src/cache/cache-manager.ts +++ b/packages/arch/src/cache/cache-manager.ts @@ -25,7 +25,7 @@ export class CacheManager { set(cacheKey: CacheKey, fn: (...fnArgs: unknown[]) => unknown, ...args: any[]): unknown { if (!this.caches.has(cacheKey)) { - this.caches.set(cacheKey, this.cacheOptions.cache) + this.caches.set(cacheKey, this.cacheOptions.cache.create()) } const existingCache = this.caches.get(cacheKey)! diff --git a/packages/arch/src/cache/cache.ts b/packages/arch/src/cache/cache.ts index 60200c1..792c16a 100644 --- a/packages/arch/src/cache/cache.ts +++ b/packages/arch/src/cache/cache.ts @@ -8,6 +8,7 @@ export interface CacheResult { } export interface Cache { + create(): Cache get(key: CacheKey): CacheResult | undefined set(key: CacheKey, value: CacheResult): void has(key: CacheKey): boolean diff --git a/packages/arch/src/cache/lru-cache.ts b/packages/arch/src/cache/lru-cache.ts index 760ecac..9055225 100644 --- a/packages/arch/src/cache/lru-cache.ts +++ b/packages/arch/src/cache/lru-cache.ts @@ -24,4 +24,8 @@ export class LruCache implements Cache { has(key: CacheKey) { return this._lru.has(key) } + + create(): Cache { + return new LruCache() + } }