{"version":3,"file":"inmemory-CVHnncRp.cjs","names":["MastraBase","TTLCache"],"sources":["../src/cache/base.ts","../src/cache/inmemory.ts"],"sourcesContent":["import { MastraBase } from '../base';\n\nexport abstract class MastraServerCache extends MastraBase {\n  constructor({ name }: { name: string }) {\n    super({\n      component: 'SERVER_CACHE',\n      name,\n    });\n  }\n\n  abstract get(key: string): Promise<unknown>;\n\n  abstract listLength(key: string): Promise<number>;\n\n  /**\n   * Store a value in the cache.\n   * @param key - Cache key\n   * @param value - Value to store\n   * @param ttlMs - Optional per-key TTL in milliseconds. If not provided, uses\n   *   the implementation's default TTL.\n   */\n  abstract set(key: string, value: unknown, ttlMs?: number): Promise<void>;\n\n  abstract listPush(key: string, value: unknown): Promise<void>;\n\n  abstract listFromTo(key: string, from: number, to?: number): Promise<unknown[]>;\n\n  abstract delete(key: string): Promise<void>;\n\n  abstract clear(): Promise<void>;\n\n  /**\n   * Atomically increment a counter and return the new value.\n   * Used for generating sequential indices for events.\n   * Returns 1 on first call (counter starts at 0, increments to 1).\n   *\n   * For Redis: Uses INCR command which is atomic.\n   * For in-memory: Uses a simple counter map.\n   */\n  abstract increment(key: string): Promise<number>;\n}\n","import { TTLCache } from '@isaacs/ttlcache';\nimport { MastraServerCache } from './base';\n\n/**\n * Options for InMemoryServerCache\n */\nexport interface InMemoryServerCacheOptions {\n  /**\n   * Maximum number of items to store in cache.\n   * Defaults to 1000.\n   */\n  maxSize?: number;\n\n  /**\n   * Default TTL in milliseconds for cached items.\n   * Defaults to 300000 (5 minutes).\n   * Set to 0 to disable TTL (items persist until explicitly deleted or evicted).\n   */\n  ttlMs?: number;\n}\n\nexport class InMemoryServerCache extends MastraServerCache {\n  private cache: TTLCache<string, unknown>;\n  private ttlMs: number;\n\n  constructor(options: InMemoryServerCacheOptions = {}) {\n    super({ name: 'InMemoryServerCache' });\n\n    this.ttlMs = options.ttlMs ?? 1000 * 60 * 5;\n    // TTLCache requires positive integer or Infinity; use Infinity when TTL is disabled\n    const ttl = this.ttlMs > 0 ? this.ttlMs : Infinity;\n\n    this.cache = new TTLCache<string, unknown>({\n      max: options.maxSize ?? 1000,\n      ttl,\n    });\n  }\n\n  async get(key: string): Promise<unknown> {\n    return this.cache.get(key);\n  }\n\n  async set(key: string, value: unknown, ttlMs?: number): Promise<void> {\n    if (ttlMs === undefined) {\n      this.cache.set(key, value);\n      return;\n    }\n    // TTLCache requires positive integer or Infinity; non-positive overrides\n    // mean \"no expiry\" and must be normalized.\n    this.cache.set(key, value, { ttl: ttlMs > 0 ? ttlMs : Infinity });\n  }\n\n  async listLength(key: string): Promise<number> {\n    const value = this.cache.get(key);\n    if (value === undefined) {\n      return 0; // Key doesn't exist - return 0\n    }\n    if (!Array.isArray(value)) {\n      throw new Error(`${key} exists but is not an array`);\n    }\n    return value.length;\n  }\n\n  async listPush(key: string, value: unknown): Promise<void> {\n    const existing = this.cache.get(key);\n    if (Array.isArray(existing)) {\n      existing.push(value);\n      // Refresh TTL on push by re-setting the key with the updated list\n      if (this.ttlMs > 0) {\n        this.cache.set(key, existing, { ttl: this.ttlMs });\n      }\n    } else if (existing !== undefined) {\n      throw new Error(`${key} exists but is not an array`);\n    } else {\n      this.cache.set(key, [value]);\n    }\n  }\n\n  async listFromTo(key: string, from: number, to: number = -1): Promise<unknown[]> {\n    const list = this.cache.get(key) as unknown[];\n    if (Array.isArray(list)) {\n      // Make 'to' inclusive like Redis LRANGE - add 1 unless it's -1\n      const endIndex = to === -1 ? undefined : to + 1;\n      return list.slice(from, endIndex);\n    }\n    return [];\n  }\n\n  async delete(key: string): Promise<void> {\n    this.cache.delete(key);\n  }\n\n  async clear(): Promise<void> {\n    this.cache.clear();\n  }\n\n  async increment(key: string): Promise<number> {\n    const value = this.cache.get(key);\n    let counter: number;\n    if (value === undefined) {\n      counter = 1;\n    } else if (typeof value === 'number') {\n      counter = value + 1;\n    } else {\n      throw new Error(`${key} exists but is not a number`);\n    }\n    this.cache.set(key, counter);\n    return counter;\n  }\n}\n"],"mappings":";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