Pythagorean Cache
![Coverage Status](https://coveralls.io/repos/github/shaunburdick/pythagorean-cache/badge.svg?branch=master)
🍷A cache that dumps when full or at an interval
![cup](https://upload.wikimedia.org/wikipedia/commons/a/a9/Physagorian_Pythagoras_Greedy_Tantalus_cup_05.svg)
Example
import { PythagoreanCache } from 'pythagorean-cache';
const cache = new PythagoreanCache<string>({ size: 10 });
cache.on('dump', () => {
});
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.