Skip to content

Repository files navigation

fast-memory-cache

npmnpm bundle sizenpmlicense

A lightweight, high-performance in-memory cache for Node.js applications with TypeScript support.

Features

  • 💨 Fast: Optimized for rapid access and storage
  • 🔑 Simple API: Intuitive methods for cache operations
  • ⏱️ TTL Support: Configurable time-to-live for cached items
  • 🧠 Memory Efficient: Minimal overhead for stored values
  • 📦 Zero Dependencies: No external packages required
  • 📝 TypeScript Support: Full type definitions included

Installation

npm install fast-memory-cache
# or
yarn add fast-memory-cache
# or
pnpm add fast-memory-cache

Usage

TypeScript

importMemoryCachefrom'fast-memory-cache';interfaceUser{id: number;name: string;}constcache=newMemoryCache();// Type-safe storage and retrievalcache.set<User>('user',{id: 1,name: 'John'});constuser=cache.get<User>('user');// User type// With expirationcache.set<string>('token','abc123',300);// expires in 5 minutes

JavaScript

constMemoryCache=require('fast-memory-cache');// Create a new cache instanceconstcache=newMemoryCache();// Store a valuecache.set('user',{id: 1,name: 'John'});// Retrieve a valueconstuser=cache.get('user');// { id: 1, name: 'John' }// Store with expiration (in seconds)cache.set('session','abc123',60);// expires in 60 seconds// Delete a valuecache.delete('user');// Clear all cachecache.clear();

API

Constructor

constcache=newMemoryCache();

Methods

get<T>(key: string): T | undefined

Retrieves a value from the cache.

  • Returns undefined if the key doesn't exist or has expired

set<T>(key: string, value: T, expireTime?: number): void

Stores a value in the cache.

  • key: The cache key
  • value: The value to store
  • expireTime: Time in seconds after which the value will be automatically deleted (optional)
    • Set to 0 or omit for values that never expire

delete(key: string): void

Removes a value from the cache.

clear(): void

Removes all values from the cache.

Performance

Fast Memory Cache is designed to be extremely lightweight and performant:

  • No serialization/deserialization overhead
  • Direct in-memory storage with no I/O operations
  • Efficient timeout management for expiration

When to Use

Fast Memory Cache is ideal for:

  • Caching API responses
  • Storing session data
  • Memoizing expensive function results
  • Reducing database load
  • Any scenario requiring fast, temporary data storage

License

MIT

Support the Project

If you find this package useful, consider:

About

Simple in-memory cache implementation

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages