fix(db): serve getPricingForModel() from the pricing cache (#13891) (#14040)

getPricingForModel() called the uncached getPricing() on every
invocation instead of the existing getCachedPricing() helper
(30s TTL, readCache.ts), so usageStats.getUsageStats() re-ran a
3-SELECT + JSON.parse + merge cycle against key_value once per
GROUP BY row -- up to 531 times on a large usage_history table --
blocking the event loop for several seconds on /api/usage/history.

Every known pricing writer (updatePricing, LiteLLM/models.dev sync)
already invalidates this cache via touchPricing()/invalidateDbCache,
so a write remains immediately visible; added a regression test that
proves both the cache hit path and the invalidation path.
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-09-18 11:57:00 -03:00
committed by GitHub
parent e94752fa53
commit a96c8381f7
3 changed files with 65 additions and 2 deletions

View File

@@ -4,7 +4,7 @@
import { getDbInstance } from "../core";
import { backupDbFile } from "../backup";
import { invalidateDbCache } from "../readCache";
import { getCachedPricing, invalidateDbCache } from "../readCache";
import { PROVIDER_ID_TO_ALIAS } from "@omniroute/open-sse/config/providerModels.ts";
import { type JsonRecord, toRecord } from "./shared";
@@ -131,7 +131,7 @@ export async function getPricingWithSources(): Promise<{
}
export async function getPricingForModel(provider: string, model: string) {
const pricing = await getPricing();
const pricing = (await getCachedPricing()) as PricingByProvider;
const findKeyInsensitive = <T>(
obj: Record<string, T> | undefined | null,