a tool for shared writing and social publishing

use shared idresolver instance

+1 -76
+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 - 12 - // Create Redis client for DID caching 13 - let redisClient: Client | null = null; 14 - if (process.env.REDIS_URL) { 15 - redisClient = new Client(process.env.REDIS_URL); 16 - } 17 - 18 - // Redis-based DID cache implementation 19 - class RedisDidCache implements DidCache { 20 - private staleTTL: number; 21 - private maxTTL: number; 22 - 23 - constructor( 24 - private client: Client, 25 - staleTTL = 60 * 60, // 1 hour 26 - maxTTL = 60 * 60 * 24, // 24 hours 27 - ) { 28 - this.staleTTL = staleTTL; 29 - this.maxTTL = maxTTL; 30 - } 31 - 32 - async cacheDid(did: string, doc: DidDocument): Promise<void> { 33 - const cacheVal = { 34 - doc, 35 - updatedAt: Date.now(), 36 - }; 37 - await this.client.setex( 38 - `did:${did}`, 39 - this.maxTTL, 40 - JSON.stringify(cacheVal), 41 - ); 42 - } 43 - 44 - async checkCache(did: string): Promise<CacheResult | null> { 45 - const cached = await this.client.get(`did:${did}`); 46 - if (!cached) return null; 47 - 48 - const { doc, updatedAt } = JSON.parse(cached); 49 - const now = Date.now(); 50 - const age = now - updatedAt; 51 - 52 - return { 53 - did, 54 - doc, 55 - updatedAt, 56 - stale: age > this.staleTTL * 1000, 57 - expired: age > this.maxTTL * 1000, 58 - }; 59 - } 60 - 61 - async refreshCache( 62 - did: string, 63 - getDoc: () => Promise<DidDocument | null>, 64 - ): Promise<void> { 65 - const doc = await getDoc(); 66 - if (doc) { 67 - await this.cacheDid(did, doc); 68 - } 69 - } 70 - 71 - async clearEntry(did: string): Promise<void> { 72 - await this.client.del(`did:${did}`); 73 - } 74 - 75 - async clear(): Promise<void> { 76 - const keys = await this.client.keys("did:*"); 77 - if (keys.length > 0) { 78 - await this.client.del(...keys); 79 - } 80 - } 81 - } 82 - 83 - // Create IdResolver with Redis-based DID cache 84 - const idResolver = new IdResolver({ 85 - didCache: redisClient ? new RedisDidCache(redisClient) : undefined, 86 - }); 11 + import { idResolver } from "./idResolver"; 87 12 88 13 export async function getReaderFeed( 89 14 cursor?: string | null,