@pdz/gc
v1.1.0
Published
Global cache object for all processes in cluster
Downloads
3
Readme
Global cache object for all processes in cluster
Example:
'use strict';
const cluster = require('cluster');
const pdzGC = require('@pdz/gc');
const log = (...args) => {
console.log.apply(null, [Date.now()].concat(args));
}
if(cluster.isMaster) {
// Is master process
const xlog = log.bind(null, 'MASTER', process.pid);
xlog('Start');
const gc = pdzGC.create('main').on('error', (e) => xlog('ERROR', e)); // Use cache namespace 'main'
gc.on('notice', (message, bySelf) => {
xlog('Notice:', message, bySelf);
}).on('updated', (bySelf, type) => {
xlog(gc.data, bySelf, type); // gc.data always similar in all processes
// Don't manipulate with gc.data directly and not getting data from it.
// Use gc.data only for read current cache dump
}).start();
cluster.fork({GC_ADD: 1, GC_TS: 100}); // Run another process 1
cluster.fork({GC_ADD: 1, GC_TS: 111}); // Run another process 2
setInterval(() => {
if(!Object.keys(cluster.workers).length) return process.exit();
gc.set({'a.b': (gc.get('a.b') || 0) + 1});
// Manipulate cache data:
// gc.set({keypath: value_to_set, ...});
// gc.assign({keypath: object_to_assign, ...});
// gc.del(keypath | [keypath, ...]);
// Getting data:
// gc.get(keypath);
}, 333);
} else {
// Is another process
const xlog = log.bind(null, 'WORKER', process.pid);
xlog('Start');
const gc = pdzGC.create('main').on('error', (e) => xlog('ERROR', e)); // Use cache namespace 'main'
gc.on('notice', (message, bySelf) => {
xlog('Notice:', message, bySelf);
}).on('updated', (bySelf, type) => {
xlog(gc.data, bySelf, type); // gc.data always similar in all processes
// Don't manipulate with gc.data directly and not getting data from it.
// Use gc.data only for read current cache dump
}).start();
const add = parseInt(process.env.GC_ADD);
let n = 0;
setInterval(() => {
if(n >= add * 5) return process.exit();
gc.set({c: (gc.get('c') || 0) + add}); // See master process code for comments
gc.notice(gc.get('c')); // Send notice to all processes
n += add;
}, parseInt(process.env.GC_TS));
}
Result:
1636060448814 MASTER 10299 Start
1636060448888 WORKER 10307 Start
1636060448889 WORKER 10306 Start
1636060448994 WORKER 10306 { c: 1 } true set
1636060448996 WORKER 10306 Notice: 1 true
1636060448997 MASTER 10299 { c: 1 } false set
1636060448998 MASTER 10299 Notice: 1 false
1636060448999 WORKER 10307 { c: 1 } false set
1636060449003 WORKER 10307 Notice: 1 false
1636060449006 WORKER 10307 { c: 2 } true set
1636060449008 WORKER 10307 Notice: 2 true
1636060449008 MASTER 10299 { c: 2 } false set
1636060449008 MASTER 10299 Notice: 2 false
1636060449009 WORKER 10306 { c: 2 } false set
1636060449009 WORKER 10306 Notice: 2 false
1636060449096 WORKER 10306 { c: 3 } true set
1636060449096 MASTER 10299 { c: 3 } false set
1636060449097 WORKER 10307 { c: 3 } false set
1636060449097 MASTER 10299 Notice: 3 false
1636060449097 WORKER 10306 Notice: 3 true
1636060449097 WORKER 10307 Notice: 3 false
1636060449118 WORKER 10307 { c: 4 } true set
1636060449118 WORKER 10307 Notice: 4 true
1636060449118 MASTER 10299 { c: 4 } false set
1636060449119 MASTER 10299 Notice: 4 false
1636060449119 WORKER 10306 { c: 4 } false set
1636060449119 WORKER 10306 Notice: 4 false
1636060449160 MASTER 10299 { c: 4, a: { b: 1 } } true set
1636060449160 WORKER 10307 { c: 4, a: { b: 1 } } false set
1636060449160 WORKER 10306 { c: 4, a: { b: 1 } } false set
1636060449196 WORKER 10306 { c: 5, a: { b: 1 } } true set
1636060449197 WORKER 10306 Notice: 5 true
1636060449197 MASTER 10299 { c: 5, a: { b: 1 } } false set
1636060449197 MASTER 10299 Notice: 5 false
1636060449197 WORKER 10307 { c: 5, a: { b: 1 } } false set
1636060449197 WORKER 10307 Notice: 5 false
1636060449229 WORKER 10307 { c: 6, a: { b: 1 } } true set
1636060449230 WORKER 10307 Notice: 6 true
1636060449230 MASTER 10299 { c: 6, a: { b: 1 } } false set
1636060449230 MASTER 10299 Notice: 6 false
1636060449230 WORKER 10306 { c: 6, a: { b: 1 } } false set
1636060449231 WORKER 10306 Notice: 6 false
1636060449298 WORKER 10306 { c: 7, a: { b: 1 } } true set
1636060449298 WORKER 10306 Notice: 7 true
1636060449299 MASTER 10299 { c: 7, a: { b: 1 } } false set
1636060449299 MASTER 10299 Notice: 7 false
1636060449299 WORKER 10307 { c: 7, a: { b: 1 } } false set
1636060449300 WORKER 10307 Notice: 7 false
1636060449342 WORKER 10307 { c: 8, a: { b: 1 } } true set
1636060449342 WORKER 10307 Notice: 8 true
1636060449342 MASTER 10299 { c: 8, a: { b: 1 } } false set
1636060449343 MASTER 10299 Notice: 8 false
1636060449343 WORKER 10306 { c: 8, a: { b: 1 } } false set
1636060449343 WORKER 10306 Notice: 8 false
1636060449398 WORKER 10306 { c: 9, a: { b: 1 } } true set
1636060449398 WORKER 10306 Notice: 9 true
1636060449398 MASTER 10299 { c: 9, a: { b: 1 } } false set
1636060449399 MASTER 10299 Notice: 9 false
1636060449399 WORKER 10307 { c: 9, a: { b: 1 } } false set
1636060449399 WORKER 10307 Notice: 9 false
1636060449454 WORKER 10307 { c: 10, a: { b: 1 } } true set
1636060449455 WORKER 10307 Notice: 10 true
1636060449455 MASTER 10299 { c: 10, a: { b: 1 } } false set
1636060449455 MASTER 10299 Notice: 10 false
1636060449455 WORKER 10306 { c: 10, a: { b: 1 } } false set
1636060449455 WORKER 10306 Notice: 10 false
1636060449493 MASTER 10299 { c: 10, a: { b: 2 } } true set
1636060449493 WORKER 10306 { c: 10, a: { b: 2 } } false set
1636060449494 WORKER 10307 { c: 10, a: { b: 2 } } false set