Skip to main content
ContextManager accepts any ICache implementation. This allows swapping the default in-memory cache with Redis, a KV store, or your own logic.

ICache interface

import type { CacheEntryOptions, ICache } from '@illalabs/sdk'

class MyCache implements ICache {
  private store = new Map<string, { value: unknown; expiresAt?: number }>()

  async get<T>(key: string): Promise<T | undefined> {
    const rec = this.store.get(key)
    if (!rec) return undefined
    if (rec.expiresAt && Date.now() > rec.expiresAt) {
      this.store.delete(key)
      return undefined
    }
    return rec.value as T
  }

  async set(
    key: string,
    value: unknown,
    options?: CacheEntryOptions
  ): Promise<void> {
    const expiresAt = options?.ttl ? Date.now() + options.ttl : undefined
    this.store.set(key, { value, expiresAt })
  }

  async del(key: string): Promise<void> {
    this.store.delete(key)
  }
}

Use it with ContextManager

const context = new ContextManager(new MyCache(), {
  cacheKeyPrefix: 'illa:sdk:ctx'
})