Get a value from cache
Cache key
Type string or options object (defaults to 'text')
Value in specified type, or null if not found
Get a value with metadata from cache
Cache key
OptionaltypeOrOptions: "text" | KVNamespaceGetOptions<"text">Type string or options object (defaults to 'text')
Object with value, metadata, and cacheStatus
Get a value with metadata from cache
Cache key
Type string or options object (defaults to 'text')
Object with value, metadata, and cacheStatus
Get a value with metadata from cache
Cache key
Type string or options object (defaults to 'text')
Object with value, metadata, and cacheStatus
Get a value with metadata from cache
Cache key
Type string or options object (defaults to 'text')
Object with value, metadata, and cacheStatus
List keys in cache
Optionaloptions: KVNamespaceListOptionsList options (limit, prefix, cursor)
List result with keys and pagination info
Store a value in cache
Cache key
Value to store (string, ArrayBuffer, ArrayBufferView, or ReadableStream)
Optionaloptions: KVNamespacePutOptionsPut options (expiration, expirationTtl, metadata)
Set the KV namespace binding
Used internally by withBinding() to configure different KV instances.
KV namespace to use
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
KV namespace to use
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)
}
}
Cache Service
Type-safe wrapper around Cloudflare KV namespaces for caching operations.
Features:
withBinding()Usage:
See
https://developers.cloudflare.com/kv/api/