@@ -8,9 +8,17 @@ import {
8
8
} from 'cache-manager' ;
9
9
import {
10
10
type CachedFunctionInitializerOptions , type CachedFunctionOptions , type CacheableFunction , type ArgumentPaths ,
11
+ Logger ,
11
12
} from './index.d' ;
12
13
13
14
let 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
+ } ;
14
22
15
23
/**
16
24
* Retrieves or initializes the cache.
@@ -29,7 +37,13 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
29
37
throw new Error ( 'Store is not provided in options but is required to initialize the cache' ) ;
30
38
}
31
39
40
+ if ( options ?. logger ) {
41
+ logger = options . logger as Logger ;
42
+ }
43
+
44
+ logger ?. trace ( { options} , 'Initializing cache' ) ;
32
45
cache ||= await ( 'config' in options ! ? caching ( options . store , options . config ) : caching ( options ! . store ) ) ;
46
+ logger ?. trace ( 'Cache initialized' ) ;
33
47
34
48
return cache as Cache < S > ;
35
49
}
@@ -39,6 +53,7 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
39
53
*/
40
54
export function resetCache ( ) {
41
55
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.' ) ;
42
57
}
43
58
44
59
/**
@@ -54,6 +69,7 @@ export function resetCache() {
54
69
export function selectorToCacheKey < F extends CacheableFunction > ( arguments_ : Parameters < F > , selector : ArgumentPaths < F > ) {
55
70
const selectors = _ . castArray ( selector ) ;
56
71
if ( selectors . length === 0 ) {
72
+ logger ?. trace ( arguments_ , 'No selectors provided, using the entire arguments object as the cache key' ) ;
57
73
return JSON . stringify ( arguments_ ) ;
58
74
}
59
75
@@ -92,13 +108,18 @@ export function cachedFunction<F extends CacheableFunction>(function_: F, option
92
108
const cacheKey = selectorToCacheKey ( arguments_ , cacheOptions . selector ! ) ;
93
109
const cache = await getOrInitializeCache ( options as CachedFunctionInitializerOptions ) ;
94
110
111
+ logger ?. trace ( { cacheOptions, cacheKey} , 'Checking cache' ) ;
95
112
const cacheValue = await cache . get < ReturnType < F > > ( cacheKey ) ;
96
113
if ( ! cacheOptions . force && cacheValue !== undefined ) {
114
+ logger ?. trace ( { cacheOptions, cacheKey} , 'Cache hit' ) ;
97
115
return cacheValue ;
98
116
}
117
+ logger ?. trace ( { cacheOptions, cacheKey} , 'Cache miss' ) ;
99
118
100
119
const result = await function_ ( ...arguments_ ) as ReturnType < F > ;
120
+ logger ?. trace ( { cacheOptions, cacheKey} , 'Setting cache' ) ;
101
121
await cache . set ( cacheKey , result , cacheOptions . ttl ) ;
122
+ logger ?. trace ( { cacheOptions, cacheKey} , 'Cache set' ) ;
102
123
103
124
return result ;
104
125
} ;
@@ -143,10 +164,13 @@ export function CacheOptions<F extends CacheableFunction>(
143
164
descriptor : TypedPropertyDescriptor < F > ,
144
165
) : any => {
145
166
if ( ! descriptor . value ) {
167
+ logger ?. warn ( 'CacheOptions decorator is only supported on methods' ) ;
146
168
return ;
147
169
}
148
170
149
171
descriptor . value . cacheOptions = options ;
172
+ logger ?. trace ( { options} , 'Cache options set' ) ;
173
+
150
174
return descriptor ;
151
175
} ;
152
176
}
0 commit comments