Key Values
Use key-value schemas when one Redis key stores one scalar or serialized value.
Define A Key-Value Schema
Section titled “Define A Key-Value Schema”import { json, kv } from "benni/schema";
type UserProfile = { name: string; score: number;};
export const profiles = kv("profile", json<UserProfile>());await redis.kv(profiles).set("42", { name: "Ada", score: 10});const profile = await redis.kv(profiles).get("42");// ^? UserProfile | nullWith TTL
Section titled “With TTL”await redis.kv(profiles).set( "42", { name: "Ada", score: 10 }, { ttlSeconds: 3600 });Conditional Writes
Section titled “Conditional Writes”const created = await redis.kv(profiles).set("42", profile, { nx: true, ttlSeconds: 3600});
const updated = await redis.kv(profiles).set("42", profile, { xx: true});Raw Redis Equivalent
Section titled “Raw Redis Equivalent”await nodeRedis.set("profile:42", JSON.stringify(profile), { EX: 3600});Use key-value schemas for sessions, feature flags, cached API responses, and values that are usually read or written as a whole.