tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
use shared idresolver instance
awarm.space
5 months ago
491f1f8c
44725b46
+1
-76
1 changed file
expand all
collapse all
unified
split
app
reader
getReaderFeed.ts
+1
-76
app/reader/getReaderFeed.ts
···
8
8
import Client from "ioredis";
9
9
import { AtUri } from "@atproto/api";
10
10
import { Json } from "supabase/database.types";
11
11
-
12
12
-
// Create Redis client for DID caching
13
13
-
let redisClient: Client | null = null;
14
14
-
if (process.env.REDIS_URL) {
15
15
-
redisClient = new Client(process.env.REDIS_URL);
16
16
-
}
17
17
-
18
18
-
// Redis-based DID cache implementation
19
19
-
class RedisDidCache implements DidCache {
20
20
-
private staleTTL: number;
21
21
-
private maxTTL: number;
22
22
-
23
23
-
constructor(
24
24
-
private client: Client,
25
25
-
staleTTL = 60 * 60, // 1 hour
26
26
-
maxTTL = 60 * 60 * 24, // 24 hours
27
27
-
) {
28
28
-
this.staleTTL = staleTTL;
29
29
-
this.maxTTL = maxTTL;
30
30
-
}
31
31
-
32
32
-
async cacheDid(did: string, doc: DidDocument): Promise<void> {
33
33
-
const cacheVal = {
34
34
-
doc,
35
35
-
updatedAt: Date.now(),
36
36
-
};
37
37
-
await this.client.setex(
38
38
-
`did:${did}`,
39
39
-
this.maxTTL,
40
40
-
JSON.stringify(cacheVal),
41
41
-
);
42
42
-
}
43
43
-
44
44
-
async checkCache(did: string): Promise<CacheResult | null> {
45
45
-
const cached = await this.client.get(`did:${did}`);
46
46
-
if (!cached) return null;
47
47
-
48
48
-
const { doc, updatedAt } = JSON.parse(cached);
49
49
-
const now = Date.now();
50
50
-
const age = now - updatedAt;
51
51
-
52
52
-
return {
53
53
-
did,
54
54
-
doc,
55
55
-
updatedAt,
56
56
-
stale: age > this.staleTTL * 1000,
57
57
-
expired: age > this.maxTTL * 1000,
58
58
-
};
59
59
-
}
60
60
-
61
61
-
async refreshCache(
62
62
-
did: string,
63
63
-
getDoc: () => Promise<DidDocument | null>,
64
64
-
): Promise<void> {
65
65
-
const doc = await getDoc();
66
66
-
if (doc) {
67
67
-
await this.cacheDid(did, doc);
68
68
-
}
69
69
-
}
70
70
-
71
71
-
async clearEntry(did: string): Promise<void> {
72
72
-
await this.client.del(`did:${did}`);
73
73
-
}
74
74
-
75
75
-
async clear(): Promise<void> {
76
76
-
const keys = await this.client.keys("did:*");
77
77
-
if (keys.length > 0) {
78
78
-
await this.client.del(...keys);
79
79
-
}
80
80
-
}
81
81
-
}
82
82
-
83
83
-
// Create IdResolver with Redis-based DID cache
84
84
-
const idResolver = new IdResolver({
85
85
-
didCache: redisClient ? new RedisDidCache(redisClient) : undefined,
86
86
-
});
11
11
+
import { idResolver } from "./idResolver";
87
12
88
13
export async function getReaderFeed(
89
14
cursor?: string | null,