Skip to content

Commit e392a44

Browse files
author
Nicolas Dorseuil
committed
debug tag cache
1 parent de71ecf commit e392a44

File tree

1 file changed

+58
-2
lines changed
  • packages/gitbook-v2/openNext/customWorkers

1 file changed

+58
-2
lines changed

packages/gitbook-v2/openNext/customWorkers/do.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,65 @@ export class R2WriteBuffer extends DurableObject {
1919
}
2020
}
2121

22-
export { DOQueueHandler } from '../../.open-next/.build/durable-objects/queue.js';
22+
// TODO: replace once i figured out why `revalidateTag` is not working in preview
23+
export class DOShardedTagCache extends DurableObject {
24+
sql;
25+
26+
constructor(state, env) {
27+
super(state, env);
28+
this.sql = state.storage.sql;
29+
state.blockConcurrencyWhile(async () => {
30+
this.sql.exec(
31+
'CREATE TABLE IF NOT EXISTS revalidations (tag TEXT PRIMARY KEY, revalidatedAt INTEGER)'
32+
);
33+
});
34+
}
35+
36+
async getLastRevalidated(tags) {
37+
try {
38+
const result = this.sql
39+
.exec(
40+
`SELECT MAX(revalidatedAt) AS time FROM revalidations WHERE tag IN (${tags.map(() => '?').join(', ')})`,
41+
...tags
42+
)
43+
.toArray();
44+
console.log('getLastRevalidated', tags, result);
45+
if (result.length === 0) return 0;
46+
// We only care about the most recent revalidation
47+
return result[0]?.time ?? 0;
48+
} catch (e) {
49+
console.error(e);
50+
// By default we don't want to crash here, so we return 0
51+
return 0;
52+
}
53+
}
54+
55+
async hasBeenRevalidated(tags, lastModified) {
56+
const result = this.sql
57+
.exec(
58+
`SELECT 1 FROM revalidations WHERE tag IN (${tags.map(() => '?').join(', ')}) AND revalidatedAt > ? LIMIT 1`,
59+
...tags,
60+
lastModified ?? Date.now()
61+
)
62+
.toArray();
2363

24-
export { DOShardedTagCache } from '../../.open-next/.build/durable-objects/sharded-tag-cache.js';
64+
console.log('hasBeenRevalidated', tags, lastModified, result);
65+
return result.length > 0;
66+
}
67+
68+
async writeTags(tags, lastModified) {
69+
console.log('writeTags', tags, lastModified);
70+
tags.forEach((tag) => {
71+
this.sql.exec(
72+
'INSERT OR REPLACE INTO revalidations (tag, revalidatedAt) VALUES (?, ?)',
73+
tag,
74+
lastModified
75+
);
76+
});
77+
}
78+
}
79+
80+
export { DOQueueHandler } from '../../.open-next/.build/durable-objects/queue.js';
2581

2682
export default {
2783
async fetch() {

0 commit comments

Comments
 (0)