Files
CurrenciCombo/orchestrator/src/services/performance.ts

49 lines
943 B
TypeScript
Raw Normal View History

import { cacheGet, cacheSet } from "./cache";
import { getPlanById } from "../db/plans";
/**
* Performance optimization utilities
*/
/**
* Get plan with caching
*/
export async function getPlanWithCache(planId: string) {
const cacheKey = `plan:${planId}`;
// Try cache first
const cached = await cacheGet(cacheKey);
if (cached) {
return cached;
}
// Get from database
const plan = await getPlanById(planId);
// Cache for 5 minutes
if (plan) {
await cacheSet(cacheKey, plan, 300);
}
return plan;
}
/**
* Batch API calls
*/
export async function batchApiCalls<T>(
calls: Array<() => Promise<T>>,
batchSize = 10
): Promise<T[]> {
const results: T[] = [];
for (let i = 0; i < calls.length; i += batchSize) {
const batch = calls.slice(i, i + batchSize);
const batchResults = await Promise.all(batch.map((call) => call()));
results.push(...batchResults);
}
return results;
}