@@ -8,9 +8,17 @@ import {
88} from 'cache-manager' ;
99import {
1010 type CachedFunctionInitializerOptions , type CachedFunctionOptions , type CacheableFunction , type ArgumentPaths ,
11+ Logger ,
1112} from './index.d' ;
1213
1314let cache : Cache | undefined ;
15+ let logger : Logger = {
16+ info ( ...args : any ) { } ,
17+ debug ( ...args : any ) { } ,
18+ trace ( ...args : any ) { } ,
19+ warn ( ...args : any ) { } ,
20+ error ( ...args : any ) { } ,
21+ } ;
1422
1523/**
1624 * Retrieves or initializes the cache.
@@ -29,7 +37,13 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
2937 throw new Error ( 'Store is not provided in options but is required to initialize the cache' ) ;
3038 }
3139
40+ if ( options ?. logger ) {
41+ logger = options . logger as Logger ;
42+ }
43+
44+ logger ?. trace ( { options} , 'Initializing cache' ) ;
3245 cache ||= await ( 'config' in options ! ? caching ( options . store , options . config ) : caching ( options ! . store ) ) ;
46+ logger ?. trace ( 'Cache initialized' ) ;
3347
3448 return cache as Cache < S > ;
3549}
@@ -39,6 +53,7 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
3953 */
4054export function resetCache ( ) {
4155 cache = undefined ;
56+ logger ?. warn ( 'You have called resetCache, which is deprecated and basically does nothing. To close any open connections, please retrieve the cache object from getOrInitializeCache and close it directly.' ) ;
4257}
4358
4459/**
@@ -54,6 +69,7 @@ export function resetCache() {
5469export function selectorToCacheKey < F extends CacheableFunction > ( arguments_ : Parameters < F > , selector : ArgumentPaths < F > ) {
5570 const selectors = _ . castArray ( selector ) ;
5671 if ( selectors . length === 0 ) {
72+ logger ?. trace ( arguments_ , 'No selectors provided, using the entire arguments object as the cache key' ) ;
5773 return JSON . stringify ( arguments_ ) ;
5874 }
5975
@@ -92,13 +108,18 @@ export function cachedFunction<F extends CacheableFunction>(function_: F, option
92108 const cacheKey = selectorToCacheKey ( arguments_ , cacheOptions . selector ! ) ;
93109 const cache = await getOrInitializeCache ( options as CachedFunctionInitializerOptions ) ;
94110
111+ logger ?. trace ( { cacheOptions, cacheKey} , 'Checking cache' ) ;
95112 const cacheValue = await cache . get < ReturnType < F > > ( cacheKey ) ;
96113 if ( ! cacheOptions . force && cacheValue !== undefined ) {
114+ logger ?. trace ( { cacheOptions, cacheKey} , 'Cache hit' ) ;
97115 return cacheValue ;
98116 }
117+ logger ?. trace ( { cacheOptions, cacheKey} , 'Cache miss' ) ;
99118
100119 const result = await function_ ( ...arguments_ ) as ReturnType < F > ;
120+ logger ?. trace ( { cacheOptions, cacheKey} , 'Setting cache' ) ;
101121 await cache . set ( cacheKey , result , cacheOptions . ttl ) ;
122+ logger ?. trace ( { cacheOptions, cacheKey} , 'Cache set' ) ;
102123
103124 return result ;
104125 } ;
@@ -143,10 +164,13 @@ export function CacheOptions<F extends CacheableFunction>(
143164 descriptor : TypedPropertyDescriptor < F > ,
144165 ) : any => {
145166 if ( ! descriptor . value ) {
167+ logger ?. warn ( 'CacheOptions decorator is only supported on methods' ) ;
146168 return ;
147169 }
148170
149171 descriptor . value . cacheOptions = options ;
172+ logger ?. trace ( { options} , 'Cache options set' ) ;
173+
150174 return descriptor ;
151175 } ;
152176}
0 commit comments