logo

Memory Cache

A high-performance, in-memory caching library for TypeScript. TTL expiration, LRU eviction, and a powerful @cached decorator—all in a zero-dependency package.

  • Zero Dependencies
  • TypeScript
  • TTL + LRU
  • Decorator Support

Why Memory Cache

Simple, fast, and reliable caching for your TypeScript applications.

Lightning Fast

In-memory storage with O(1) lookups. No network latency, no disk I/O—just pure speed.

TTL Expiration

Set time-to-live for cache entries. Expired items are automatically cleaned up.

LRU Eviction

Smart eviction policy removes least recently used items when the cache reaches max size.

@cached Decorator

Automatic method-level caching with a simple decorator. No boilerplate required.

TypeScript First

Full type safety with generics. Your cached values are properly typed.

Zero Dependencies

Lightweight and self-contained. No bloat, no supply chain risks.

Quick Example

Get started in seconds with simple, intuitive APIs.

import { MemoryCache, cached } from '@humanspeak/memory-cache'

// Create a cache with 5 minute TTL and max 100 items
const cache = new MemoryCache<User>({
    ttl: 300000,
    maxSize: 100
})

// Simple get/set operations
cache.set('user:123', { name: 'Alice' })
const user = cache.get('user:123')

// Or use the @cached decorator
class UserService {
    @cached({ ttl: 60000 })
    async getUser(id: string) {
        return await fetchUserFromDb(id)
    }
}