Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Shared memory for node processes

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 bin
Octocat-spinner-32 examples
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 package.json
Octocat-spinner-32 perfectapi.json
Octocat-spinner-32 readme.md
readme.md

What is this?

This is an implementation of shared memory, for use by multiple node processes that need to share data. It is a common API, independent of the actual means of storing and sharing the memory. It does not use any low-level OS shared memory capabilities, and is compatible with both Windows and Linux.

Why use this?

You have a node process that you want to scale across CPUs or across servers, but you need to share some data between them. You could use Redis or MongoDB or memcached, but this is much simpler.

Is this a database?

No, although in upcoming forks of this project, a database may be used as a means of storing and sharing the memory.

Usage

In this implementation, you would run node-sharedmem as a service. You can do so from the command line like:

$ sharedmem server -p 3000

(will run on port 3000). See also options for installing as a service or daemon on Windows or Linux: https://github.com/perfectapi/node-perfectapi/wiki/Installers.

Once you have the service running on an endpoint, you can use it in this pattern (you will need to have perfectapi module installed; npm install perfectapi):

var perfectapi = require('perfectapi');
perfectapi.proxy('http://localhost:3000/sharedmem', function(err, sharedmem) {

    var myuser = {email: 'steve@perfectapi.com', last:'campbell', first:'steve'};
    var config = {collection: 'users', 'key': myuser.email, 'variable': myuser};
    sharedmem.save(config, function(err, result) {
        //myuser is saved.  Lets check:

        config = {collection: 'users', 'key': 'steve@perfectapi.com'};
        sharedmem.get(config, function(err, result) {
            //result == myuser
        })
    });

});

You can also access an API test page at http://localhost:3000/sharedmem/testapp (replace localhost with your actual server name).

Available methods (API)

I have put up a public test page - it has all the commands and their descriptions. You can also play around with the library there - just be aware that others may be doing the same.

Why are the methods asynchronous?

I agree, it is not intuitive to access memory in an asynchronous manner. On Linux you can use node-fiberize to switch to synchronous calls.

I could build fiber support in, but unfortunately node-fibers is not currently available for Windows, (and I want to retain compatibility with Windows).

Something went wrong with that request. Please try again.