[object Object]

← back to AsSeenInMovies

asseeninmovies: /api/{movie,person,search,spotted} Cache-Control + parallelize /api/person/:id

7197fa39781ebcb19a9f35d5b7a1bfca8490fdd9 · 2026-05-12 23:16:28 -0700 · SteveStudio2

Files touched

Diff

commit 7197fa39781ebcb19a9f35d5b7a1bfca8490fdd9
Author: SteveStudio2 <stevestudio2@SteveStacStudio.lan>
Date:   Tue May 12 23:16:28 2026 -0700

    asseeninmovies: /api/{movie,person,search,spotted} Cache-Control + parallelize /api/person/:id
---
 server.js | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/server.js b/server.js
index 8911399..04a4f02 100644
--- a/server.js
+++ b/server.js
@@ -177,6 +177,8 @@ app.get('/search', async (req, res) => {
 });
 
 app.get('/api/search', async (req, res) => {
+  // 60s Cache-Control — search results are stable across short windows.
+  res.setHeader('Cache-Control', 'public, max-age=60, stale-while-revalidate=120');
   const q = String(req.query.q || '').trim();
   if (!q || q.length < 2) return res.json({ movies: [], people: [] });
   const like = '%' + q + '%';
@@ -198,15 +200,19 @@ app.get('/api/movie/:id', async (req, res) => {
   const id = parseInt(req.params.id, 10);
   if (!Number.isFinite(id) || id < 1) return res.status(400).json({ error: 'bad id' });
   try {
-    const m = await pool.query(`SELECT * FROM asim_movies WHERE id=$1 LIMIT 1`, [id]);
+    // 5-min Cache-Control — movie metadata is stable; only SPOTTED changes,
+    // and it's surfaced separately in /api/spotted with its own short cache.
+    const [m, credits, spotted] = await Promise.all([
+      pool.query(`SELECT * FROM asim_movies WHERE id=$1 LIMIT 1`, [id]),
+      pool.query(`
+        SELECT c.role, c.character_name, c.order_idx, c.job_title, c.department,
+               p.id AS person_id, p.full_name, p.headshot_url, p.headshot_source, p.premium_tier
+          FROM asim_credits c JOIN asim_people p ON p.id = c.person_id
+         WHERE c.movie_id=$1 ORDER BY c.order_idx NULLS LAST, p.full_name`, [id]),
+      pool.query(`SELECT * FROM asim_spotted WHERE movie_id=$1 ORDER BY verified_at DESC NULLS LAST, created_at DESC`, [id])
+    ]);
     if (!m.rows[0]) return res.status(404).json({ error: 'not found' });
-    const credits = await pool.query(`
-      SELECT c.role, c.character_name, c.order_idx, c.job_title, c.department,
-             p.id AS person_id, p.full_name, p.headshot_url, p.headshot_source, p.premium_tier
-        FROM asim_credits c JOIN asim_people p ON p.id = c.person_id
-       WHERE c.movie_id=$1 ORDER BY c.order_idx NULLS LAST, p.full_name`, [id]);
-    const spotted = await pool.query(`
-      SELECT * FROM asim_spotted WHERE movie_id=$1 ORDER BY verified_at DESC NULLS LAST, created_at DESC`, [id]);
+    res.setHeader('Cache-Control', 'public, max-age=300, stale-while-revalidate=600');
     res.json({ movie: m.rows[0], credits: credits.rows, spotted: spotted.rows });
   } catch (e) {
     res.status(500).json({ error: e.message });
@@ -218,18 +224,21 @@ app.get('/api/person/:id', async (req, res) => {
   const id = parseInt(req.params.id, 10);
   if (!Number.isFinite(id) || id < 1) return res.status(400).json({ error: 'bad id' });
   try {
-    const p = await pool.query(`SELECT * FROM asim_people WHERE id=$1 LIMIT 1`, [id]);
+    const [p, credits] = await Promise.all([
+      pool.query(`SELECT * FROM asim_people WHERE id=$1 LIMIT 1`, [id]),
+      pool.query(`
+        SELECT c.role, c.character_name, c.job_title, c.department,
+               m.id AS movie_id, m.title, m.release_year, m.poster_url, m.poster_source
+          FROM asim_credits c JOIN asim_movies m ON m.id = c.movie_id
+         WHERE c.person_id=$1 ORDER BY m.release_year DESC NULLS LAST`, [id])
+    ]);
     if (!p.rows[0]) return res.status(404).json({ error: 'not found' });
-    const credits = await pool.query(`
-      SELECT c.role, c.character_name, c.job_title, c.department,
-             m.id AS movie_id, m.title, m.release_year, m.poster_url, m.poster_source
-        FROM asim_credits c JOIN asim_movies m ON m.id = c.movie_id
-       WHERE c.person_id=$1 ORDER BY m.release_year DESC NULLS LAST`, [id]);
     // Strip premium_extras from public response unless claimed + active
     const person = p.rows[0];
     if (!person.claimed_at || (person.premium_expires_at && new Date(person.premium_expires_at) < new Date())) {
       person.premium_extras = null;
     }
+    res.setHeader('Cache-Control', 'public, max-age=300, stale-while-revalidate=600');
     res.json({ person, credits: credits.rows });
   } catch (e) {
     res.status(500).json({ error: e.message });
@@ -680,6 +689,7 @@ app.get('/api/spotted', async (req, res) => {
   const limit = Math.min(parseInt(req.query.limit, 10) || 60, 200);
   const cat = req.query.category;
   try {
+    res.setHeader('Cache-Control', 'public, max-age=60, stale-while-revalidate=120');
     const params = [];
     let where = `WHERE verified_at IS NOT NULL AND (verified_by IS NULL OR verified_by NOT LIKE 'rejected%')`;
     if (cat) { params.push(cat); where += ` AND product_category = $${params.length}`; }

← f78067e asseeninmovies: /api/spotted/stats Cache-Control max-age=30  ·  back to AsSeenInMovies  ·  asim-healthz 4de56c1 →