Skip to content

Commit

Permalink
remove dependency on memory queue from kv cache again
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx committed Feb 10, 2025
1 parent d33af3c commit 95bb5be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
3 changes: 0 additions & 3 deletions packages/cloudflare/src/api/kv-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs
import { IgnorableError, RecoverableError } from "@opennextjs/aws/utils/error.js";

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

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

Expand Down Expand Up @@ -117,8 +116,6 @@ class Cache implements IncrementalCache {
);
} catch {
throw new RecoverableError(`Failed to set cache [${key}]`);
} finally {
memoryQueue.remove(key);
}
}

Expand Down
28 changes: 13 additions & 15 deletions packages/cloudflare/src/api/memory-queue.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,26 @@ const defaultOpts = {
describe("MemoryQueue", () => {
beforeAll(() => {
vi.useFakeTimers();
globalThis.internalFetch = vi.fn();
globalThis.internalFetch = vi.fn().mockReturnValue(new Promise((res) => setTimeout(() => res(true), 1)));
});

it("should de-dupe revalidations", async () => {
await cache.send(defaultOpts);
const firstBatch = [cache.send(defaultOpts), cache.send(defaultOpts)];
vi.advanceTimersByTime(1);
await Promise.all(firstBatch);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1);
await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(1);

cache.remove("/test");

await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);
await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);

const secondBatch = [cache.send(defaultOpts)];
vi.advanceTimersByTime(10_000);
await Promise.all(secondBatch);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(2);

await cache.send(defaultOpts);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(3);

await cache.send({ ...defaultOpts, MessageGroupId: generateMessageGroupId("/other") });
const thirdBatch = [
cache.send(defaultOpts),
cache.send({ ...defaultOpts, MessageGroupId: generateMessageGroupId("/other") }),
];
vi.advanceTimersByTime(1);
await Promise.all(thirdBatch);
expect(globalThis.internalFetch).toHaveBeenCalledTimes(4);
});
});
16 changes: 3 additions & 13 deletions packages/cloudflare/src/api/memory-queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { generateMessageGroupId } from "@opennextjs/aws/core/routing/queue.js";
import logger from "@opennextjs/aws/logger.js";
import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides.js";

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

try {
Expand All @@ -36,20 +35,11 @@ class MemoryQueue implements Queue {
});
} catch (e) {
logger.error(e);
} finally {
clearTimeout(this.revalidatedPaths.get(MessageGroupId));
this.revalidatedPaths.delete(MessageGroupId);
}
}

private removeId(id: string) {
clearTimeout(this.revalidatedPaths.get(id));
this.revalidatedPaths.delete(id);
}

public remove(path: string) {
if (this.revalidatedPaths.size > 0) {
this.removeId(generateMessageGroupId(path));
}
}
}

export default new MemoryQueue();

0 comments on commit 95bb5be

Please sign in to comment.