Skip to content

Nebutra/cache

Repository files navigation

@nebutra/cache

Public mirror for @nebutra/cache from Nebutra/Nebutra-Sailor.

This repository is generated from the Nebutra Sailor monorepo. Package releases are cut from the monorepo and mirrored here for discovery, standalone cloning, and contribution intake.

  • Canonical source: packages/integrations/cache in Nebutra/Nebutra-Sailor
  • Package registry: npm and GitHub Packages
  • Contributions: open issues or PRs here; maintainers port accepted changes back into the monorepo source package

Redis caching strategies for Upstash Redis.

Installation

pnpm add @nebutra/cache

Strategies

Strategy Description
ttlCache Standard TTL-based caching
lockCache Distributed locks
stampede Cache stampede prevention
lazyRefresh Background refresh before expiry

Setup

UPSTASH_REDIS_REST_URL=https://...upstash.io
UPSTASH_REDIS_REST_TOKEN=...

Usage

TTL Cache

import { ttlCache } from "@nebutra/cache";

// Get or set with TTL
const data = await ttlCache.getOrSet(
  "user:123",
  async () => await fetchUser(123),
  { ttl: 3600 }, // 1 hour
);

Distributed Lock

import { lockCache } from "@nebutra/cache";

// Acquire lock
const lock = await lockCache.acquire("process:order:456", {
  ttl: 30000, // 30 seconds
});

try {
  await processOrder(456);
} finally {
  await lock.release();
}

Stampede Protection

import { stampedeCache } from "@nebutra/cache";

// Prevents multiple simultaneous cache rebuilds
const data = await stampedeCache.getOrSet(
  "expensive:query",
  async () => await expensiveQuery(),
  { ttl: 3600, lockTtl: 5000 },
);

Lazy Refresh

import { lazyRefresh } from "@nebutra/cache";

// Refresh in background before expiry
const data = await lazyRefresh.getOrSet(
  "api:data",
  async () => await fetchFromApi(),
  { ttl: 3600, refreshAt: 0.8 }, // Refresh at 80% of TTL
);

Multi-tenancy

All cache keys are automatically prefixed with tenant ID:

// Internally becomes: "tenant:org_123:user:456"
await ttlCache.get("user:456", { tenantId: "org_123" });

Related

License

MIT

About

Cache provider contracts and runtime helpers for Nebutra applications.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors