@@ -33,64 +33,6 @@ async function getCount(): Promise<number> {
3333 return count ;
3434}
3535
36- type Cache = { value : number | null ; updated : number } ;
37- let cached : Cache = { value : null , updated : 0 } ;
38-
39- // Default cache duration 30 seconds; can be overridden with env var (ms)
40- const CACHE_DURATION_MS = Number ( process . env . RSVP_CACHE_DURATION_MS ) || 30_000 ;
41-
42- // Extend global to hold a single timer across module reloads (avoid duplicate timers in dev)
43- declare global {
44-
45- var __RSVP_CACHE_TIMER__ : NodeJS . Timeout | undefined ;
46- }
47-
48- async function updateCache ( ) : Promise < void > {
49- try {
50- const count = await getCount ( ) ;
51- cached = { value : count , updated : Date . now ( ) } ;
52- console . info ( "[rsvp] cache updated:" , cached . value , new Date ( cached . updated ) . toISOString ( ) ) ;
53- } catch ( err : unknown ) {
54- console . error ( "[rsvp] updateCache failed:" , err ) ;
55- // keep previous cached value if any
56- }
57- }
58-
59- // Start background updater once per server instance
60- if ( ! global . __RSVP_CACHE_TIMER__ ) {
61- // initial immediate update (fire-and-forget)
62- updateCache ( ) . catch ( ( e ) => console . error ( "[rsvp] initial update failed:" , e ) ) ;
63-
64- // schedule periodic updates
65- global . __RSVP_CACHE_TIMER__ = setInterval ( ( ) => {
66- updateCache ( ) . catch ( ( e ) => console . error ( "[rsvp] scheduled update failed:" , e ) ) ;
67- } , CACHE_DURATION_MS ) ;
68- }
69-
70- // Cleanup function to clear the background timer
71- export function shutdownRSVPTimer ( ) : void {
72- if ( global . __RSVP_CACHE_TIMER__ ) {
73- clearInterval ( global . __RSVP_CACHE_TIMER__ ) ;
74- global . __RSVP_CACHE_TIMER__ = undefined ;
75- console . info ( "[rsvp] RSVP cache timer cleared." ) ;
76- }
77- }
78-
79- // Optionally clear timer on process exit (for dev/hot-reload environments)
80- if ( typeof process !== "undefined" && process . on ) {
81- process . on ( "SIGTERM" , shutdownRSVPTimer ) ;
82- process . on ( "SIGINT" , shutdownRSVPTimer ) ;
83- process . on ( "exit" , shutdownRSVPTimer ) ;
84- }
85-
8636export default function handler ( _req : NextApiRequest , res : NextApiResponse ) {
87- if ( cached . value === null ) {
88- res . status ( 503 ) . json ( { error : "Service Unavailable: RSVP count not yet available." } ) ;
89- return ;
90- }
91-
92- res . status ( 200 ) . json ( {
93- count : cached . value ,
94- updatedAt : new Date ( cached . updated ) . toISOString ( ) ,
95- } ) ;
37+ getCount ( ) . then ( count => res . status ( 200 ) . json ( { count } ) ) ;
9638}
0 commit comments