npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pdz/gc

v1.1.0

Published

Global cache object for all processes in cluster

Downloads

5

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