class MemoryCache {
  cache = /* @__PURE__ */ new Map();
  maxSize;
  defaultTTL;
  cleanupInterval = null;
  constructor(maxSize = 1e4, defaultTTL = 3e5) {
    this.maxSize = maxSize;
    this.defaultTTL = defaultTTL;
    if (process.env.NODE_ENV !== "test") {
      this.cleanupInterval = setInterval(() => this.cleanup(), 3e5);
    }
  }
  set(key, value, ttl) {
    const now = Date.now();
    const expiresAt = now + (ttl || this.defaultTTL);
    if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
      this.evictLRU();
    }
    this.cache.set(key, {
      value,
      expiresAt,
      lastAccessed: now
    });
  }
  get(key) {
    const entry = this.cache.get(key);
    if (!entry) {
      return null;
    }
    const now = Date.now();
    if (entry.expiresAt < now) {
      this.cache.delete(key);
      return null;
    }
    entry.lastAccessed = now;
    return entry.value;
  }
  delete(key) {
    return this.cache.delete(key);
  }
  has(key) {
    const entry = this.cache.get(key);
    if (!entry) {
      return false;
    }
    if (entry.expiresAt < Date.now()) {
      this.cache.delete(key);
      return false;
    }
    return true;
  }
  clear() {
    this.cache.clear();
  }
  size() {
    return this.cache.size;
  }
  destroy() {
    if (this.cleanupInterval) {
      clearInterval(this.cleanupInterval);
      this.cleanupInterval = null;
    }
    this.cache.clear();
  }
  evictLRU() {
    let oldestKey = null;
    let oldestTime = Date.now();
    for (const [key, entry] of this.cache.entries()) {
      if (entry.lastAccessed < oldestTime) {
        oldestTime = entry.lastAccessed;
        oldestKey = key;
      }
    }
    if (oldestKey) {
      this.cache.delete(oldestKey);
    }
  }
  cleanup() {
    const now = Date.now();
    const toDelete = [];
    for (const [key, entry] of this.cache.entries()) {
      if (entry.expiresAt < now) {
        toDelete.push(key);
      }
    }
    toDelete.forEach((key) => this.cache.delete(key));
  }
  // Método para obtener estadísticas
  getStats() {
    const now = Date.now();
    let expired = 0;
    let active = 0;
    for (const entry of this.cache.values()) {
      if (entry.expiresAt < now) {
        expired++;
      } else {
        active++;
      }
    }
    return {
      total: this.cache.size,
      active,
      expired,
      maxSize: this.maxSize
    };
  }
}
const rateLimitCache = new MemoryCache(5e3, 6e4);
const apiKeyCache = new MemoryCache(1e3, 3e5);
const userCache = new MemoryCache(2e3, 6e5);
function getCacheKey(prefix, ...parts) {
  return `${prefix}:${parts.join(":")}`;
}
function invalidatePattern(cache, pattern) {
  const keys = Array.from(cache.cache.keys());
  const regex = new RegExp(pattern.replace("*", ".*"));
  keys.forEach((key) => {
    if (regex.test(key)) {
      cache.delete(key);
    }
  });
}
export {
  apiKeyCache,
  getCacheKey,
  invalidatePattern,
  rateLimitCache,
  userCache
};
