From c5e2f583203132bc96b9a8060de6472ebba0158a Mon Sep 17 00:00:00 2001 From: Teo Gebhard Date: Mon, 18 Nov 2024 12:21:45 +0200 Subject: [PATCH] invalidateCache() helper function --- packages/sdk/src/contracts/StreamRegistry.ts | 42 +++++++++---------- .../test-utils/fake/FakeStreamRegistry.ts | 10 ----- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/packages/sdk/src/contracts/StreamRegistry.ts b/packages/sdk/src/contracts/StreamRegistry.ts index 3f925d2248..696c191429 100644 --- a/packages/sdk/src/contracts/StreamRegistry.ts +++ b/packages/sdk/src/contracts/StreamRegistry.ts @@ -108,6 +108,11 @@ const formCacheKeyPrefix = (streamId: StreamID): string => { return `${streamId}|` } +const invalidateCache = (cache: CachingMap, streamId: StreamID) => { + const matchTarget = (s: string) => s.startsWith(formCacheKeyPrefix(streamId)) + cache.invalidate(matchTarget) +} + @scoped(Lifecycle.ContainerScoped) export class StreamRegistry { @@ -172,13 +177,13 @@ export class StreamRegistry { cacheKey: ([streamId]) => formCacheKeyPrefix(streamId) }) this.isStreamPublisher_cached = new CachingMap((streamId: StreamID, userId: UserID) => { - return this.isStreamPublisher(streamId, userId, false) + return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.PUBLISH) }, { ...config.cache, cacheKey: ([streamId, userId]) =>`${formCacheKeyPrefix(streamId)}${userId}` }) this.isStreamSubscriber_cached = new CachingMap((streamId: StreamID, userId: UserID) => { - return this.isStreamSubscriber(streamId, userId, false) + return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.SUBSCRIBE) }, { ...config.cache, cacheKey: ([streamId, userId]) =>`${formCacheKeyPrefix(streamId)}${userId}` @@ -270,7 +275,7 @@ export class StreamRegistry { streamId, ethersOverrides )) - this.invalidateMetadataCache(streamId) + invalidateCache(this.getStreamMetadata_cached, streamId) this.invalidatePermissionCaches(streamId) } @@ -512,48 +517,39 @@ export class StreamRegistry { // Caching // -------------------------------------------------------------------------------------------- - getStreamMetadata(streamId: StreamID, useCache = true): Promise { - if (useCache) { - return this.getStreamMetadata_cached.get(streamId) - } else { - return this.getStreamMetadata_nonCached(streamId) + async getStreamMetadata(streamId: StreamID, useCache = true): Promise { + if (!useCache) { + invalidateCache(this.getStreamMetadata_cached, streamId) } + return this.getStreamMetadata_cached.get(streamId) } isStreamPublisher(streamId: StreamID, userId: UserID, useCache = true): Promise { if (useCache) { - return this.isStreamPublisher_cached.get(streamId, userId) - } else { - return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.PUBLISH) + invalidateCache(this.isStreamPublisher_cached, streamId) } + return this.isStreamPublisher_cached.get(streamId, userId) } isStreamSubscriber(streamId: StreamID, userId: UserID, useCache = true): Promise { if (useCache) { - return this.isStreamSubscriber_cached.get(streamId, userId) - } else { - return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.SUBSCRIBE) + invalidateCache(this.isStreamSubscriber_cached, streamId) } + return this.isStreamSubscriber_cached.get(streamId, userId) } hasPublicSubscribePermission(streamId: StreamID): Promise { return this.hasPublicSubscribePermission_cached.get(streamId) } - populateMetadataCache(streamId: StreamID, metadata: StreamMetadata): void { + private populateMetadataCache(streamId: StreamID, metadata: StreamMetadata): void { this.getStreamMetadata_cached.set([streamId], metadata) } - - invalidateMetadataCache(streamId: StreamID): void { - this.logger.trace('Clear metadata cache for stream', { streamId }) - this.getStreamMetadata_cached.invalidate((s) => s.startsWith(formCacheKeyPrefix(streamId))) - } invalidatePermissionCaches(streamId: StreamID): void { this.logger.trace('Clear permission caches for stream', { streamId }) - const matchTarget = (s: string) => s.startsWith(formCacheKeyPrefix(streamId)) - this.isStreamPublisher_cached.invalidate(matchTarget) - this.isStreamSubscriber_cached.invalidate(matchTarget) + invalidateCache(this.isStreamPublisher_cached, streamId) + invalidateCache(this.isStreamSubscriber_cached, streamId) // TODO should also clear cache for hasPublicSubscribePermission? } } diff --git a/packages/sdk/test/test-utils/fake/FakeStreamRegistry.ts b/packages/sdk/test/test-utils/fake/FakeStreamRegistry.ts index cdae1ff4db..984f6df2f5 100644 --- a/packages/sdk/test/test-utils/fake/FakeStreamRegistry.ts +++ b/packages/sdk/test/test-utils/fake/FakeStreamRegistry.ts @@ -167,16 +167,6 @@ export class FakeStreamRegistry implements Methods { }) } - // eslint-disable-next-line class-methods-use-this - populateMetadataCache(): void { - // no-op - } - - // eslint-disable-next-line class-methods-use-this - invalidateMetadataCache(): void { - // no-op - } - // eslint-disable-next-line class-methods-use-this invalidatePermissionCaches(): void { // no-op