Skip to content

Commit 7005134

Browse files
committed
remove dependency on memory queue from kv cache again
1 parent d33af3c commit 7005134

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

packages/cloudflare/src/api/kv-cache.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs
22
import { IgnorableError, RecoverableError } from "@opennextjs/aws/utils/error.js";
33

44
import { getCloudflareContext } from "./cloudflare-context.js";
5-
import memoryQueue from "./memory-queue.js";
65

76
export const CACHE_ASSET_DIR = "cdn-cgi/_next_cache";
87

@@ -117,8 +116,6 @@ class Cache implements IncrementalCache {
117116
);
118117
} catch {
119118
throw new RecoverableError(`Failed to set cache [${key}]`);
120-
} finally {
121-
memoryQueue.remove(key);
122119
}
123120
}
124121

packages/cloudflare/src/api/memory-queue.spec.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@ const defaultOpts = {
1616
describe("MemoryQueue", () => {
1717
beforeAll(() => {
1818
vi.useFakeTimers();
19-
globalThis.internalFetch = vi.fn();
19+
globalThis.internalFetch = vi.fn().mockReturnValue(new Promise((res) => setTimeout(() => res(true), 1)));
2020
});
2121

2222
it("should de-dupe revalidations", async () => {
23-
await cache.send(defaultOpts);
23+
const firstBatch = [cache.send(defaultOpts), cache.send(defaultOpts)];
24+
vi.advanceTimersByTime(1);
25+
await Promise.all(firstBatch);
2426
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1);
25-
await cache.send(defaultOpts);
26-
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1);
27-
28-
cache.remove("/test");
2927

30-
await cache.send(defaultOpts);
31-
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);
32-
await cache.send(defaultOpts);
28+
const secondBatch = [cache.send(defaultOpts)];
29+
vi.advanceTimersByTime(1);
30+
await Promise.all(secondBatch);
3331
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);
3432

3533
vi.advanceTimersByTime(10_000);
3634

37-
await cache.send(defaultOpts);
38-
expect(globalThis.internalFetch).toHaveBeenCalledTimes(3);
39-
40-
await cache.send({ ...defaultOpts, MessageGroupId: generateMessageGroupId("/other") });
35+
const thirdBatch = [
36+
cache.send(defaultOpts),
37+
cache.send({ ...defaultOpts, MessageGroupId: generateMessageGroupId("/other") }),
38+
];
39+
vi.advanceTimersByTime(1);
40+
await Promise.all(thirdBatch);
4141
expect(globalThis.internalFetch).toHaveBeenCalledTimes(4);
4242
});
4343
});

packages/cloudflare/src/api/memory-queue.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js";
21
import logger from "@opennextjs/aws/logger.js";
32
import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides.js";
43

@@ -18,7 +17,7 @@ class MemoryQueue implements Queue {
1817
this.revalidatedPaths.set(
1918
MessageGroupId,
2019
// force remove to allow new revalidations incase something went wrong
21-
setTimeout(() => this.removeId(MessageGroupId), 10_000)
20+
setTimeout(() => this.revalidatedPaths.delete(MessageGroupId), 10_000)
2221
);
2322

2423
try {
@@ -36,20 +35,11 @@ class MemoryQueue implements Queue {
3635
});
3736
} catch (e) {
3837
logger.error(e);
38+
} finally {
39+
clearTimeout(this.revalidatedPaths.get(MessageGroupId));
3940
this.revalidatedPaths.delete(MessageGroupId);
4041
}
4142
}
42-
43-
private removeId(id: string) {
44-
clearTimeout(this.revalidatedPaths.get(id));
45-
this.revalidatedPaths.delete(id);
46-
}
47-
48-
public remove(path: string) {
49-
if (this.revalidatedPaths.size > 0) {
50-
this.removeId(generateMessageGroupId(path));
51-
}
52-
}
5343
}
5444

5545
export default new MemoryQueue();

0 commit comments

Comments
 (0)