sfcc-dev-mcp
Version:
MCP server for Salesforce B2C Commerce Cloud development assistance including logs, debugging, and development tools
114 lines • 3.13 kB
JavaScript
/**
* Simple in-memory cache for log operations
* Provides short-term caching to avoid repeated API calls for common operations
*/
export class LogCache {
cache = new Map();
DEFAULT_TTL = 5000; // 5 seconds default TTL
MAX_CACHE_SIZE = 100; // Prevent memory bloat
/**
* Get cached data if available and not expired
*/
get(key) {
const entry = this.cache.get(key);
if (!entry) {
return null;
}
// Check if expired
if (Date.now() - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return null;
}
return entry.data;
}
/**
* Set cached data with TTL
*/
set(key, data, ttl = this.DEFAULT_TTL) {
// Implement basic LRU by removing oldest entries when cache is full
if (this.cache.size >= this.MAX_CACHE_SIZE) {
const oldestKey = this.cache.keys().next().value;
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
this.cache.set(key, {
data,
timestamp: Date.now(),
ttl,
});
}
/**
* Generate cache key for log operations
*/
static generateKey(operation, args) {
const sortedArgs = Object.keys(args)
.sort()
.map(key => `${key}=${args[key]}`)
.join('&');
return `${operation}:${sortedArgs}`;
}
/**
* Clear all cached entries
*/
clear() {
this.cache.clear();
}
/**
* Remove expired entries (can be called periodically)
*/
cleanup() {
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
}
}
}
}
/**
* Enhanced log client wrapper with caching capabilities
* Provides transparent caching for frequently accessed log operations
*/
export class CachedLogClient {
logClient;
cache = new LogCache();
CACHEABLE_OPERATIONS = new Set([
'list_log_files',
'get_latest_job_log_files',
'summarize_logs',
]);
constructor(logClient) {
this.logClient = logClient;
}
/**
* Execute operation with caching if applicable
*/
async executeWithCache(operation, args, executor, ttl) {
// Only cache certain operations to avoid stale data issues
if (!this.CACHEABLE_OPERATIONS.has(operation)) {
return executor();
}
const cacheKey = LogCache.generateKey(operation, args);
const cached = this.cache.get(cacheKey);
if (cached !== null) {
return cached;
}
const result = await executor();
this.cache.set(cacheKey, result, ttl);
return result;
}
/**
* Clear cache - useful when log state might have changed
*/
clearCache() {
this.cache.clear();
}
/**
* Cleanup expired cache entries
*/
cleanupCache() {
this.cache.cleanup();
}
}
//# sourceMappingURL=log-cache.js.map