Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit 10d8267

Browse files
committed
Add onIntervalExpiration and tests.
1 parent efbf09e commit 10d8267

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

packages/plugins/cache/src/providers/memory.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class MemoryCacheProvider implements CacheProvider {
1818
for (const [key, entry] of this.entryStore) {
1919
if (entryIsExpired(entry)) {
2020
this.entryStore.delete(key);
21+
this.options?.onIntervalExpiration?.(entry);
2122
}
2223
}
2324

@@ -87,4 +88,9 @@ export type MemoryCacheOptions = {
8788
* @default 60
8889
*/
8990
checkInterval?: number;
91+
92+
/**
93+
* Called when an entry has expired via the interval check.
94+
*/
95+
onIntervalExpiration?: (entry: CacheEntry) => void,
9096
};

tests/e2e/orm/cache/memory.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,37 @@ describe('Cache plugin (memory)', () => {
868868
});
869869
});
870870

871+
it('supports custom options', async () => {
872+
const onIntervalExpiration = vi.fn(() => {});
873+
const extDb = db.$use(defineCachePlugin({
874+
provider: new MemoryCacheProvider({
875+
checkInterval: 10,
876+
onIntervalExpiration,
877+
}),
878+
}));
879+
880+
await extDb.user.exists({
881+
cache: {
882+
ttl: 5,
883+
},
884+
});
885+
886+
vi.advanceTimersByTime(5100);
887+
expect(onIntervalExpiration).not.toHaveBeenCalled();
888+
vi.advanceTimersByTime(10000);
889+
expect(onIntervalExpiration).toHaveBeenCalledOnce();
890+
891+
// @ts-expect-error
892+
const arg = onIntervalExpiration.mock.lastCall[0];
893+
894+
expect(arg).toMatchObject({
895+
result: false,
896+
options: {
897+
ttl: 5,
898+
},
899+
})
900+
});
901+
871902
it('handles edge cases', async () => {
872903
const extDb = db.$use(defineCachePlugin({
873904
provider: new MemoryCacheProvider(),

0 commit comments

Comments
 (0)