Skip to content

Proposal: KV cache #247

Description

@terrablue

Simple key-value interface for caching, sessions, and ephemeral state.

Interface

interface KV {
  get<T>(key: string): Promise<T | undefined>;
  set<T>(key: string, value: T, options?: { ttl?: number }): Promise<void>;
  delete(key: string): Promise<void>;
  has(key: string): Promise<boolean>;
}

ttl is in seconds.

Config

Default (in-memory):

// config/kv.ts
import kv from "primate/kv";

export default kv();

Redis:

// config/kv.ts
import redis from "@primate/redis";

export default redis({
  url: "redis://localhost:6379", // optional, this is the default
});

No config file = in-memory by default.

Usage

import kv from "#kv"; // given a tsconfig alias

await kv.set("session:abc", { userId: 123 }, { ttl: 3600 });

const session = await kv.get<{ userId: number }>("session:abc");

if (await kv.has("session:abc")) {
  // ...
}

await kv.delete("session:abc");

Drivers

primate/kv — in-memory, development/single-instance. Data lost on restart.
@primate/redis — distributed, production.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions