1- import type { CacheInvalidationOptions , CacheProvider , CacheQueryResultEntry } from '../types' ;
1+ import type { CacheInvalidationOptions , CacheProvider , CacheEntry } from '../types' ;
22import { entryIsExpired } from '../utils' ;
33
4- export class MemoryCache implements CacheProvider {
5- private readonly queryResultStore : Map < string , CacheQueryResultEntry > ;
4+ export class MemoryCacheProvider implements CacheProvider {
5+ private readonly entryStore : Map < string , CacheEntry > ;
66 private readonly tagStore : Map < string , Set < string > > ;
77
88 constructor ( private readonly options ?: MemoryCacheOptions ) {
9- this . queryResultStore = new Map < string , CacheQueryResultEntry > ( ) ;
9+ this . entryStore = new Map < string , CacheEntry > ( ) ;
1010 this . tagStore = new Map < string , Set < string > > ;
1111
1212 setInterval ( ( ) => {
@@ -15,42 +15,42 @@ export class MemoryCache implements CacheProvider {
1515 }
1616
1717 private checkExpiration ( ) {
18- for ( const [ key , entry ] of this . queryResultStore . entries ( ) ) {
18+ for ( const [ key , entry ] of this . entryStore ) {
1919 if ( entryIsExpired ( entry ) ) {
20- this . queryResultStore . delete ( key ) ;
20+ this . entryStore . delete ( key ) ;
2121 }
2222 }
2323
24- for ( const [ tag , queryKeys ] of this . tagStore . entries ( ) ) {
25- for ( const queryKey of queryKeys ) {
26- if ( ! this . queryResultStore . has ( queryKey ) ) {
27- queryKeys . delete ( queryKey ) ;
24+ for ( const [ tag , keys ] of this . tagStore ) {
25+ for ( const key of keys ) {
26+ if ( ! this . entryStore . has ( key ) ) {
27+ keys . delete ( key ) ;
2828 }
2929 }
3030
31- if ( queryKeys . size === 0 ) {
31+ if ( keys . size === 0 ) {
3232 this . tagStore . delete ( tag ) ;
3333 }
3434 }
3535 }
3636
37- getQueryResult ( key : string ) {
38- return Promise . resolve ( this . queryResultStore . get ( key ) ) ;
37+ get ( key : string ) {
38+ return Promise . resolve ( this . entryStore . get ( key ) ) ;
3939 }
4040
41- setQueryResult ( key : string , entry : CacheQueryResultEntry ) {
42- this . queryResultStore . set ( key , entry ) ;
41+ set ( key : string , entry : CacheEntry ) {
42+ this . entryStore . set ( key , entry ) ;
4343
4444 if ( entry . options . tags ) {
4545 for ( const tag of entry . options . tags ) {
46- let queryKeys = this . tagStore . get ( tag ) ;
46+ let keys = this . tagStore . get ( tag ) ;
4747
48- if ( ! queryKeys ) {
49- queryKeys = new Set < string > ( ) ;
50- this . tagStore . set ( tag , queryKeys ) ;
48+ if ( ! keys ) {
49+ keys = new Set < string > ( ) ;
50+ this . tagStore . set ( tag , keys ) ;
5151 }
5252
53- queryKeys . add ( key ) ;
53+ keys . add ( key ) ;
5454 }
5555 }
5656
@@ -60,11 +60,11 @@ export class MemoryCache implements CacheProvider {
6060 invalidate ( options : CacheInvalidationOptions ) {
6161 if ( options . tags ) {
6262 for ( const tag of options . tags ) {
63- const queryKeys = this . tagStore . get ( tag ) ;
63+ const keys = this . tagStore . get ( tag ) ;
6464
65- if ( queryKeys ) {
66- for ( const queryKey of queryKeys ) {
67- this . queryResultStore . delete ( queryKey ) ;
65+ if ( keys ) {
66+ for ( const key of keys ) {
67+ this . entryStore . delete ( key ) ;
6868 }
6969 }
7070 }
@@ -74,7 +74,7 @@ export class MemoryCache implements CacheProvider {
7474 }
7575
7676 invalidateAll ( ) {
77- this . queryResultStore . clear ( ) ;
77+ this . entryStore . clear ( ) ;
7878 this . tagStore . clear ( ) ;
7979 return Promise . resolve ( ) ;
8080 }
0 commit comments