SPB Git

Repository Public

cache.ts 3.0 KB 81 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/lib/cost/cache.ts9 * Module           : lib10 * Repository       : https://git.spboucher.ai/vrai-prix11 * Created          : 2026-09-0812 * Last Modified    : 2026-09-0813 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142/**43 * Cache mémoire simple (par processus) pour les lectures coûteuses de la base44 * de coûts : aperçu, assemblages, indices. Invalidé par TTL et par la clé45 * `last_ingest` (cost_meta) mise à jour à chaque ingestion.46 */47import { metaGet } from "./db";4849interface Entry<T> { value: T; expires: number; ingest: string | null }50const store = new Map<string, Entry<unknown>>();5152export function cached<T>(key: string, ttlMs: number, compute: () => T): T {53  let ingest: string | null = null;54  try { ingest = metaGet("last_ingest"); } catch { /* base indisponible */ }55  const e = store.get(key) as Entry<T> | undefined;56  if (e && e.expires > Date.now() && e.ingest === ingest) return e.value;57  const value = compute();58  store.set(key, { value, expires: Date.now() + ttlMs, ingest });59  return value;60}6162export function invalidateCache(): void {63  store.clear();64}6566/** Limiteur de débit en mémoire (par clé, fenêtre glissante). */67const hits = new Map<string, number[]>();68export function rateLimit(key: string, max: number, windowMs: number): boolean {69  const now = Date.now();70  const arr = (hits.get(key) ?? []).filter((t) => now - t < windowMs);71  if (arr.length >= max) { hits.set(key, arr); return false; }72  arr.push(now);73  hits.set(key, arr);74  return true;75}7677export function clientKey(req: Request): string {78  const xff = req.headers.get("x-forwarded-for");79  return (xff ? xff.split(",").pop()!.trim() : req.headers.get("x-real-ip")) ?? "local";80}81
Files main
Menu

Theme