@@ -19,9 +19,65 @@ export class R2WriteBuffer extends DurableObject {
19
19
}
20
20
}
21
21
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 ( ) ;
23
63
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' ;
25
81
26
82
export default {
27
83
async fetch ( ) {
0 commit comments