rotating-cache
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

rotating-cache

Quick-and-dirty rotating cache for JS/TS. Caches a fixed amount of entries, and is pretty fast!

Installation

# Any of the following
npm install rotating-cache
yarn add rotating-cache
bun add rotating-cache

Usage

TypeScript

import createCache from 'rotating-cache';

const lookup = createCache<string, string>({
	computation: str => str.split('').reverse().join(''),
});

lookup('abc'); // 'cba', computed
lookup('bca'); // 'acb', computed
lookup('abc'); // 'cba', cached
lookup('bca'); // 'acb', cached

JavaScript (ESM)

import createCache from 'rotating-cache';

const lookup = createCache({
	computation: str => str.split('').reverse().join(''),
});

JavaScript (CJS)

const createCache = require('rotating-cache');

const lookup = createCache({
	computation: str => str.split('').reverse().join(''),
});

Caching

createCache accepts hash and cacheSize as props. hash will take the given value and calculate a unique identifier from it (defaults to String) while cacheSize is the total size of cached entries.

While caching, each entry is checked in two parallel caches. The older cache will be cycled out and replaced with the newer cache every time the newer cache is full.

Readme

Keywords

none

Package Sidebar

Install

npm i rotating-cache

Weekly Downloads

2

Version

1.0.0

License

none

Unpacked Size

3.86 kB

Total Files

6

Last publish

Collaborators

  • partman