Stratal API Reference
    Preparing search index...

    Class CacheService

    Cache Service

    Type-safe wrapper around Cloudflare KV namespaces for caching operations.

    Features:

    • Mirrors all KVNamespace methods with full type safety
    • Supports multiple KV bindings via withBinding()
    • Automatic error handling with logging
    • Security: Raw errors are logged, not exposed to users

    Usage:

    class MyService {
    private readonly uploadsCache: CacheService

    constructor(
    @inject(CACHE_TOKENS.CacheService) private readonly cache: CacheService,
    @inject(DI_TOKENS.CloudflareEnv) private readonly env: Env
    ) {
    // Initialize specialized caches in constructor
    this.uploadsCache = this.cache.withBinding(this.env.UPLOADS_CACHE)
    }

    async cacheData(key: string, value: string) {
    await this.cache.put(key, value, { expirationTtl: 3600 })
    await this.uploadsCache.put(`upload:${key}`, value)
    }
    }
    Index

    Constructors

    Methods

    • Get a value from cache

      Parameters

      • key: string

        Cache key

      • OptionaltypeOrOptions: "text" | KVNamespaceGetOptions<"text">

        Type string or options object (defaults to 'text')

      Returns Promise<string | null>

      Value in specified type, or null if not found

      If operation fails

    • Get a value from cache

      Type Parameters

      • ExpectedValue = unknown

      Parameters

      • key: string

        Cache key

      • typeOrOptions: "json" | KVNamespaceGetOptions<"json">

        Type string or options object (defaults to 'text')

      Returns Promise<ExpectedValue | null>

      Value in specified type, or null if not found

      If operation fails

    • Get a value from cache

      Parameters

      • key: string

        Cache key

      • typeOrOptions: "arrayBuffer" | KVNamespaceGetOptions<"arrayBuffer">

        Type string or options object (defaults to 'text')

      Returns Promise<ArrayBuffer | null>

      Value in specified type, or null if not found

      If operation fails

    • Get a value from cache

      Parameters

      • key: string

        Cache key

      • typeOrOptions: "stream" | KVNamespaceGetOptions<"stream">

        Type string or options object (defaults to 'text')

      Returns Promise<ReadableStream<any> | null>

      Value in specified type, or null if not found

      If operation fails

    • Get a value with metadata from cache

      Type Parameters

      • Metadata = unknown

      Parameters

      • key: string

        Cache key

      • OptionaltypeOrOptions: "text" | KVNamespaceGetOptions<"text">

        Type string or options object (defaults to 'text')

      Returns Promise<KVNamespaceGetWithMetadataResult<string, Metadata>>

      Object with value, metadata, and cacheStatus

      If operation fails

    • Get a value with metadata from cache

      Type Parameters

      • ExpectedValue = unknown
      • Metadata = unknown

      Parameters

      • key: string

        Cache key

      • typeOrOptions: "json" | KVNamespaceGetOptions<"json">

        Type string or options object (defaults to 'text')

      Returns Promise<KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>

      Object with value, metadata, and cacheStatus

      If operation fails

    • Get a value with metadata from cache

      Type Parameters

      • Metadata = unknown

      Parameters

      • key: string

        Cache key

      • typeOrOptions: "arrayBuffer" | KVNamespaceGetOptions<"arrayBuffer">

        Type string or options object (defaults to 'text')

      Returns Promise<KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>>

      Object with value, metadata, and cacheStatus

      If operation fails

    • Get a value with metadata from cache

      Type Parameters

      • Metadata = unknown

      Parameters

      • key: string

        Cache key

      • typeOrOptions: "stream" | KVNamespaceGetOptions<"stream">

        Type string or options object (defaults to 'text')

      Returns Promise<KVNamespaceGetWithMetadataResult<ReadableStream<any>, Metadata>>

      Object with value, metadata, and cacheStatus

      If operation fails

    • List keys in cache

      Type Parameters

      • Metadata = unknown

      Parameters

      • Optionaloptions: KVNamespaceListOptions

        List options (limit, prefix, cursor)

      Returns Promise<KVNamespaceListResult<Metadata, string>>

      List result with keys and pagination info

      If operation fails

      // List all keys
      const result = await cache.list()

      // List with prefix
      const result = await cache.list({ prefix: 'user:' })

      // Paginated list
      const result = await cache.list({ limit: 100 })
      if (!result.list_complete) {
      const nextPage = await cache.list({ cursor: result.cursor })
      }
    • Store a value in cache

      Parameters

      • key: string

        Cache key

      • value: string | ArrayBuffer | ReadableStream<any> | ArrayBufferView<ArrayBufferLike>

        Value to store (string, ArrayBuffer, ArrayBufferView, or ReadableStream)

      • Optionaloptions: KVNamespacePutOptions

        Put options (expiration, expirationTtl, metadata)

      Returns Promise<void>

      If operation fails

      // Simple put
      await cache.put('key', 'value')

      // With TTL
      await cache.put('key', 'value', { expirationTtl: 3600 })

      // With metadata
      await cache.put('key', 'value', { metadata: { created: Date.now() } })
    • Create a new CacheService instance with a different KV binding

      Pattern: Returns a new instance (immutable)

      Best Practice: Initialize specialized caches as class properties in constructor

      Parameters

      • kv: KVNamespace

        KV namespace to use

      Returns CacheService

      New CacheService instance with the specified binding

      class MyService {
      private readonly uploadsCache: CacheService
      private readonly systemCache: CacheService

      constructor(
      @inject(CACHE_TOKENS.CacheService) private readonly cache: CacheService,
      @inject(DI_TOKENS.CloudflareEnv) private readonly env: Env
      ) {
      this.uploadsCache = this.cache.withBinding(this.env.UPLOADS_CACHE)
      this.systemCache = this.cache.withBinding(this.env.SYSTEM_CONFIG_KV)
      }
      }