Skip to content

Commit

Permalink
feat: allowing for customKeyFunction in higher-order function
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadienvan committed Sep 26, 2024
1 parent f1c5928 commit 5aea03e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ The options available are:
- `executionTime`: The execution time of the current function execution in milliseconds.
- `args`: The arguments passed to the current function.
- `timeFrameCacheRecords`: The cache records of the last `timeFrame` milliseconds.
- `customKeyFunction` (_optional_): A function to generate a custom key for every cache record.
The function receives the arguments passed to the current function and should return a string.
If not passed, the key will be generated based on an internal algorithm that considers the arguments passed to the function.
- `millisecondThreshold` (_optional_): The threshold in milliseconds to be considered for the condition checks. If not passed, this criteria will be ignored.
- `requestsThreshold` (_optional_): The number of requests to be considered for the condition checks. Default: `1`.
- `expirationMode` (_optional_): The expiration mode to use. Default: `default`.
Expand Down
16 changes: 16 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,22 @@ describe('Higher-Order Function', () => {
await sleep(TTL + EXECUTION_MARGIN);
expect(eventHits.get('onCacheHit')).toBe(1);
});

it('should allow for custom getDataCacheKey functions', async () => {
const mockFn = jest.fn();
const wrappedMockFn = cacheCandidate(mockFn, {
customKeyFunction: (args) => {
return 'custom-key';
},
events: {
onCacheSet: ({key}) => {
expect(key).toBe('custom-key');
}
}
});
wrappedMockFn(1);
await sleep(TTL + EXECUTION_MARGIN);
});
});

describe('Library-wide Conditions', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function cacheCandidate<T extends (...args: any[]) => Promise<any>>(
return function cacheCandidateWrapper(...args: Parameters<T>): ReturnType<T> {
return letsCandidate({
options,
key: getDataCacheKey([uniqueIdentifier, JSON.stringify(args)]),
key: _options.customKeyFunction ? _options.customKeyFunction(args) : getDataCacheKey([uniqueIdentifier, JSON.stringify(args)]),
timeoutCache,
runningQueryCache,
timeframeCache,
Expand Down
1 change: 1 addition & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CacheCandidateInputOptions {
ttl?: number;
timeFrame?: number;
candidateFunction?: (CandidateFunctionOptions) => boolean;
customKeyFunction?: (args: any) => string;
millisecondThreshold?: number;
requestsThreshold?: number;
expirationMode?: 'default' | 'timeout-only' | 'eject';
Expand Down

0 comments on commit 5aea03e

Please sign in to comment.