# cachetrax

> Caching Money.

Latest version **1.0.6** (published 2026-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install cachetrax
pnpm add cachetrax
yarn add cachetrax
bun add cachetrax
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2026-02-27 |
| First published | 2018-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 56.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Steven Velozo |
| Maintainers | stevenvelozo |
| Keywords | cache |

## Links

- npm: https://www.npmjs.com/package/cachetrax
- Repository: https://github.com/stevenvelozo/cachetrax
- Issues: https://github.com/stevenvelozo/cachetrax/issues
- npm.io page: https://npm.io/package/cachetrax

## Dependencies (1)

- [fable-serviceproviderbase](https://npm.io/package/fable-serviceproviderbase.md) ^3.0.19

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

- 1.0.6 (latest) — 2026-02-27
- 1.0.5 — 2026-02-18
- 1.0.4 — 2023-06-03
- 1.0.3 — 2023-05-24
- 1.0.2 — 2023-05-22
- 1.0.1 — 2023-05-05
- 1.0.0 — 2018-08-22

## README

# CacheTrax

> A hash-indexed object cache with time and size based expiration

CacheTrax is a lightweight in-memory cache that combines a hash map for O(1) key lookups with a double linked list for ordered eviction. It supports automatic size-based pruning on insert, time-based expiration, and custom pruning functions for application-specific cache management.

## Features

- **O(1) Hash Lookups** -- cached entries are indexed by unique hash keys for constant-time reads
- **Double Linked List Ordering** -- entries maintain insertion order for predictable FIFO eviction
- **Size-Based Pruning** -- set `maxLength` and the cache automatically evicts the oldest entry on insert
- **Time-Based Expiration** -- set `maxAge` in milliseconds to expire stale entries on prune
- **Touch to Refresh** -- reinvigorate an entry's timestamp to prevent it from aging out
- **Custom Pruning** -- pass a function to `pruneCustom` for application-specific eviction logic
- **Fable Service** -- extends `fable-serviceproviderbase` and registers as an `ObjectCache` service
- **Browser Compatible** -- includes a browser shim that attaches to `window.CacheTrax`

## Quick Start

```javascript
const libCacheTrax = require('cachetrax');

// Create a cache with a size limit
let cache = new libCacheTrax();
cache.maxLength = 100;
cache.maxAge = 300000; // 5 minutes

// Store entries by hash
cache.put({ name: 'Alice', role: 'admin' }, 'user-alice');
cache.put({ name: 'Bob', role: 'editor' }, 'user-bob');

// Read by hash (returns false on miss)
cache.read('user-alice');  // { name: 'Alice', role: 'admin' }
cache.read('user-carol');  // false

// Refresh an entry to prevent expiration
cache.touch('user-alice');

// Manually remove an entry
cache.expire('user-bob');

// Run all pruning rules (age then length)
cache.prune((pRemovedRecords) =>
{
    console.log(`Pruned ${pRemovedRecords.length} entries`);
});
```

## Installation

```bash
npm install cachetrax
```

## How It Works

CacheTrax maintains two parallel data structures:

- A **hash map** (`_HashMap`) for O(1) lookups by key
- A **double linked list** (`_List`) for maintaining insertion order

New entries are appended to the tail of the list. When the cache exceeds `maxLength`, the head (oldest entry) is popped off. Time-based pruning walks the hash map and expires any entry whose `Created` timestamp is older than `maxAge` milliseconds.

```
put(data, hash)   → append to list tail, index in hash map
read(hash)         → hash map lookup, return datum or false
touch(hash)        → remove + re-insert at tail with fresh timestamp
expire(hash)       → remove from list and hash map
prune()            → expire by age, then pop by length
```

## Configuration

| Property | Type | Default | Description |
|----------|------|---------|-------------|
| `maxLength` | Number | `0` | Maximum cache size. `0` means unlimited. |
| `maxAge` | Number | `0` | Maximum entry age in milliseconds. `0` means no expiration. |

## API

| Method | Description |
|--------|-------------|
| `put(pData, pHash)` | Add or update a cache entry. Returns the stored datum. |
| `read(pHash)` | Retrieve a datum by hash. Returns `false` on miss. |
| `touch(pHash)` | Refresh timestamp and move to tail. Returns datum or `false`. |
| `expire(pHash)` | Remove an entry. Returns the removed node or `false`. |
| `getNode(pHash)` | Get the full node object including metadata. Returns `false` on miss. |
| `prune(fComplete)` | Run age then length pruning. Callback receives removed records array. |
| `pruneBasedOnExpiration(fComplete)` | Expire entries older than `maxAge`. |
| `pruneBasedOnLength(fComplete)` | Pop entries until at or below `maxLength`. |
| `pruneCustom(fComplete, fPruneFunction)` | Expire entries where the function returns `true`. |

## Documentation

Detailed documentation is available in the `docs/` folder:

| Document | Description |
|----------|-------------|
| [Architecture](docs/architecture.md) | How the hash map and linked list work together |
| [Cache Operations](docs/operations.md) | Full guide to `put`, `read`, `touch`, `expire` and `getNode` |
| [Pruning Strategies](docs/pruning.md) | Size-based, time-based and custom pruning |

## Testing

```bash
npm test
npm run coverage
```

## Related Packages

- [fable](https://github.com/stevenvelozo/fable) - Service dependency injection framework
- [fable-serviceproviderbase](https://github.com/stevenvelozo/fable-serviceproviderbase) - Base class for Fable services

## License

MIT

## Contributing

Pull requests are welcome. For details on our code of conduct, contribution process, and testing requirements, see the [Retold Contributing Guide](https://github.com/stevenvelozo/retold/blob/main/docs/contributing.md).

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