// utils/ctxCache.js import crypto from "node:crypto"; import pool from "../config/mysqlPool.js"; import { query } from '../shared/db/withEncryption.js'; /** * @param {string} userId * @param {string} careerProfileId * @param {string} summaryText ← pre-built summary * @returns {string} ← cached or stored summary */ export async function cacheSummary(userId, careerProfileId, summaryText) { const hash = crypto.createHash("sha1").update(summaryText).digest("hex"); // 1. try cache const [rows] = await pool.query( `SELECT ctx_text FROM context_cache WHERE user_id=? AND career_profile_id=? AND ctx_hash=? LIMIT 1`, [userId, careerProfileId, hash] ); if (rows[0]) return rows[0].ctx_text; // 2. miss → store await pool.query( `REPLACE INTO context_cache (user_id, career_profile_id, ctx_hash, ctx_text) VALUES (?,?,?,?)`, [userId, careerProfileId, hash, summaryText] ); return summaryText; }