Caches

Shovel implements the standard Cache API for storing Request/Response pairs.


CacheStorage

Global self.caches implements CacheStorage.

open(name: string): Promise<Cache>

Opens a named cache, creating it if it doesn't exist.

const cache = await self.caches.open("pages");

match(request: Request | string, options?: CacheQueryOptions): Promise<Response | undefined>

Searches all caches for a matching response.

const response = await self.caches.match(request);

has(name: string): Promise<boolean>

Returns whether a named cache exists.

const exists = await self.caches.has("pages");

delete(name: string): Promise<boolean>

Deletes a named cache. Returns true if the cache existed.

await self.caches.delete("old-cache");

keys(): Promise<string[]>

Returns all cache names.

const names = await self.caches.keys();

Cache

Each cache implements the Cache interface.

put(request: Request | string, response: Response): Promise<void>

Stores a request/response pair.

await cache.put(request, response.clone());

match(request: Request | string, options?: CacheQueryOptions): Promise<Response | undefined>

Retrieves a cached response.

const response = await cache.match(request);

matchAll(request?: Request | string, options?: CacheQueryOptions): Promise<Response[]>

Retrieves all matching responses.

const responses = await cache.matchAll("/api/");

add(request: Request | string): Promise<void>

Fetches a URL and caches the response.

await cache.add("/styles.css");

addAll(requests: (Request | string)[]): Promise<void>

Fetches multiple URLs and caches all responses.

await cache.addAll(["/", "/styles.css", "/app.js"]);

delete(request: Request | string, options?: CacheQueryOptions): Promise<boolean>

Removes a cached entry. Returns true if the entry existed.

await cache.delete("/old-page");

keys(request?: Request | string, options?: CacheQueryOptions): Promise<Request[]>

Lists cached requests.

const requests = await cache.keys();

CacheQueryOptions

Option Type Description
ignoreSearch boolean Ignore URL query string
ignoreMethod boolean Ignore request method
ignoreVary boolean Ignore Vary header

Configuration

Configure caches in shovel.json:

{
  "caches": {
    "pages": {
      "module": "@b9g/cache/memory"
    }
  }
}

Use "*" for a catch-all default:

{
  "caches": {
    "*": { "module": "@b9g/cache/memory" }
  }
}

Configuration Fields

Field Type Description
module string Module path to import
export string Named export (default: "default")
maxEntries number Maximum cache entries
ttl number Time-to-live in seconds

Implementations

Module Description
@b9g/cache/memory In-memory storage (lost on restart)
@b9g/cache-redis Redis-backed persistent cache

See Also