Skip to content

Commit

Permalink
invalidateCache() helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
teogeb committed Nov 18, 2024
1 parent 6cea1c7 commit c5e2f58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
42 changes: 19 additions & 23 deletions packages/sdk/src/contracts/StreamRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ const formCacheKeyPrefix = (streamId: StreamID): string => {
return `${streamId}|`
}

const invalidateCache = (cache: CachingMap<string, any, any>, streamId: StreamID) => {
const matchTarget = (s: string) => s.startsWith(formCacheKeyPrefix(streamId))
cache.invalidate(matchTarget)
}

@scoped(Lifecycle.ContainerScoped)
export class StreamRegistry {

Expand Down Expand Up @@ -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}`
Expand Down Expand Up @@ -270,7 +275,7 @@ export class StreamRegistry {
streamId,
ethersOverrides
))
this.invalidateMetadataCache(streamId)
invalidateCache(this.getStreamMetadata_cached, streamId)
this.invalidatePermissionCaches(streamId)
}

Expand Down Expand Up @@ -512,48 +517,39 @@ export class StreamRegistry {
// Caching
// --------------------------------------------------------------------------------------------

getStreamMetadata(streamId: StreamID, useCache = true): Promise<StreamMetadata> {
if (useCache) {
return this.getStreamMetadata_cached.get(streamId)
} else {
return this.getStreamMetadata_nonCached(streamId)
async getStreamMetadata(streamId: StreamID, useCache = true): Promise<StreamMetadata> {
if (!useCache) {
invalidateCache(this.getStreamMetadata_cached, streamId)
}
return this.getStreamMetadata_cached.get(streamId)
}

isStreamPublisher(streamId: StreamID, userId: UserID, useCache = true): Promise<boolean> {
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<boolean> {
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<boolean> {
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?
}
}
10 changes: 0 additions & 10 deletions packages/sdk/test/test-utils/fake/FakeStreamRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,6 @@ export class FakeStreamRegistry implements Methods<StreamRegistry> {
})
}

// 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
Expand Down

0 comments on commit c5e2f58

Please sign in to comment.