Skip to content

Commit 910255a

Browse files
committed
fix: use LRU eviction for cron schedule cache
1 parent 2ded7dc commit 910255a

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/cron/schedule.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
clearCronScheduleCacheForTest,
55
computeNextRunAtMs,
66
computePreviousRunAtMs,
7+
getCronScheduleCacheMaxForTest,
78
getCronScheduleCacheSizeForTest,
89
hasCronInCacheForTest,
910
} from "./schedule.js";
@@ -147,16 +148,17 @@ describe("cron schedule", () => {
147148

148149
it("promotes accessed entries to avoid premature LRU eviction", () => {
149150
const nowMs = Date.parse("2026-03-01T00:00:00.000Z");
151+
const cacheMax = getCronScheduleCacheMaxForTest();
150152

151153
// Fill cache to capacity with unique expressions.
152154
// i=0 → "0 0 * * *", i=1 → "1 0 * * *", ..., i=511 → "31 8 * * *"
153-
for (let i = 0; i < 512; i++) {
155+
for (let i = 0; i < cacheMax; i++) {
154156
computeNextRunAtMs(
155157
{ kind: "cron", expr: `${i % 60} ${Math.floor(i / 60)} * * *`, tz: "UTC" },
156158
nowMs,
157159
);
158160
}
159-
expect(getCronScheduleCacheSizeForTest()).toBe(512);
161+
expect(getCronScheduleCacheSizeForTest()).toBe(cacheMax);
160162

161163
// Entry #0 ("0 0 * * *") is the oldest by insertion order.
162164
// Access it so LRU promotes it (delete + re-insert at end of Map).
@@ -165,7 +167,7 @@ describe("cron schedule", () => {
165167
// Entry #1 ("1 0 * * *") is now the least-recently-used.
166168
// Insert a new entry to trigger one eviction.
167169
computeNextRunAtMs({ kind: "cron", expr: "0 0 1 1 *", tz: "UTC" }, nowMs);
168-
expect(getCronScheduleCacheSizeForTest()).toBe(512);
170+
expect(getCronScheduleCacheSizeForTest()).toBe(cacheMax);
169171

170172
// Under LRU: entry #0 survived (was promoted), entry #1 was evicted.
171173
// Under FIFO: entry #0 would be evicted instead — this assertion would fail.

src/cron/schedule.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ export function getCronScheduleCacheSizeForTest(): number {
173173
return cronEvalCache.size;
174174
}
175175

176+
export function getCronScheduleCacheMaxForTest(): number {
177+
return CRON_EVAL_CACHE_MAX;
178+
}
179+
176180
export function hasCronInCacheForTest(expr: string, tz: string): boolean {
177181
return cronEvalCache.has(`${tz}\u0000${expr}`);
178182
}

0 commit comments

Comments
 (0)