@@ -16,8 +16,22 @@ const NOTION_STORE_DATABASE_ID = "7c36ef34cebb48e097706442634abaaf";
1616const redis = require ( 'redis' ) ;
1717const client = redis . createClient ( ) ; // Connects to default 127.0.0.1:6379 on your Droplet
1818
19- client . on ( 'error' , ( err ) => console . log ( 'Redis Client Error' , err ) ) ;
20- client . connect ( ) ; // v4 of redis requires explicit connect
19+ client . on ( 'error' , ( err ) => {
20+ // console.log('Redis Client Error:', err.message);
21+ // This prevents the app from crashing; it just logs the error
22+ } ) ;
23+ let redisIsReady = false ;
24+ async function connectRedis ( ) {
25+ try {
26+ await client . connect ( ) ;
27+ redisIsReady = true ;
28+ console . log ( "Connected to Redis" ) ;
29+ } catch ( err ) {
30+ console . log ( "Redis not found. Running in 'No-Cache' mode." ) ;
31+ redisIsReady = false ;
32+ }
33+ }
34+ connectRedis ( ) ;
2135const CACHE_TTL = 3500 ; // 60 minutes in seconds
2236console . log ( "starting up" )
2337app . use ( express . static ( "public" ) )
@@ -254,7 +268,7 @@ app.get("/poetic-promenade", async (req,res) => {
254268 // console.log(response)
255269
256270 const response = await prepareEventData ( eventData , "poetic-promenade" )
257-
271+ console . log ( response )
258272 res . render ( "programs/prom" , response )
259273
260274 //
@@ -1036,6 +1050,7 @@ app.get("/sessions/:session/:class", async(req,res) => {
10361050 const cacheKey = `cache:session:${ session } :class:${ className } ` ;
10371051
10381052 try {
1053+ if ( redisIsReady ) {
10391054 // 1. Try to get data from Redis
10401055 const cachedData = await client . get ( cacheKey ) ;
10411056
@@ -1055,6 +1070,14 @@ app.get("/sessions/:session/:class", async(req,res) => {
10551070 res . status ( 404 ) . send ( "Class not found" ) ;
10561071 }
10571072 }
1073+ } else {
1074+ const freshData = await revalidateClassData ( session , className , cacheKey ) ;
1075+ if ( freshData ) {
1076+ res . render ( "programs/class-concurrent" , freshData ) ;
1077+ } else {
1078+ res . status ( 404 ) . send ( "Class not found" ) ;
1079+ }
1080+ }
10581081 } catch ( error ) {
10591082 console . error ( "Route Error:" , error ) ;
10601083 res . status ( 500 ) . send ( "Internal Server Error" ) ;
@@ -1161,6 +1184,7 @@ app.get("/blog/:slug", async (req, res) => {
11611184 const cacheKey = `cache:blog:${ slug } ` ;
11621185
11631186 try {
1187+ if ( redisIsReady ) {
11641188 const cachedData = await client . get ( cacheKey ) ;
11651189
11661190 if ( cachedData ) {
@@ -1187,6 +1211,19 @@ app.get("/blog/:slug", async (req, res) => {
11871211 res . status ( 404 ) . send ( "Post not found" ) ;
11881212 }
11891213 }
1214+ } else {
1215+ console . log ( "skipping redis" )
1216+ const freshData = await revalidateBlogData ( slug , cacheKey ) ;
1217+ if ( freshData ) {
1218+ res . render ( "blog/post" , {
1219+ title : freshData . Name ,
1220+ postHTML : freshData . postHTML ,
1221+ ...freshData
1222+ } ) ;
1223+ } else {
1224+ res . status ( 404 ) . send ( "Post not found" ) ;
1225+ }
1226+ }
11901227 } catch ( error ) {
11911228 console . error ( "Blog Route Error:" , error ) ;
11921229 res . status ( 500 ) . send ( "Internal Server Error" ) ;
@@ -1443,6 +1480,7 @@ async function prepareEventData(eventData, eventSlug){
14431480 const webContent = contentBlockId ? await getBlocks ( contentBlockId ) : [ ] ;
14441481 let response = parseEventData ( eventData )
14451482 response . pageContent = parsePageContentIntoKeyedObject ( webContent ) ;
1483+ response . Images = response [ "Event Photos" ]
14461484
14471485 return response
14481486}
@@ -2441,6 +2479,10 @@ function formatRichText(textArray) {
24412479}
24422480
24432481async function getCachedData ( key , fetcher ) {
2482+ if ( ! redisIsReady ) {
2483+ console . log ( `[Local] Skipping cache for: ${ key } ` ) ;
2484+ return await fetcher ( ) ;
2485+ }
24442486 const cached = await client . get ( key ) ;
24452487
24462488 if ( cached ) {
@@ -2471,11 +2513,14 @@ async function revalidateClassData(sessionSlug, classSlug, cacheKey) {
24712513
24722514 // Your original processing logic
24732515 const response = await prepareClassData ( data , classSlug ) ;
2474-
2516+ if ( redisIsReady ) {
24752517 // Store in Redis (Expire after 1 hour to account for image URL expiration)
2476- await client . set ( cacheKey , JSON . stringify ( response ) , {
2477- EX : 3500
2478- } ) ;
2518+ await client . set ( cacheKey , JSON . stringify ( response ) , {
2519+ EX : 3500
2520+ } ) ;
2521+ } else {
2522+ console . log ( "skipping redis" )
2523+ }
24792524
24802525 return response ;
24812526}
@@ -2494,11 +2539,15 @@ async function revalidateBlogData(slug, cacheKey) {
24942539 const postHTML = parsePageContentHTML ( pageContent ) ;
24952540
24962541 const finalData = { ...parsedData , postHTML } ;
2542+ if ( redisIsReady ) {
2543+ // Update Redis with a TTL (e.g., 1 hour)
2544+ await client . set ( cacheKey , JSON . stringify ( finalData ) , {
2545+ EX : 3500
2546+ } ) ;
2547+ } else {
2548+ console . log ( "skipping redis" )
2549+ }
24972550
2498- // Update Redis with a TTL (e.g., 1 hour)
2499- await client . set ( cacheKey , JSON . stringify ( finalData ) , {
2500- EX : 3500
2501- } ) ;
25022551
25032552 return finalData ;
25042553}
0 commit comments