tekir
All Packages
Databasev0.1.10

@tekir/cache

Multi-driver cache: memory, Redis, and database stores, plus an HTTP response cache middleware.

Installation

$bun add @tekir/cache

Features

  • Multi-driver: memory, Redis, database
  • get/set/has/delete/flush API
  • getOrSet with lazy loading factory
  • pull() to get and delete in one call
  • TTL support with configurable defaults
  • Store switching at runtime via store()
  • HTTP response cache middleware with ETag and 304 support

Quick Example

TypeScript
import { Cache, cache, MemoryCacheStore } from '@tekir/cache'

const store = new Cache({ stores: { memory: new MemoryCacheStore() } })

router.get('/api/posts',
  cache({ store, ttl: 60 }),
  async () => Post.all())

Changelog

v0.1.10LatestSeptember 16, 2026
  • HTTP caching now preserves binary bodies and response status, isolates host and credential variants, and refuses private, no-store, error, empty, and cookie-setting responses.
  • Published output now uses the shared Node-targeted ESM bundle pipeline with external dependencies and generated TypeScript declarations, while Bun consumers keep the native source export.
v0.1.9July 23, 2026
  • Package metadata now follows the shared compatible 0.1.x dependency range used by this coordinated Tekir release.
v0.1.8June 13, 2026
  • The HTTP response cache is now secure by default. Requests carrying Authorization or Cookie skip the cache entirely (authenticated: 'bypass'). Set authenticated: 'vary' to include credential headers in the cache key, or 'allow' to opt back into the previous behavior; a custom key builder is always honored. Behavior change: authenticated GETs are no longer cached by default.
  • getOrSet now shares a single in-flight computation across concurrent misses for the same key (single-flight), so a cold key under load runs factory() once instead of once per request.
  • RedisCacheStore.flush() now deletes only this store's keys via SCAN+DEL over <prefix>* instead of FLUSHDB, and throws on an empty prefix (which would have wiped the whole database). set uses an atomic SET ... EX where the client supports it.
  • The memory store now enforces a maxEntries cap (default 10000, FIFO eviction) with periodic sweeping of expired entries, and both stores gained a manual prune().
  • Found and fixed with Fable.
v0.1.7May 8, 2026
  • Cache.get<T> and the per-store get<T> methods now default T to any instead of unknown. Callers no longer need a redundant generic argument or runtime type guard to read a value back out: await cache.get('user') returns a value you can use directly. Pass an explicit type (cache.get<User>('user')) when you want narrowing back.
v0.1.6April 29, 2026
  • RedisCacheStore now accepts any redis-like client without requiring a matching send() signature. The interface dropped send and connected fields so @tekir/redis, ioredis, and node-redis clients all type-check directly without casts.
v0.1.5April 29, 2026
  • RedisCacheStore now accepts both @tekir/redis (returns boolean) and node-redis / ioredis (return number) clients without a TypeScript cast. Existence checks normalise both shapes at the call site.
v0.1.4April 29, 2026
  • Memory and database cache setups now type-check cleanly without @tekir/redis installed. The redis store is loaded only when the redis driver is used.
v0.1.3April 29, 2026
  • Added cache() middleware that caches full HTTP responses by URL with TTL, conditional revalidation via If-None-Match, and Vary header support. Skips mutating methods, error responses, and no-store requests by default.
  • Added setDefaultCacheStore() so CacheProvider auto-wires the middleware. Routes can use cache({ ttl: 60 }) once the provider is registered, no store option needed.
v0.1.2April 27, 2026
  • Split CacheConfig (loose, what config/cache.ts exports) from a new CacheManagerOptions (strict, what the Cache class accepts). The provider expands driver configs before instantiating, so direct new Cache({...}) callers and config-driven setups both type-check.
v0.1.1April 27, 2026
  • CacheConfig.stores now accepts driver-config objects ({ driver: 'memory' }) alongside CacheStore instances, so apps can declare stores in config/cache.ts without importing store classes.
v0.1.0April 1, 2026
  • Initial release

Other Database packages