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

1.0.0 • Public • Published

Pythagorean Cache

Build Status Coverage Status

🍷A cache that dumps when full or at an interval

cup

Example

import { PythagoreanCache } from 'pythagorean-cache';

// dump when size reaches 10
const cache = new PythagoreanCache<string>({ size: 10 });

cache.on('dump', () => {
  /* do something with 10 items */
});

for (let x = 0; x < 10; x++) {
  cache.push(`Hello ${x}`);
}

Options

  • size: The size of the cache. When the size is reached, a dump event is fired with all the items in the cache and then emptied
  • interval: The number of microseconds to wait before firing a dump event. This can be used to dump the cache at regular intervals.

These options can be combined, allowing you to create a cache that dumps at a certain size or at a certain interval.

Inspired Use Case

I was receiving a bursting stream of events I needed to add to a database. Instead of inserting each one as they come in, it was more efficient to do bulk inserts at intervals/sizes. This way that database was protected from burst events and I could expect the events to be inserted within a certain amount of time regardless.

Package Sidebar

Install

npm i pythagorean-cache

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

14.5 kB

Total Files

5

Last publish

Collaborators

  • shaunburdick