A simple JSON auto persistent object using files.
const PersistentObject = require('persistent-cache-object');
const map = new PersistentObject('./map.db');
map['key'] = 'value';
map.close();
By default the persistent object will update the file every 5 seconds only if the object has changed since. This can be controlled or disabled when
initializing the Object. flush()
must be used to manually write to file.
const PersistentObject = require('persistent-cache-object');
const map = new PersistentObject('./map.db', null, {'disableInterval': true});
map['key'] = 'value';
map.flush();
map['key2'] = 'value2';
map.close();
The file update interval timer can also be set on initialization. This example sets the interval to 10s and disabling it later.
const PersistentObject = require('persistent-cache-object');
const map = new PersistentObject('./map.db', null, {interval: 10000});
map['key'] = 'value';
map.setInterval(-1);
Initialize persistent object with and existing object.
const PersistentObject = require('persistent-cache-object');
const map = new PersistentObject('./map.db', {'key':'value'});
Using locks to allow multiple node processes access the same object. N.B to be used with caution.
const PersistentObject = require('../src/persistent-cache-object');
const map = new PersistentObject('./map.db', null, {'disableInterval': true});
map.lock(() => {
map.reload();
map['aKey'] = 'aValue';
map.flush();
map.unlock();
});