# fast-memory-cache

> Simple in-memory cache implementation

Latest version **3.0.0** (published 2025-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install fast-memory-cache
pnpm add fast-memory-cache
yarn add fast-memory-cache
bun add fast-memory-cache
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2025-04-02 |
| First published | 2014-11-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Konstantin Ikonnikov |
| Maintainers | mdevils |
| Keywords | memory, cache |

## Links

- npm: https://www.npmjs.com/package/fast-memory-cache
- Repository: https://github.com/mdevils/fast-memory-cache
- Homepage: https://github.com/mdevils/fast-memory-cache#readme
- Issues: https://github.com/mdevils/fast-memory-cache/issues
- Funding: https://github.com/sponsors/mdevils
- npm.io page: https://npm.io/package/fast-memory-cache

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2025-04-02
- 2.0.4 — 2016-01-18
- 2.0.3 — 2016-01-17
- 2.0.2 — 2015-11-19
- 2.0.1 — 2015-01-23
- 2.0.0 — 2015-01-23
- 1.0.0 — 2014-11-26

## README

# fast-memory-cache

![npm](https://img.shields.io/npm/v/fast-memory-cache)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/fast-memory-cache)
![npm](https://img.shields.io/npm/dm/fast-memory-cache)
![license](https://img.shields.io/npm/l/fast-memory-cache)

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

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

## Usage

### TypeScript

```ts
import MemoryCache from 'fast-memory-cache';

interface User {
  id: number;
  name: string;
}

const cache = new MemoryCache();

// Type-safe storage and retrieval
cache.set<User>('user', { id: 1, name: 'John' });
const user = cache.get<User>('user'); // User type

// With expiration
cache.set<string>('token', 'abc123', 300); // expires in 5 minutes
```

### JavaScript

```js
const MemoryCache = require('fast-memory-cache');

// Create a new cache instance
const cache = new MemoryCache();

// Store a value
cache.set('user', { id: 1, name: 'John' });

// Retrieve a value
const user = cache.get('user'); // { id: 1, name: 'John' }

// Store with expiration (in seconds)
cache.set('session', 'abc123', 60); // expires in 60 seconds

// Delete a value
cache.delete('user');

// Clear all cache
cache.clear();
```

## API

### Constructor

```ts
const cache = new MemoryCache();
```

### 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:

- ⭐ Starring the [repository](https://github.com/mdevils/fast-memory-cache)
- 🐛 Reporting [issues](https://github.com/mdevils/fast-memory-cache/issues)
- 🤝 Contributing with PRs

---
_Source: https://npm.io/package/fast-memory-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
