@openstd/cachekit
v0.1.0
Published
Functional and lightweight TypeScript cache utility
Downloads
13
Maintainers
Readme
CacheKit TypeScript Client
A lightweight, functional TypeScript cache utility package. Built with performance and simplicity in mind.
Features
- TTL (Time To Live): Automatic expiration of cache entries.
- LRU (Least Recently Used): Efficient eviction strategy for memory management.
- In-Memory Storage: Fast and reliable local caching.
- TypeScript First: Full type safety and modern ESM support.
- Bun Optimized: Native support for Bun's high-performance runtime.
Installation
bun add @openstd/cachekitUsage
Simple Memory Cache
import { MemoryCache } from "@openstd/cachekit";
const cache = new MemoryCache();
// Set with TTL (5 seconds)
await cache.set("user:1", { name: "Alice" }, { ttl: 5000 });
// Get value
const user = await cache.get("user:1");
console.log(user); // { name: "Alice" }
// Wait for expiration
await new Promise(resolve => setTimeout(resolve, 6000));
console.log(await cache.get("user:1")); // undefinedLRU Cache
import { LRUCache } from "cachekit";
// Max capacity of 2 items
const cache = new LRUCache(2);
await cache.set("a", 1);
await cache.set("b", 2);
await cache.set("c", 3); // "a" is evicted
console.log(await cache.get("a")); // undefined
console.log(await cache.get("b")); // 2
console.log(await cache.get("c")); // 3API
Cache Interface
All cache implementations follow the same interface:
get<T>(key: string): Promise<T | undefined>set<T>(key: string, value: T, options?: CacheOptions): Promise<void>delete(key: string): Promise<boolean>clear(): Promise<void>size(): Promise<number>has(key: string): Promise<boolean>
License
Apache-2.0
