Skip to content

recap/persistent-cache-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

persistent-cache-object

A simple JSON auto persistent object using files.

example 1

const PersistentObject = require('persistent-cache-object');
const map = new PersistentObject('./map.db');
map['key'] = 'value';
map.close();

example 2

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();

example 3

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);

example 4

Initialize persistent object with and existing object.

const PersistentObject = require('persistent-cache-object');
const map = new PersistentObject('./map.db', {'key':'value'});

example 5

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();
});

About

Simple node file persistent JSON object

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published