@unikvs/redis.bun
How to use @unikvs/redis.bun, which stores byte sequences in Redis from Bun.
Overview
@unikvs/redis.bun is a storage plugin that stores byte sequences in Redis. Bun only Use it for caches or temporary data shared across nodes.
nameis"Redis", andisOpenreports whether it is open.open()connects, andclose()disconnects when done.- This storage prefixes keys with
"unikvs:"by default, limiting the scope ofclear()deletions. - When the connection URL is omitted, the client resolves it from environment variables (
REDIS_URL,VALKEY_URL).
Install it with the following command:
npm install @unikvs/redis.bunpnpm add @unikvs/redis.bunyarn add @unikvs/redis.bunbun add @unikvs/redis.bunnub add @unikvs/redis.bunaube add @unikvs/redis.bunA Redis server is required separately.
Usage
Redis implements IStorage. Pass it to UniKvs via persist().
import { Redis } from "@unikvs/redis.bun";
Redis
Storage that saves to Redis. Callers open it with open() and close it with close() when done.
The constructor arguments are as follows:
RedisStorageOptions
Bun RedisOptions plus this package’s own fields. Bun fields are passed through as-is.
keyPrefix?string
The prefix prepended to every key. Defaults to `"unikvs:"`. Specify an empty string to use keys as-is.
stringThe following example saves a value and reads it back:
import { Redis } from "@unikvs/redis.bun";
import * as unikvs from "unikvs";
const kvs = new unikvs.Builder<{ file: unikvs.Value<Uint8Array> }>()
.persist(new Redis("redis://127.0.0.1:6379"))
.build();
await kvs.open();
await kvs.set("file", new Uint8Array([0, 1, 2]));
const data = await kvs.get("file");
console.log(data); // Uint8Array(3) [0, 1, 2]
await kvs.close();
Data types
| Operation | Data type |
|---|---|
write / read |
Uint8Array<ArrayBuffer> |
getWritable / getReadable chunks |
Uint8Array<ArrayBuffer> |
write/readuse byte sequences (Uint8Array<ArrayBuffer>).- Supports streamed reading and writing with
Uint8Array<ArrayBuffer>chunks. - Write streams append to a temporary key and atomically replace it with a rename on completion.
- Read streams emit the fetched bytes as a single chunk.
Errors
This package has no package-specific errors. Reading a missing key throws the shared @unikvs/core error KeyNotFoundError, and opening it on a runtime other than Bun throws the shared error UnsupportedRuntimeError.