@@ -8,9 +8,9 @@ type CachingClientType = RedisClientType<any, any, any, any, any>;
88type CmdFunc = ( ) => Promise < ReplyUnion > ;
99
1010export interface ClientSideCacheConfig {
11- ttl : number ;
12- maxEntries : number ;
13- lru : boolean ;
11+ ttl ? : number ;
12+ maxEntries ? : number ;
13+ lru ? : boolean ;
1414}
1515
1616type CacheCreator = {
@@ -48,7 +48,7 @@ export class ClientSideCacheEntryValue extends ClientSideCacheEntryBase {
4848 readonly #value: any ;
4949
5050 get value ( ) {
51- return structuredClone ( this . #value) ;
51+ return this . #value;
5252 }
5353
5454 constructor ( ttl : number , value : any ) {
@@ -79,7 +79,6 @@ export abstract class ClientSideCacheProvider extends EventEmitter {
7979 abstract cacheMisses ( ) : number ;
8080 abstract onError ( ) : void ;
8181 abstract onClose ( ) : void ;
82- abstract onDestroy ( ) : void ;
8382}
8483
8584export class BasicClientSideCache extends ClientSideCacheProvider {
@@ -98,7 +97,7 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
9897 this . #keyToCacheKeySetMap = new Map < string , Set < string > > ( ) ;
9998 this . ttl = config ?. ttl ?? 0 ;
10099 this . #maxEntries = config ?. maxEntries ?? 0 ;
101- this . #lru = config ?. lru ?? false ;
100+ this . #lru = config ?. lru ?? true ;
102101 }
103102
104103 /* logic of how caching works:
@@ -123,7 +122,6 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
123122 transformReply : TransformReply | undefined ,
124123 typeMapping : TypeMapping | undefined
125124 ) {
126- console . log ( "handleCache: enter" ) ;
127125 let reply : ReplyUnion ;
128126
129127 const cacheKey = parser . cacheKey ;
@@ -133,10 +131,9 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
133131 if ( cacheEntry ) {
134132 // If instanceof is "too slow", can add a "type" and then use an "as" cast to call proper getters.
135133 if ( cacheEntry instanceof ClientSideCacheEntryValue ) { // "2b1"
136- console . log ( "returning value from cache" ) ;
137134 this . #cacheHit( ) ;
138135
139- return cacheEntry . value ;
136+ return structuredClone ( cacheEntry . value ) ;
140137 } else if ( cacheEntry instanceof ClientSideCacheEntryPromise ) { // 2b2
141138 // unsure if this should be considered a cache hit, a miss, or neither?
142139 reply = await cacheEntry . promise ;
@@ -171,15 +168,14 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
171168 // 3b
172169 if ( cacheEntry . validate ( ) ) { // revalidating promise entry (dont save value, if promise entry has been invalidated)
173170 // 3c
174- console . log ( "saving value to cache" ) ;
175171 cacheEntry = this . createValueEntry ( client , val ) ;
176172 this . set ( cacheKey , cacheEntry , parser . keys ) ;
177173 this . emit ( "cached-key" , cacheKey ) ;
178174 } else {
179- console . log ( "cache entry for key got invalidated between execution and saving, so not saving" ) ;
175+ // console.log("cache entry for key got invalidated between execution and saving, so not saving");
180176 }
181177
182- return val ;
178+ return structuredClone ( val ) ;
183179 }
184180
185181 override trackingOn ( ) {
@@ -189,21 +185,24 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
189185 override invalidate ( key : RedisArgument | null ) {
190186 if ( key === null ) {
191187 this . clear ( false ) ;
188+ this . emit ( "invalidate" , key ) ;
189+
192190 return ;
193191 }
194- const set = this . #keyToCacheKeySetMap . get ( key . toString ( ) ) ;
195- if ( set ) {
196- for ( const cacheKey of set ) {
197- console . log ( `invalidate: got ${ cacheKey } from key ${ key . toString ( ) } set` ) ;
192+
193+ const keySet = this . #keyToCacheKeySetMap . get ( key . toString ( ) ) ;
194+ if ( keySet ) {
195+ for ( const cacheKey of keySet ) {
198196 const entry = this . #cacheKeyToEntryMap. get ( cacheKey ) ;
199197 if ( entry ) {
200198 entry . invalidate ( ) ;
201199 }
202200 this . #cacheKeyToEntryMap. delete ( cacheKey ) ;
203- this . emit ( "invalidate" , cacheKey ) ;
204201 }
205202 this . #keyToCacheKeySetMap. delete ( key . toString ( ) ) ;
206203 }
204+
205+ this . emit ( 'invalidate' , key ) ;
207206 }
208207
209208 override clear ( reset = true ) {
@@ -305,9 +304,9 @@ export class BasicClientSideCache extends ClientSideCacheProvider {
305304 this . clear ( ) ;
306305 }
307306
308- override onClose ( ) { }
309-
310- override onDestroy ( ) { }
307+ override onClose ( ) {
308+ this . clear ( ) ;
309+ }
311310
312311 /**
313312 * @internal
@@ -366,7 +365,11 @@ export abstract class PooledClientSideCacheProvider extends BasicClientSideCache
366365 return super . has ( cacheKey ) ;
367366 }
368367
369- onConnect ( factory : ( ) => CachingClientType ) { } ;
368+ onPoolConnect ( factory : ( ) => CachingClientType ) { } ;
369+
370+ onPoolClose ( ) {
371+ this . clear ( ) ;
372+ } ;
370373}
371374
372375// doesn't do anything special in pooling, clears cache on every client disconnect
@@ -382,6 +385,14 @@ export class BasicPooledClientSideCache extends PooledClientSideCacheProvider {
382385 override removeClient ( client : CachingClientType ) : void {
383386 return ;
384387 }
388+
389+ override onError ( ) {
390+ this . clear ( false ) ;
391+ }
392+
393+ override onClose ( ) {
394+ this . clear ( false ) ;
395+ }
385396}
386397
387398class PooledClientSideCacheEntryValue extends ClientSideCacheEntryValue {
@@ -443,7 +454,9 @@ export class PooledNoRedirectClientSideCache extends BasicPooledClientSideCache
443454 }
444455
445456 // don't clear cache on error here
446- override onError ( ) : void { }
457+ override onError ( ) { }
458+
459+ override onClose ( ) { }
447460}
448461
449462// Only clears cache on "management"/"redirect" client disconnect
@@ -482,7 +495,7 @@ export class PooledRedirectClientSideCache extends PooledClientSideCacheProvider
482495
483496 override onError ( ) : void { } ;
484497
485- override async onConnect ( factory : ( ) => CachingClientType ) {
498+ override async onPoolConnect ( factory : ( ) => CachingClientType ) {
486499 const client = factory ( ) ;
487500 this . #redirectClient = client ;
488501
@@ -502,20 +515,10 @@ export class PooledRedirectClientSideCache extends PooledClientSideCacheProvider
502515 }
503516 }
504517
505- onDestroy ( ) {
506- this . clear ( ) ;
518+ override onClose ( ) { } ;
507519
508- if ( this . #redirectClient) {
509- this . #id = undefined ;
510- const client = this . #redirectClient;
511- this . #redirectClient = undefined ;
512-
513- client . destroy ( ) ;
514- }
515- }
516-
517- override async onClose ( ) {
518- this . clear ( ) ;
520+ override onPoolClose ( ) {
521+ super . onPoolClose ( ) ;
519522
520523 if ( this . #redirectClient) {
521524 this . #id = undefined ;
0 commit comments