Files
OmniRoute/src/lib/settingsCache.ts
diegosouzapw 71d14209a4 feat: OmniRoute v1.0.0 — Intelligent AI Gateway & Universal LLM Proxy
OmniRoute is an intelligent API gateway that unifies 20+ AI providers behind a single
OpenAI-compatible endpoint. Features include intelligent routing with 6 strategies,
multi-format translation (OpenAI/Claude/Gemini/Responses API), circuit breakers,
semantic caching, combo fallback chains, real-time health monitoring, and a full
dashboard with provider management, analytics, and CLI tool integration.

Key highlights:
- 20+ providers (Claude Code, Codex, Gemini CLI, GitHub Copilot, iFlow, Qwen, Kiro, etc.)
- 6 routing strategies (Fill First, Round Robin, P2C, Random, Least Used, Cost Optimized)
- Export/Import database backup with full archive support
- Translator Playground with 4 modes (Playground, Chat Tester, Test Bench, Live Monitor)
- 100% TypeScript across src/ and open-sse/
- Docker support with multi-stage builds
- Comprehensive documentation and 9 dashboard screenshots
2026-02-18 00:02:15 -03:00

63 lines
1.4 KiB
TypeScript

/**
* Settings Cache — FASE-03 Architecture Refactoring
*
* In-memory cache for settings to eliminate self-fetch anti-pattern in middleware.
* The middleware was making HTTP requests to its own /api/settings endpoint,
* which caused circular dependencies and performance issues.
*
* @module settingsCache
*/
import { getSettings } from "@/lib/localDb";
/** @type {{ data: object|null, lastFetch: number, ttl: number }} */
const cache = {
data: null,
lastFetch: 0,
ttl: 5000, // 5 seconds TTL
};
/**
* Get settings from cache (or refresh if stale).
* This replaces the self-fetch pattern in middleware.
*
* @returns {Promise<object>} Settings object
*/
export async function getCachedSettings() {
const now = Date.now();
if (cache.data && now - cache.lastFetch < cache.ttl) {
return cache.data;
}
try {
const settings = await getSettings();
cache.data = settings;
cache.lastFetch = now;
return settings;
} catch (err) {
// If fetch fails but we have stale data, return it
if (cache.data) {
console.error("[SettingsCache] Failed to refresh, using stale data:", err.message);
return cache.data;
}
throw err;
}
}
/**
* Invalidate the cache (e.g. after settings update).
*/
export function invalidateSettingsCache() {
cache.data = null;
cache.lastFetch = 0;
}
/**
* Set the cache TTL in milliseconds.
* @param {number} ms
*/
export function setSettingsCacheTTL(ms) {
cache.ttl = ms;
}