Skip to content

Commit 1bee13d

Browse files
authored
utils - fix: making Keyv a dependency (#1462)
* utils - fix: making Keyv a dependency * Update wrap.test.ts * Update wrap.test.ts * Update wrap.test.ts * Update wrap.test.ts * removing error on warning * Update wrap.test.ts * Update package.json * lint with node-cache * Update wrap.test.ts
1 parent 00ff4b5 commit 1bee13d

File tree

5 files changed

+67
-72
lines changed

5 files changed

+67
-72
lines changed

packages/cache-manager/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"keyv": "^5.5.4"
6363
},
6464
"devDependencies": {
65-
"@biomejs/biome": "^2.3.4",
65+
"@biomejs/biome": "^2.3.6",
6666
"@faker-js/faker": "^10.1.0",
6767
"@keyv/redis": "^5.1.3",
6868
"@types/node": "^24.10.0",

packages/cache-manager/test/wrap.test.ts

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -95,53 +95,50 @@ describe("wrap", () => {
9595
it.each([
9696
[500, 250],
9797
[{ ttl: 500, refreshThreshold: 250 }, undefined],
98-
])(
99-
"should allow dynamic refreshThreshold on wrap function with ttl/options param as %s",
100-
async (ttlOrOptions, refreshThreshold) => {
101-
// 1st call should be cached
102-
expect(
103-
await cache.wrap(
104-
data.key,
105-
async () => 0,
106-
ttlOrOptions as never,
107-
refreshThreshold,
108-
),
109-
).toEqual(0);
110-
await sleep(260);
111-
// Background refresh, but stale value returned
112-
expect(
113-
await cache.wrap(
114-
data.key,
115-
async () => 1,
116-
ttlOrOptions as never,
117-
refreshThreshold,
118-
),
119-
).toEqual(0);
120-
// New value in cache
121-
expect(
122-
await cache.wrap(
123-
data.key,
124-
async () => 2,
125-
ttlOrOptions as never,
126-
refreshThreshold,
127-
),
128-
).toEqual(1);
129-
130-
await sleep(260);
131-
// No background refresh with the new override params
132-
expect(await cache.wrap(data.key, async () => 3, undefined, 125)).toEqual(
133-
1,
134-
);
135-
await sleep(140);
136-
// Background refresh, but stale value returned
137-
expect(await cache.wrap(data.key, async () => 4, undefined, 125)).toEqual(
138-
1,
139-
);
140-
expect(await cache.wrap(data.key, async () => 5, undefined, 125)).toEqual(
141-
4,
142-
);
143-
},
144-
);
98+
])("should allow dynamic refreshThreshold on wrap function with ttl/options param as %s", async (ttlOrOptions, refreshThreshold) => {
99+
// 1st call should be cached
100+
expect(
101+
await cache.wrap(
102+
data.key,
103+
async () => 0,
104+
ttlOrOptions as never,
105+
refreshThreshold,
106+
),
107+
).toEqual(0);
108+
await sleep(260);
109+
// Background refresh, but stale value returned
110+
expect(
111+
await cache.wrap(
112+
data.key,
113+
async () => 1,
114+
ttlOrOptions as never,
115+
refreshThreshold,
116+
),
117+
).toEqual(0);
118+
// New value in cache
119+
expect(
120+
await cache.wrap(
121+
data.key,
122+
async () => 2,
123+
ttlOrOptions as never,
124+
refreshThreshold,
125+
),
126+
).toEqual(1);
127+
128+
await sleep(260);
129+
// No background refresh with the new override params
130+
expect(await cache.wrap(data.key, async () => 3, undefined, 125)).toEqual(
131+
1,
132+
);
133+
await sleep(140);
134+
// Background refresh, but stale value returned
135+
expect(await cache.wrap(data.key, async () => 4, undefined, 125)).toEqual(
136+
1,
137+
);
138+
expect(await cache.wrap(data.key, async () => 5, undefined, 125)).toEqual(
139+
4,
140+
);
141+
});
145142

146143
it("should allow refreshThreshold function on wrap function", async () => {
147144
const config = {

packages/node-cache/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"clean": "rimraf ./dist ./coverage ./node_modules"
4343
},
4444
"devDependencies": {
45-
"@biomejs/biome": "^2.3.5",
45+
"@biomejs/biome": "^2.3.6",
4646
"@faker-js/faker": "^10.1.0",
4747
"@types/node": "^24.10.1",
4848
"@vitest/coverage-v8": "^4.0.9",

packages/node-cache/src/index.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,26 @@ export class NodeCache<T> extends Hookified {
430430
return this.intervalId;
431431
}
432432

433+
public startInterval(): void {
434+
if (this.options.checkperiod && this.options.checkperiod > 0) {
435+
const checkPeriodinSeconds = this.options.checkperiod * 1000;
436+
this.intervalId = setInterval(() => {
437+
this.checkData();
438+
}, checkPeriodinSeconds).unref();
439+
440+
return;
441+
}
442+
443+
this.intervalId = 0;
444+
}
445+
446+
public stopInterval(): void {
447+
if (this.intervalId !== 0) {
448+
clearInterval(this.intervalId);
449+
this.intervalId = 0;
450+
}
451+
}
452+
433453
private formatKey(key: string | number): string {
434454
return key.toString();
435455
}
@@ -445,19 +465,6 @@ export class NodeCache<T> extends Hookified {
445465
return expirationTimestamp;
446466
}
447467

448-
private startInterval(): void {
449-
if (this.options.checkperiod && this.options.checkperiod > 0) {
450-
const checkPeriodinSeconds = this.options.checkperiod * 1000;
451-
this.intervalId = setInterval(() => {
452-
this.checkData();
453-
}, checkPeriodinSeconds).unref();
454-
455-
return;
456-
}
457-
458-
this.intervalId = 0;
459-
}
460-
461468
private checkData(): void {
462469
for (const [key, value] of this.store.entries()) {
463470
if (value.ttl > 0 && value.ttl < Date.now()) {
@@ -470,13 +477,6 @@ export class NodeCache<T> extends Hookified {
470477
}
471478
}
472479

473-
private stopInterval(): void {
474-
if (this.intervalId !== 0) {
475-
clearInterval(this.intervalId);
476-
this.intervalId = 0;
477-
}
478-
}
479-
480480
private createError(errorCode: string, key?: string): Error {
481481
let error = errorCode;
482482
/* v8 ignore next -- @preserve */

packages/utils/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@
5252
"LICENSE"
5353
],
5454
"dependencies": {
55-
"hashery": "^1.2.0"
56-
},
57-
"peerDependencies": {
55+
"hashery": "^1.2.0",
5856
"keyv": "^5.5.4"
5957
}
6058
}

0 commit comments

Comments
 (0)