Cloudflare - v1.3.0
    Preparing search index...

    Class KVService<E>

    Cache capability, backed by Workers KV.

    Values are stored as JSON and expire on the TTL the configuration layer settled on, unless a call names its own.

    A cache is optional infrastructure: when the namespace is not bound, reads miss and writes are dropped rather than raising. That keeps a Worker deployed without KV working — slower, not broken.

    Bayu Dwiyan Satria

    1.0.0

    1.0.0

    Type Parameters

    Implements

    • CacheStore<E>
    Index
    • Reads a cached value.

      Type Parameters

      • T = unknown

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The cache key.

      Returns Promise<T>

      The cached value, or null on a miss or an unbound namespace.

    • Reports whether the cache is bound to this Worker.

      Parameters

      • env: E

        The Worker environment.

      Returns boolean

      true when the binding is present.

    • Lists the keys under a prefix.

      Parameters

      • env: E

        The Worker environment.

      • prefix: string

        The key prefix to match.

      Returns Promise<string[]>

      The matched key names.

      One page only — KV paginates, and a cache listing is rarely worth walking to the end.

    • Returns a cached value, computing and caching it on a miss.

      The read-through pattern most callers actually want — one call instead of a get, a branch, and a set.

      Type Parameters

      • T

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The cache key.

      • load: () => Promise<T>

        Computes the value when the cache misses.

      • Optionalttl: number

        Lifetime in seconds. Falls back to the configured default.

      Returns Promise<T>

      The cached or freshly computed value.

      const article = await this.cache.remember(env, `article:${id}`, () => this.load(env, id))
      
    • Drops a cached value.

      Deleting a key that was never written is not an error.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The cache key.

      Returns Promise<void>

    • Writes a value.

      Parameters

      • env: E

        The Worker environment.

      • key: string

        The cache key.

      • value: unknown

        The value to store.

      • Optionalttl: number

        Lifetime in seconds. Falls back to the configured default; Cloudflare enforces a minimum of 60.

      Returns Promise<void>