Skip to content

Commit eefd68a

Browse files
committed
feat: added trace logging
1 parent e04eee5 commit eefd68a

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/index.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import {
1212
*/
1313
export type SingleOrArray<T> = T | T[];
1414

15+
/**
16+
* Represents a logger object.
17+
*/
18+
export type Logger = {
19+
trace: (...args: any) => any;
20+
debug: (...args: any) => any;
21+
info: (...args: any) => any;
22+
warn: (...args: any) => any;
23+
error: (...args: any) => any;
24+
}
25+
1526
/**
1627
* Represents any function that takes any number of arguments and returns any value.
1728
*/
@@ -41,9 +52,8 @@ export type ArgumentPaths<F extends CacheableFunction> = SingleOrArray<Paths<Par
4152
/**
4253
* Represents the options for initializing a cached function.
4354
*/
44-
export type CachedFunctionInitializerOptions =
45-
{store: FactoryStore; config: FactoryConfig} |
46-
{store: Store};
55+
export type CachedFunctionInitializerOptions = {logger?: Partial<Logger>;} &
56+
({store: FactoryStore; config: FactoryConfig} | {store: Store});
4757

4858
/**
4959
* Options for a cached function.

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ import {
88
} from 'cache-manager';
99
import {
1010
type CachedFunctionInitializerOptions, type CachedFunctionOptions, type CacheableFunction, type ArgumentPaths,
11+
Logger,
1112
} from './index.d';
1213

1314
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+
};
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
*/
4054
export 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() {
5469
export 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
}

tests/redis.test_.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const cache = await getOrInitializeCache<RedisStore>({
88
host: 'localhost',
99
port: 6379,
1010
}),
11+
logger: console
1112
});
1213

1314
type Person = {

0 commit comments

Comments
 (0)