Skip to content

Commit 7a44ed1

Browse files
authored
fix: pass createKey option through wrap methods (#1269)
The createKey option was not being passed from wrap() methods to the underlying wrapSync/wrap functions, preventing custom cache key generation as documented. Fixed in: - @cacheable/cacheable: CacheableMemory.wrap() and Cacheable.wrap() - @cacheable/memory: CacheableMemory.wrap() Added tests to verify the fix works correctly.
1 parent 7cea5d7 commit 7a44ed1

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

packages/cacheable/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ export class Cacheable extends Hookified {
754754
const wrapOptions = {
755755
ttl: options?.ttl ?? this._ttl,
756756
keyPrefix: options?.keyPrefix,
757+
createKey: options?.createKey,
757758
cache: this,
758759
cacheId: this._cacheId,
759760
};

packages/cacheable/src/memory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ export class CacheableMemory extends Hookified {
684684
const wrapOptions = {
685685
ttl: options?.ttl ?? this._ttl,
686686
keyPrefix: options?.keyPrefix,
687+
createKey: options?.createKey,
687688
cache: this,
688689
};
689690

packages/cacheable/test/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,22 @@ describe("cacheable wrap", async () => {
708708
const result3 = await wrapped(1);
709709
expect(result3).not.toBe(result2);
710710
});
711+
712+
test("Cacheable.wrap() passes createKey option through", async () => {
713+
const cacheable = new Cacheable();
714+
let createKeyCalled = false;
715+
const asyncFunction = async (argument: string) => `Result for ${argument}`;
716+
const options = {
717+
createKey: () => {
718+
createKeyCalled = true;
719+
return "testKey";
720+
},
721+
};
722+
723+
const wrapped = cacheable.wrap(asyncFunction, options);
724+
await wrapped("arg1");
725+
expect(createKeyCalled).toBe(true);
726+
});
711727
});
712728

713729
describe("cacheable namespace", async () => {

packages/cacheable/test/wrap.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,4 +378,20 @@ describe("wrap functions handling thrown errors", () => {
378378

379379
expect(result1).toBe(result2);
380380
});
381+
382+
it("CacheableMemory.wrap() passes createKey option through", () => {
383+
const cache = new CacheableMemory();
384+
let createKeyCalled = false;
385+
const testFunction = (argument: string) => `Result for ${argument}`;
386+
const options = {
387+
createKey: () => {
388+
createKeyCalled = true;
389+
return "testKey";
390+
},
391+
};
392+
393+
const wrapped = cache.wrap(testFunction, options);
394+
wrapped("arg1");
395+
expect(createKeyCalled).toBe(true);
396+
});
381397
});

packages/memory/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ export class CacheableMemory extends Hookified {
678678
const wrapOptions = {
679679
ttl: options?.ttl ?? this._ttl,
680680
keyPrefix: options?.keyPrefix,
681+
createKey: options?.createKey,
681682
cache: this as CacheSyncInstance,
682683
};
683684

packages/memory/test/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,22 @@ describe("cacheable wrap", async () => {
702702
expect(result).toBe(result2); // Cached
703703
});
704704

705+
test("CacheableMemory.wrap() passes createKey option through", () => {
706+
const cacheable = new CacheableMemory();
707+
let createKeyCalled = false;
708+
const testFunction = (argument: string) => `Result for ${argument}`;
709+
const options = {
710+
createKey: () => {
711+
createKeyCalled = true;
712+
return "testKey";
713+
},
714+
};
715+
716+
const wrapped = cacheable.wrap(testFunction, options);
717+
wrapped("arg1");
718+
expect(createKeyCalled).toBe(true);
719+
});
720+
705721
test("should be able to pass in expiration time", async () => {
706722
const cacheable = new CacheableMemory();
707723
const expire = Date.now() + 100;

0 commit comments

Comments
 (0)