Skip to content

Commit 1b62163

Browse files
committed
shared cache debug function
1 parent b8d264e commit 1b62163

File tree

7 files changed

+29
-19
lines changed

7 files changed

+29
-19
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
22
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides.js";
33
import { IgnorableError } from "@opennextjs/aws/utils/error.js";
44

55
import { getCloudflareContext } from "../../cloudflare-context.js";
6-
import { IncrementalCacheEntry } from "./internal.js";
6+
import { debugCache, IncrementalCacheEntry } from "../internal.js";
77

88
export const NAME = "cf-kv-incremental-cache";
99

@@ -24,7 +24,7 @@ class KVIncrementalCache implements IncrementalCache {
2424
const kv = getCloudflareContext().env.NEXT_INC_CACHE_KV;
2525
if (!kv) throw new IgnorableError("No KV Namespace");
2626

27-
debug(`Get ${key}`);
27+
debugCache(`Get ${key}`);
2828

2929
try {
3030
const entry = await kv.get<IncrementalCacheEntry<IsFetch> | CacheValue<IsFetch>>(
@@ -56,7 +56,7 @@ class KVIncrementalCache implements IncrementalCache {
5656
const kv = getCloudflareContext().env.NEXT_INC_CACHE_KV;
5757
if (!kv) throw new IgnorableError("No KV Namespace");
5858

59-
debug(`Set ${key}`);
59+
debugCache(`Set ${key}`);
6060

6161
try {
6262
await kv.put(
@@ -79,7 +79,7 @@ class KVIncrementalCache implements IncrementalCache {
7979
const kv = getCloudflareContext().env.NEXT_INC_CACHE_KV;
8080
if (!kv) throw new IgnorableError("No KV Namespace");
8181

82-
debug(`Delete ${key}`);
82+
debugCache(`Delete ${key}`);
8383

8484
try {
8585
await kv.delete(this.getKVKey(key, /* isFetch= */ false));

packages/cloudflare/src/api/overrides/incremental-cache/r2-incremental-cache.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
22
import type { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides.js";
33
import { IgnorableError } from "@opennextjs/aws/utils/error.js";
44

55
import { getCloudflareContext } from "../../cloudflare-context.js";
6+
import { debugCache } from "../internal.js";
67

78
export const NAME = "cf-r2-incremental-cache";
89

@@ -23,7 +24,7 @@ class R2IncrementalCache implements IncrementalCache {
2324
const r2 = getCloudflareContext().env.NEXT_INC_CACHE_R2_BUCKET;
2425
if (!r2) throw new IgnorableError("No R2 bucket");
2526

26-
debug(`Get ${key}`);
27+
debugCache(`Get ${key}`);
2728

2829
try {
2930
const r2Object = await r2.get(this.getR2Key(key, isFetch));
@@ -47,7 +48,7 @@ class R2IncrementalCache implements IncrementalCache {
4748
const r2 = getCloudflareContext().env.NEXT_INC_CACHE_R2_BUCKET;
4849
if (!r2) throw new IgnorableError("No R2 bucket");
4950

50-
debug(`Set ${key}`);
51+
debugCache(`Set ${key}`);
5152

5253
try {
5354
await r2.put(this.getR2Key(key, isFetch), JSON.stringify(value));
@@ -60,7 +61,7 @@ class R2IncrementalCache implements IncrementalCache {
6061
const r2 = getCloudflareContext().env.NEXT_INC_CACHE_R2_BUCKET;
6162
if (!r2) throw new IgnorableError("No R2 bucket");
6263

63-
debug(`Delete ${key}`);
64+
debugCache(`Delete ${key}`);
6465

6566
try {
6667
await r2.delete(this.getR2Key(key));

packages/cloudflare/src/api/overrides/incremental-cache/regional-cache.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
22
import { CacheValue, IncrementalCache, WithLastModified } from "@opennextjs/aws/types/overrides.js";
33

44
import { getCloudflareContext } from "../../cloudflare-context.js";
5-
import { IncrementalCacheEntry } from "./internal.js";
5+
import { debugCache, IncrementalCacheEntry } from "../internal.js";
66
import { NAME as KV_CACHE_NAME } from "./kv-incremental-cache.js";
77

88
const ONE_MINUTE_IN_SECONDS = 60;
@@ -57,7 +57,7 @@ class RegionalCache implements IncrementalCache {
5757
// Check for a cached entry as this will be faster than the store response.
5858
const cachedResponse = await cache.match(localCacheKey);
5959
if (cachedResponse) {
60-
debug("Get - cached response");
60+
debugCache("Get - cached response");
6161

6262
// Re-fetch from the store and update the regional cache in the background
6363
if (this.opts.shouldLazilyUpdateOnCacheHit) {

packages/cloudflare/src/api/overrides/incremental-cache/internal.ts renamed to packages/cloudflare/src/api/overrides/internal.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ export type IncrementalCacheEntry<IsFetch extends boolean> = {
44
value: CacheValue<IsFetch>;
55
lastModified: number;
66
};
7+
8+
export const debugCache = (name: string, ...args: unknown[]) => {
9+
if (process.env.NEXT_PRIVATE_DEBUG_CACHE) {
10+
console.log(`[${name}] `, ...args);
11+
}
12+
};

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
22
import type { Queue, QueueMessage } from "@opennextjs/aws/types/overrides.js";
33
import { IgnorableError } from "@opennextjs/aws/utils/error.js";
44

55
import { getCloudflareContext } from "../../cloudflare-context";
6+
import { debugCache } from "../internal";
67

78
export const DEFAULT_REVALIDATION_TIMEOUT_MS = 10_000;
89

@@ -48,7 +49,7 @@ export class MemoryQueue implements Queue {
4849
if (response.status !== 200 || response.headers.get("x-nextjs-cache") !== "REVALIDATED") {
4950
error(`Revalidation failed for ${url} with status ${response.status}`);
5051
}
51-
debug(`Revalidation successful for ${url}`);
52+
debugCache(`Revalidation successful for ${url}`);
5253
} catch (e) {
5354
error(e);
5455
} finally {

packages/cloudflare/src/api/overrides/tag-cache/d1-next-tag-cache.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
22
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
33
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
44
import { RecoverableError } from "@opennextjs/aws/utils/error.js";
55

66
import { getCloudflareContext } from "../../cloudflare-context.js";
7+
import { debugCache } from "../internal.js";
78

89
export const NAME = "d1-next-mode-tag-cache";
910

@@ -48,7 +49,7 @@ export class D1NextModeTagCache implements NextModeTagCache {
4849
const cfEnv = getCloudflareContext().env;
4950
const db = cfEnv.NEXT_TAG_CACHE_D1;
5051

51-
if (!db) debug("No D1 database found");
52+
if (!db) debugCache("No D1 database found");
5253

5354
const isDisabled = !!(globalThis as unknown as { openNextConfig: OpenNextConfig }).openNextConfig
5455
.dangerous?.disableTagCache;

packages/cloudflare/src/api/overrides/tag-cache/do-sharded-tag-cache.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { debug, error } from "@opennextjs/aws/adapters/logger.js";
1+
import { error } from "@opennextjs/aws/adapters/logger.js";
22
import { generateShardId } from "@opennextjs/aws/core/routing/queue.js";
33
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next";
44
import type { NextModeTagCache } from "@opennextjs/aws/types/overrides.js";
55
import { IgnorableError } from "@opennextjs/aws/utils/error.js";
66

77
import { getCloudflareContext } from "../../cloudflare-context";
8+
import { debugCache } from "../internal";
89

910
export const DEFAULT_WRITE_RETRIES = 3;
1011
export const DEFAULT_NUM_SHARDS = 4;
@@ -196,7 +197,7 @@ class ShardedDOTagCache implements NextModeTagCache {
196197
const cfEnv = getCloudflareContext().env;
197198
const db = cfEnv.NEXT_TAG_CACHE_DO_SHARDED;
198199

199-
if (!db) debug("No Durable object found");
200+
if (!db) debugCache("No Durable object found");
200201
const isDisabled = !!(globalThis as unknown as { openNextConfig: OpenNextConfig }).openNextConfig
201202
.dangerous?.disableTagCache;
202203

@@ -334,7 +335,7 @@ class ShardedDOTagCache implements NextModeTagCache {
334335
const key = await this.getCacheKey(doId, tags);
335336
await cache.delete(key);
336337
} catch (e) {
337-
debug("Error while deleting from regional cache", e);
338+
debugCache("Error while deleting from regional cache", e);
338339
}
339340
}
340341
}

0 commit comments

Comments
 (0)