Skip to content
UniKVS
Esc
↑↓navigate↵open⌘Jpreview
On this page

@unikvs/cache-storage

How to use CacheStorage in @unikvs/cache-storage, a storage that keeps Responses in the Cache API.

Overview

@unikvs/cache-storage Browser only is a storage that keeps Response objects in the Cache API. Use it to cache fetched responses as-is.

  • name is CacheStorage.
  • Requires open and close. open opens the Cache.
  • Map logical keys to Request objects with toRequest.
  • Uses a dedicated Cache, so clear deletes every enumerated request.

Install with:

npm install @unikvs/cache-storage
pnpm add @unikvs/cache-storage
yarn add @unikvs/cache-storage
bun add @unikvs/cache-storage
nub add @unikvs/cache-storage
aube add @unikvs/cache-storage

Usage

CacheStorage implements IStorage. Pass it to UniKvs with persist().

import { CacheStorage } from "@unikvs/cache-storage";

Constructor arguments:

PropType
cacheNamestring

Name of the Cache to use.

Typestring
toRequestIToRequest

Function that converts a logical key into a Request.

TypeIToRequest
caches?CacheStorage | undefined

CacheStorage of the Cache API to use. Uses the runtime Cache API when omitted.

TypeCacheStorage | undefined

The following example stores a Response and reads it back.

import { CacheStorage } from "@unikvs/cache-storage";
import * as unikvs from "unikvs";

const kvs = new unikvs.Builder<{
  page: unikvs.PlainValue<Response>;
}>()
  .persist(
    new CacheStorage({
      cacheName: "unikvs",
      toRequest: (key) => `/kvs/${key}`,
    }),
  )
  .build();

await kvs.open();
await kvs.set("page", new Response("hello"));

const res = await kvs.get("page");
console.log(await res.text()); // "hello"

await kvs.close();

Data types

Operation Supported data type
write/read Response
getWritable/getReadable chunks Uint8Array<ArrayBuffer>
  • Stores Response objects as-is.
  • Stream chunks support only Uint8Array<ArrayBuffer>.
  • Streams from getWritable concatenate chunks and save them as a Response on close.
  • Streams from getReadable emit the stored Response bytes as a single chunk.

Errors

This package throws the following errors.

KeyNotFoundError

Meaning

Thrown when the specified key does not exist. A shared error from @unikvs/core.

How to fix

Check with exists before reading.

Example

The following example catches KeyNotFoundError.

import { KeyNotFoundError } from "@unikvs/cache-storage";

try {
  await kvs.get("missing-key");
} catch (ex) {
  if (ex instanceof KeyNotFoundError) {
    console.log("Key not found");
  } else {
    throw ex;
  }
}

CacheStorageNotAvailableError

Meaning

Thrown at open time when no Cache API is found. No caches was injected and the runtime has no Cache API.

How to fix

Inject a CacheStorage via options.caches or run in a supported browser.

InvalidChunkTypeError

PropType
keystring

The key being operated on.

Typestring
chunkunknown

The received chunk value.

Typeunknown
chunkTypestring

The type name of the received chunk.

Typestring

Meaning

Thrown when a non-Uint8Array<ArrayBuffer> chunk is written to getWritable.

How to fix

Write Uint8Array chunks to streams.

Was this page helpful?