Revert "feat(package-executor): add 2-minute in-memory cache for execution results"
This reverts commit fb1ed57471.
This commit is contained in:
parent
7bd0caea97
commit
489d0125ac
1 changed files with 2 additions and 94 deletions
|
|
@ -6,70 +6,6 @@
|
||||||
import type { ExecutionResult, ExecutorOptions } from './types.js';
|
import type { ExecutionResult, ExecutorOptions } from './types.js';
|
||||||
|
|
||||||
const DEFAULT_TIMEOUT = 10000; // 10 seconds
|
const DEFAULT_TIMEOUT = 10000; // 10 seconds
|
||||||
const CACHE_TTL_MS = 2 * 60 * 1000; // 2 minutes
|
|
||||||
|
|
||||||
// In-memory cache for execution results
|
|
||||||
interface CacheEntry {
|
|
||||||
result: ExecutionResult;
|
|
||||||
expiresAt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
const executionCache = new Map<string, CacheEntry>();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generate a cache key from execution parameters
|
|
||||||
*/
|
|
||||||
function getCacheKey(
|
|
||||||
packageName: string,
|
|
||||||
functionName: string,
|
|
||||||
params: Record<string, unknown>
|
|
||||||
): string {
|
|
||||||
const paramsKey = JSON.stringify(params, Object.keys(params).sort());
|
|
||||||
return `${packageName}::${functionName}::${paramsKey}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get cached result if still valid
|
|
||||||
*/
|
|
||||||
function getCachedResult(cacheKey: string): ExecutionResult | null {
|
|
||||||
const entry = executionCache.get(cacheKey);
|
|
||||||
if (!entry) return null;
|
|
||||||
|
|
||||||
if (Date.now() > entry.expiresAt) {
|
|
||||||
executionCache.delete(cacheKey);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return entry.result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Store result in cache
|
|
||||||
*/
|
|
||||||
function setCachedResult(cacheKey: string, result: ExecutionResult): void {
|
|
||||||
// Only cache successful results
|
|
||||||
if (!result.success) return;
|
|
||||||
|
|
||||||
executionCache.set(cacheKey, {
|
|
||||||
result,
|
|
||||||
expiresAt: Date.now() + CACHE_TTL_MS,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Periodically clean up expired cache entries
|
|
||||||
*/
|
|
||||||
function cleanupCache(): void {
|
|
||||||
const now = Date.now();
|
|
||||||
for (const [key, entry] of executionCache.entries()) {
|
|
||||||
if (now > entry.expiresAt) {
|
|
||||||
executionCache.delete(key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run cleanup every minute
|
|
||||||
setInterval(cleanupCache, 60 * 1000).unref();
|
|
||||||
|
|
||||||
// Ensure URL has protocol
|
// Ensure URL has protocol
|
||||||
function getSandboxUrl(): string {
|
function getSandboxUrl(): string {
|
||||||
|
|
@ -95,16 +31,6 @@ export async function executePackage(
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
const timeout = options.timeout || DEFAULT_TIMEOUT;
|
const timeout = options.timeout || DEFAULT_TIMEOUT;
|
||||||
|
|
||||||
// Check cache first
|
|
||||||
const cacheKey = getCacheKey(packageName, functionName, params);
|
|
||||||
const cachedResult = getCachedResult(cacheKey);
|
|
||||||
if (cachedResult) {
|
|
||||||
return {
|
|
||||||
...cachedResult,
|
|
||||||
executionTimeMs: 0, // Indicate cache hit with 0ms execution time
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Call the remote sandbox service
|
// Call the remote sandbox service
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
|
|
@ -145,17 +71,12 @@ export async function executePackage(
|
||||||
executionTimeMs?: number;
|
executionTimeMs?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
const executionResult: ExecutionResult = {
|
return {
|
||||||
success: result.success,
|
success: result.success,
|
||||||
output: result.output,
|
output: result.output,
|
||||||
error: result.error,
|
error: result.error,
|
||||||
executionTimeMs: result.executionTimeMs || executionTimeMs,
|
executionTimeMs: result.executionTimeMs || executionTimeMs,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cache successful results
|
|
||||||
setCachedResult(cacheKey, executionResult);
|
|
||||||
|
|
||||||
return executionResult;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const executionTimeMs = Date.now() - startTime;
|
const executionTimeMs = Date.now() - startTime;
|
||||||
|
|
||||||
|
|
@ -176,12 +97,9 @@ export async function executePackage(
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the package cache on the remote sandbox and local execution cache
|
* Clear the package cache on the remote sandbox
|
||||||
*/
|
*/
|
||||||
export async function clearCache(): Promise<void> {
|
export async function clearCache(): Promise<void> {
|
||||||
// Clear local execution cache
|
|
||||||
executionCache.clear();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${SANDBOX_URL}/cache/clear`, {
|
const response = await fetch(`${SANDBOX_URL}/cache/clear`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
|
@ -196,16 +114,6 @@ export async function clearCache(): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get current cache statistics
|
|
||||||
*/
|
|
||||||
export function getCacheStats(): { size: number; ttlMs: number } {
|
|
||||||
return {
|
|
||||||
size: executionCache.size,
|
|
||||||
ttlMs: CACHE_TTL_MS,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the sandbox service is healthy
|
* Check if the sandbox service is healthy
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue