Skip to content

Commit 8d21047

Browse files
committed
feat: added noCache to options
1 parent cca9f3c commit 8d21047

File tree

6 files changed

+2159
-77
lines changed

6 files changed

+2159
-77
lines changed

bun.lockb

413 KB
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"xo": "^0.59.3"
1818
},
1919
"peerDependencies": {
20-
"typescript": "^5.5.4"
20+
"typescript": "^5.6.2"
2121
},
2222
"scripts": {
2323
"build": "tsc",
@@ -26,7 +26,7 @@
2626
},
2727
"dependencies": {
2828
"cache-manager": "^5.7.6",
29-
"eslint-plugin-th-rules": "^1.13.4",
29+
"eslint-plugin-th-rules": "^1.14.0",
3030
"lodash": "^4.17.21",
3131
"reflect-metadata": "^0.2.2"
3232
},

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export type CachedFunctionInitializerOptions = {logger?: Partial<Logger>;} &
6565
* @property {boolean} [force] - Whether to force the execution of the cached function.
6666
*/
6767
export type CachedFunctionOptions<F extends CacheableFunction> = Partial<CachedFunctionInitializerOptions> & {
68+
/** The selector for the cached function. */
6869
selector?: ArgumentPaths<F>;
70+
/** The time-to-live (TTL) for the cached function. */
6971
ttl?: number;
72+
/** Whether to force update the cache. */
7073
force?: boolean;
74+
/** Don't use the cache at all - when `true`, calls to functions wrapped by `cachedFunction` will essentially just call the original function. */
75+
noCache?: boolean;
7176
};

src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function getOrInitializeCache<S extends Store>(options?: CachedFunc
4141
logger = options.logger as Logger;
4242
}
4343

44-
logger?.trace({options}, 'Initializing cache');
44+
logger?.trace('Initializing cache');
4545
cache ||= await ('config' in options! ? caching(options.store, options.config) : caching(options!.store));
4646
logger?.trace('Cache initialized');
4747

@@ -105,21 +105,26 @@ export function cachedFunction<F extends CacheableFunction>(function_: F, option
105105
throw new Error('No cache options provided, either use the @CacheOptions decorator or provide options to cachedFunction directly.');
106106
}
107107

108+
if (!cacheOptions.noCache) {
109+
logger.trace('Cache is disabled, calling the original function directly');
110+
return function_(...arguments_) as ReturnType<F>;
111+
}
112+
108113
const cacheKey = selectorToCacheKey(arguments_, cacheOptions.selector!);
109114
const cache = await getOrInitializeCache(options as CachedFunctionInitializerOptions);
110115

111-
logger?.trace({cacheOptions, cacheKey}, 'Checking cache');
116+
logger?.trace({cacheKey}, 'Checking cache');
112117
const cacheValue = await cache.get<ReturnType<F>>(cacheKey);
113118
if (!cacheOptions.force && cacheValue !== undefined) {
114-
logger?.trace({cacheOptions, cacheKey}, 'Cache hit');
119+
logger?.trace({cacheKey}, 'Cache hit');
115120
return cacheValue;
116121
}
117-
logger?.trace({cacheOptions, cacheKey}, 'Cache miss');
122+
logger?.trace({cacheKey}, 'Cache miss');
118123

119124
const result = await function_(...arguments_) as ReturnType<F>;
120-
logger?.trace({cacheOptions, cacheKey}, 'Setting cache');
125+
logger?.trace({cacheKey}, 'Setting cache');
121126
await cache.set(cacheKey, result, cacheOptions.ttl);
122-
logger?.trace({cacheOptions, cacheKey}, 'Cache set');
127+
logger?.trace({cacheKey}, 'Cache set');
123128

124129
return result;
125130
};
@@ -169,8 +174,6 @@ export function CacheOptions<F extends CacheableFunction>(
169174
}
170175

171176
descriptor.value.cacheOptions = options;
172-
logger?.trace({options}, 'Cache options set');
173-
174177
return descriptor;
175178
};
176179
}

tests/redis.test_.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {random} from 'lodash';
33
import {type RedisStore, redisStore} from 'cache-manager-ioredis-yet';
44
import {cachedFunction, CacheOptions, getOrInitializeCache} from '../src';
5+
import { selectorToCacheKey } from '../src/index';
56

67
const cache = await getOrInitializeCache<RedisStore>({
78
store: await redisStore({
@@ -22,14 +23,14 @@ type Person = {
2223
};
2324

2425
class CachedPersonCreator {
25-
@CacheOptions('0.name', 10_000)
26+
@CacheOptions({ttl: 10_000})
2627
static async createPerson(person: Person) {
2728
console.log('Person created!!!!!');
2829
return person;
2930
}
3031
}
3132

32-
const cachedCreatePerson = cachedFunction(CachedPersonCreator.createPerson);
33+
const cachedCreatePerson = cachedFunction(CachedPersonCreator.createPerson, {ttl: -2345, selector: ['0.name']});
3334
const person = await cachedCreatePerson({
3435
id: random(0, 100_000).toString(),
3536
name: 'Tomer Horowitz',

0 commit comments

Comments
 (0)