@@ -42,7 +42,7 @@ import {
4242} from '../permission'
4343import { filter , map } from '../utils/GeneratorUtils'
4444import { LoggerFactory } from '../utils/LoggerFactory'
45- import { CacheAsyncFn , CacheAsyncFnType } from '../utils/CacheAsyncFn '
45+ import { CachingMap } from '../utils/CachingMap '
4646import { until } from '../utils/promises'
4747import { ChainEventPoller } from './ChainEventPoller'
4848import { ContractFactory } from './ContractFactory'
@@ -119,10 +119,10 @@ export class StreamRegistry {
119119 private readonly config : Pick < StrictStreamrClientConfig , 'contracts' | 'cache' | '_timeouts' >
120120 private readonly authentication : Authentication
121121 private readonly logger : Logger
122- private readonly getStreamMetadata_cached : CacheAsyncFnType < [ StreamID ] , StreamMetadata , string >
123- private readonly isStreamPublisher_cached : CacheAsyncFnType < [ StreamID , UserID ] , boolean , string >
124- private readonly isStreamSubscriber_cached : CacheAsyncFnType < [ StreamID , UserID ] , boolean , string >
125- private readonly hasPublicSubscribePermission_cached : CacheAsyncFnType < [ StreamID ] , boolean , string >
122+ private readonly getStreamMetadata_cached : CachingMap < string , StreamMetadata , [ StreamID ] >
123+ private readonly isStreamPublisher_cached : CachingMap < string , boolean , [ StreamID , UserID ] >
124+ private readonly isStreamSubscriber_cached : CachingMap < string , boolean , [ StreamID , UserID ] >
125+ private readonly hasPublicSubscribePermission_cached : CachingMap < string , boolean , [ StreamID ] >
126126
127127 /** @internal */
128128 constructor (
@@ -163,31 +163,31 @@ export class StreamRegistry {
163163 } ) ,
164164 loggerFactory
165165 } )
166- this . getStreamMetadata_cached = CacheAsyncFn ( ( streamId : StreamID ) => {
166+ this . getStreamMetadata_cached = new CachingMap ( ( streamId : StreamID ) => {
167167 return this . getStreamMetadata_nonCached ( streamId )
168168 } , {
169169 ...config . cache ,
170170 cacheKey : ( [ streamId ] ) : string => {
171171 return `${ streamId } ${ CACHE_KEY_SEPARATOR } `
172172 }
173173 } )
174- this . isStreamPublisher_cached = CacheAsyncFn ( ( streamId : StreamID , userId : UserID ) => {
174+ this . isStreamPublisher_cached = new CachingMap ( ( streamId : StreamID , userId : UserID ) => {
175175 return this . isStreamPublisher ( streamId , userId , false )
176176 } , {
177177 ...config . cache ,
178178 cacheKey ( [ streamId , userId ] ) : string {
179179 return [ streamId , userId ] . join ( CACHE_KEY_SEPARATOR )
180180 }
181181 } )
182- this . isStreamSubscriber_cached = CacheAsyncFn ( ( streamId : StreamID , userId : UserID ) => {
182+ this . isStreamSubscriber_cached = new CachingMap ( ( streamId : StreamID , userId : UserID ) => {
183183 return this . isStreamSubscriber ( streamId , userId , false )
184184 } , {
185185 ...config . cache ,
186186 cacheKey ( [ streamId , userId ] ) : string {
187187 return [ streamId , userId ] . join ( CACHE_KEY_SEPARATOR )
188188 }
189189 } )
190- this . hasPublicSubscribePermission_cached = CacheAsyncFn ( ( streamId : StreamID ) => {
190+ this . hasPublicSubscribePermission_cached = new CachingMap ( ( streamId : StreamID ) => {
191191 return this . hasPermission ( {
192192 streamId,
193193 public : true ,
@@ -264,7 +264,7 @@ export class StreamRegistry {
264264 JSON . stringify ( metadata ) ,
265265 ethersOverrides
266266 ) )
267- this . clearStreamCache ( streamId )
267+ this . invalidateStreamCache ( streamId )
268268 }
269269
270270 async deleteStream ( streamIdOrPath : string ) : Promise < void > {
@@ -275,7 +275,7 @@ export class StreamRegistry {
275275 streamId ,
276276 ethersOverrides
277277 ) )
278- this . clearStreamCache ( streamId )
278+ this . invalidateStreamCache ( streamId )
279279 }
280280
281281 private async streamExistsOnChain ( streamIdOrPath : string ) : Promise < boolean > {
@@ -462,7 +462,7 @@ export class StreamRegistry {
462462 ...assignments : InternalPermissionAssignment [ ]
463463 ) : Promise < void > {
464464 const streamId = await this . streamIdBuilder . toStreamID ( streamIdOrPath )
465- this . clearStreamCache ( streamId )
465+ this . invalidateStreamCache ( streamId )
466466 await this . connectToContract ( )
467467 for ( const assignment of assignments ) {
468468 for ( const permission of assignment . permissions ) {
@@ -484,7 +484,7 @@ export class StreamRegistry {
484484 for ( const item of items ) {
485485 validatePermissionAssignments ( item . assignments )
486486 const streamId = await this . streamIdBuilder . toStreamID ( item . streamId )
487- this . clearStreamCache ( streamId )
487+ this . invalidateStreamCache ( streamId )
488488 streamIds . push ( streamId )
489489 targets . push ( item . assignments . map ( ( assignment ) => {
490490 return isPublicPermissionAssignment ( assignment ) ? PUBLIC_PERMISSION_USER_ID : assignment . userId
@@ -518,40 +518,40 @@ export class StreamRegistry {
518518
519519 getStreamMetadata ( streamId : StreamID , useCache = true ) : Promise < StreamMetadata > {
520520 if ( useCache ) {
521- return this . getStreamMetadata_cached ( streamId )
521+ return this . getStreamMetadata_cached . get ( streamId )
522522 } else {
523523 return this . getStreamMetadata_nonCached ( streamId )
524524 }
525525 }
526526
527527 isStreamPublisher ( streamId : StreamID , userId : UserID , useCache = true ) : Promise < boolean > {
528528 if ( useCache ) {
529- return this . isStreamPublisher_cached ( streamId , userId )
529+ return this . isStreamPublisher_cached . get ( streamId , userId )
530530 } else {
531531 return this . isStreamPublisherOrSubscriber_nonCached ( streamId , userId , StreamPermission . PUBLISH )
532532 }
533533 }
534534
535535 isStreamSubscriber ( streamId : StreamID , userId : UserID , useCache = true ) : Promise < boolean > {
536536 if ( useCache ) {
537- return this . isStreamSubscriber_cached ( streamId , userId )
537+ return this . isStreamSubscriber_cached . get ( streamId , userId )
538538 } else {
539539 return this . isStreamPublisherOrSubscriber_nonCached ( streamId , userId , StreamPermission . SUBSCRIBE )
540540 }
541541 }
542542
543543 hasPublicSubscribePermission ( streamId : StreamID ) : Promise < boolean > {
544- return this . hasPublicSubscribePermission_cached ( streamId )
544+ return this . hasPublicSubscribePermission_cached . get ( streamId )
545545 }
546546
547- clearStreamCache ( streamId : StreamID ) : void {
547+ invalidateStreamCache ( streamId : StreamID ) : void {
548548 this . logger . debug ( 'Clear caches matching stream' , { streamId } )
549549 // include separator so startsWith(streamid) doesn't match streamid-something
550550 const target = `${ streamId } ${ CACHE_KEY_SEPARATOR } `
551551 const matchTarget = ( s : string ) => s . startsWith ( target )
552- this . getStreamMetadata_cached . clearMatching ( matchTarget )
553- this . isStreamPublisher_cached . clearMatching ( matchTarget )
554- this . isStreamSubscriber_cached . clearMatching ( matchTarget )
552+ this . getStreamMetadata_cached . invalidate ( matchTarget )
553+ this . isStreamPublisher_cached . invalidate ( matchTarget )
554+ this . isStreamSubscriber_cached . invalidate ( matchTarget )
555555 // TODO should also clear cache for hasPublicSubscribePermission?
556556 }
557557}
0 commit comments