[object Object]

← back to Ventura Claw Leads

add in-process 120s cache for /api/businesses.geo to prevent RSS spikes

f62acb4e583e1ed2df346e77fe5d884b7e8b84e8 · 2026-05-07 09:25:52 -0700 · Steve Abrams

Files touched

Diff

commit f62acb4e583e1ed2df346e77fe5d884b7e8b84e8
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Thu May 7 09:25:52 2026 -0700

    add in-process 120s cache for /api/businesses.geo to prevent RSS spikes
---
 routes/public.js | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/routes/public.js b/routes/public.js
index b50afbb..bf6cbaf 100644
--- a/routes/public.js
+++ b/routes/public.js
@@ -4,6 +4,13 @@ const db = require('../lib/db');
 const { VERTICALS, BY_KEY } = require('../lib/verticals');
 const router = express.Router();
 
+// In-process cache for /api/businesses.geo — prevents repeated full-table scans
+// from spiking Node RSS beyond the pm2 max_memory_restart threshold.
+// TTL matches the HTTP cache-control: max-age=120.
+let _geoCache = null;
+let _geoCacheAt = 0;
+const GEO_CACHE_TTL_MS = 120_000;
+
 router.get('/', async (req, res, next) => {
   try {
     const stats = await db.one(`
@@ -394,13 +401,18 @@ router.get('/api/businesses/suggest', async (req, res, next) => {
 
 router.get('/api/businesses.geo', async (req, res, next) => {
   try {
-    const rows = await db.many(`
-      SELECT id, slug, business_name, vertical, neighborhood, city, state, headline,
-             latitude, longitude, tier, claim_status, verified
-        FROM businesses
-       WHERE status = 'active' AND latitude IS NOT NULL AND longitude IS NOT NULL
-    `);
-    res.set('cache-control', 'public, max-age=120').json({ businesses: rows });
+    const now = Date.now();
+    if (!_geoCache || (now - _geoCacheAt) > GEO_CACHE_TTL_MS) {
+      const rows = await db.many(`
+        SELECT id, slug, business_name, vertical, neighborhood, city, state, headline,
+               latitude, longitude, tier, claim_status, verified
+          FROM businesses
+         WHERE status = 'active' AND latitude IS NOT NULL AND longitude IS NOT NULL
+      `);
+      _geoCache = { businesses: rows };
+      _geoCacheAt = now;
+    }
+    res.set('cache-control', 'public, max-age=120').json(_geoCache);
   } catch (err) { next(err); }
 });
 

← 602dc99 increase max_memory_restart from 300M to 600M to stop OOM-tr  ·  back to Ventura Claw Leads  ·  Add scripts/dedupe-corridor-shells.sql + hide 119 noisy seed 1acea14 →