Spec-Zone.ru › Deno 2

Cache

Tools for caching and managing data. Store and retrieve data from the cache, manage cache storage limits, and handle cache-related events.

Eg caches


interface Cache

Represents a single named store of Request/Response pairs. Obtain a Cache via CacheStorage.open and use it to persist responses and later match incoming requests against them.

Methods

put(
request: RequestInfo | URL,
response: Response,
): Promise<void>

Put the provided request/response into the cache.

How is the API different from browsers?

  1. You cannot match cache objects using relative paths.
  2. You cannot pass options like ignoreVary, ignoreMethod, ignoreSearch.
match(
request: RequestInfo | URL,
options?: CacheQueryOptions,
): Promise<Response | undefined>

Return cache object matching the provided request.

How is the API different from browsers?

  1. You cannot match cache objects using relative paths.
  2. You cannot pass options like ignoreVary, ignoreMethod, ignoreSearch.
delete(
request: RequestInfo | URL,
options?: CacheQueryOptions,
): Promise<boolean>

Delete cache object matching the provided request.

How is the API different from browsers?

  1. You cannot delete cache objects using relative paths.
  2. You cannot pass options like ignoreVary, ignoreMethod, ignoreSearch.
keys(
request?: RequestInfo | URL,
options?: CacheQueryOptions,
): Promise<ReadonlyArray<Request>>

Return the Request keys stored in the cache, in insertion order. When a request is provided, only the matching keys are returned.

How is the API different from browsers?

  1. You cannot match cache objects using relative paths.
  2. You cannot pass options like ignoreVary, ignoreMethod, ignoreSearch.

variable Cache

The constructor object for Cache.

Cache instances are obtained via CacheStorage.open rather than constructed directly, so calling the constructor throws.

Properties

prototype: Cache
readonly

interface CacheQueryOptions

Properties

ignoreMethod: boolean
optional
ignoreSearch: boolean
optional
ignoreVary: boolean
optional

interface CacheStorage

Represents the storage for named Cache objects. It provides the methods used to open, enumerate, look up, and delete caches, and is accessed via the global caches property.

Methods

open(cacheName: string): Promise<Cache>

Open a cache storage for the provided name.

has(cacheName: string): Promise<boolean>

Check if cache already exists for the provided name.

delete(cacheName: string): Promise<boolean>

Delete cache storage for the provided name.

keys(): Promise<string[]>

Return an array of all cache names tracked by the cache storage.

match(
request: RequestInfo | URL,
options?: MultiCacheQueryOptions,
): Promise<Response | undefined>

Check if a given Request or URL string is a key for a stored Response. Returns the matching Response, or undefined if no match is found.

If options.cacheName is provided, only the cache with that name is searched. Otherwise, all caches are searched in creation order.

variable CacheStorage

The constructor object for CacheStorage.

The CacheStorage instance is accessed via the global caches property rather than constructed directly, so calling the constructor throws.

Properties

prototype: CacheStorage
readonly

interface MultiCacheQueryOptions

extends CacheQueryOptions

Properties

cacheName: string
optional

variable caches

The global CacheStorage instance, providing access to the named Cache objects used to store and retrieve Request/Response pairs.

Type

CacheStorage

Provides access to the Cache API. Returns a CacheStorage object, which enables storing, retrieving, and managing request/response pairs in a cache.

Examples

// Open (or create) a cache
const cache = await caches.open('v1');

// Store a response
await cache.put('/api/data', new Response('Hello World'));

// Retrieve from cache with fallback
const response = await caches.match('/api/data') || await fetch('/api/data');

// Delete specific cache
await caches.delete('v1');

// List all cache names
const cacheNames = await caches.keys();

// Cache-first strategy
async function fetchWithCache(request) {
  const cached = await caches.match(request);
  if (cached) return cached;

  const response = await fetch(request);
  const cache = await caches.open('v1');
  await cache.put(request, response.clone());
  return response;
}

Type

CacheStorage

See

  • https://developer.mozilla.org/en-US/docs/Web/API/Window/caches


© 2018–2025 the Deno authors
Licensed under the MIT License.
https://docs.deno.com/api/web/cache

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API