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

shmdb

v0.3.0

Published

A key/value embedded database aimed at resolving the problem of sharing memory among multi-process.

Downloads

13

Readme

node-shmdb

why

Shareing the data between parent process and child process,you can servel ways. First,use variable.

var cluster = require('cluster'); 
var a = 1;
if (cluster.isMaster) {	
	cluster.fork();  
} else if (cluster.isWorker) {
	console.log('a is ',a);
}

Varivale a will get the value 1 in child process.But when you assign the value of a in one of process, the other process can not get the new value.In other words, it suit for readonly varivale.

Second,using event message to notify the other process.

var cluster = require('cluster'); 

if (cluster.isMaster) {	
	var worker = cluster.fork();  
	worker.send('a message form parent');
	worker.on('message', function(msg) {//message send by child
		console.log('get a message,its value:',msg);
	});
} else if (cluster.isWorker) {
	cluster.worker.send(1);
	process.on('message',function(msg) {
		console.log(msg);
	});
}  

In node,you can use the method of send to notify other process some value has changed.It suit for one process write and the other read. But while both of two process read and read the same data,how can we deal with it? As we all known,there is no mechanism of lock in node,how to pervent from reading dirty data? node-shmdb is just aim at resolving the issue.

theory

node-shmdb is an implementation of shmdb, which is based on os's shared memory.shmdb is a key-value type of database, unlike memcache or redis, it is not a database that connected via network,but it is embeded.For sharing data between processes, it use operation system's shared memeory.

install

npm install shmdb

example

var shmdb = require('shmdb');
var cluster = require('cluster');
var obj = null;

if (cluster.isMaster) {
	console.info('=====================I am master=====================');
	obj = shmdb(10);
	if (obj.rv == 0) {
		console.log(obj);
		obj.put('some','value1');
		
		cluster.fork({shmid:obj.shmid,semid:obj.semid});  
		setTimeout(function() {
			console.log('get new value changed in child process.',obj.get('some'));
			console.log('put value to xx in parent process.',obj.put('xx','parent'));
		},2000);
	} else {
		console.error('get shared memery error:0x[%x]',obj.rv);
	}
	
} else if (cluster.isWorker) {
	console.info('=====================I am child=====================');
	var shmid = parseInt(process.env.shmid);
	var semid = parseInt(process.env.semid);
	if (shmid && semid) {
		obj = shmdb({shmid:shmid,semid:semid});
		console.log('get some in child:',obj.get('some'));
		console.log('put result in child',obj.put('some','other'));
		setTimeout(function() {
			console.log('get xx form parent',obj.get('xx'));
		},4000); 
	}
}

API

shmdb(param)

description
Initializing shmdb in parent process or child process.It return an instance of Class Shmdb.

parameter

  • {Number|Object} param Using Number in parent process,it will create the shared memery in operation system,and return the two variable,shmid and semid,which will be used in child process.Using Object in child process,and its format should be {shmid:Number,semid:Number},the shmid and semid can be obtained from parent process.

return

  • {Shmdb} its prototies are as follows:

      {rv:Number,shmid:Number,semid:Number}

    When success,the rv will be zero,and shmid and semid will be assigned.On the other hand,the rv will be none-zero.

Shmdb.prototype.put(key,value)

description

Put a pair of key/value into shmdb.

parameter

  • key {String} the key
  • value {String} the value

return

  • {Object} {code:Number} when success the code will be zero.

Shmdb.prototype.get(key)

decription

Get value by key.

parameter

  • key {String} the key

return

  • {Object} {code:Number,value:String} when success the code will be zero.

Shmdb.prototype.remove(key)

description

Remove an pair of key/value by key.

parameter

  • key {String} the key

return

{Object} {code:Number,value:String} when success the code will be zero.

Shmdb.prototype.destroy()

description

Destory the shared memery allocated when you call the function shmdb.When process exit,this function will be called auto.

parameter

  • void

return

  • {Object} {code:Number} when success the code will be zero.

contributors

yunnysunny (maintainer)

license

Apache License, Version 2.0