0.1.5 • Published 8 years ago
persistent-counter v0.1.5
PersistentCounter
A simple counter with persistent storage
Installation
$ npm install persistent-counter
Usage
Create a persistent counter, and optionally provide a writeInterval
in
milliseconds. Increment at will, and the incremented value will be automatically
flushed to disk every writeInterval
milliseconds.
If your program exits without calling flush()
, any increments since the
last timer-triggered write will be lost.
The current count can be accessed by the count
property.
Example
let PersistentCounter = require('persistent-counter');
let pc = new PersistentCounter({
filename: 'path/to/count.json',
writeInterval: 5 * 60 * 1000
});
pc.increment();
console.log(pc.count);
pc.flush();
Caveats
This is a lightweight solution for a single process. I haven't tested it with multiple processes on the same file, and I expect that it wouldn't work right, and could possibly corrupt data.