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

cluster-shared-memory

v1.1.5

Published

Shared memory for the Node.js cluster module.

Downloads

355

Readme

cluster-shared-memory

npm node-current GitHub repo size

Cross-process storage acts like shared memory for Node.js applications which use the cluster module.

If you are looking for a tool to share the physical memory, cluster-shared-memory can not meet your needs. You can only use it to share data between processes.

It provides in-memory storage managed by the master process, and the workers communicate with the master through IPC. It's basically used in the Node.js cluster applications to share data between processes.

It supports reading and writing objects in shared memory storage, mutually exclusive access between processes, listening objects in shared memory storage, and an LRU cache.

Usage

const cluster = require('cluster');
require('cluster-shared-memory');

if (cluster.isMaster) {
  for (let i = 0; i < 2; i++) {
    cluster.fork();
  }
} else {
  const sharedMemoryController = require('cluster-shared-memory');
  // Note: it must be a serializable object
  const obj = {
    name: 'Tom',
    age: 10,
  };
  // Set an object
  await sharedMemoryController.set('myObj', obj);
  // Get an object
  const myObj = await sharedMemoryController.get('myObj');
  // Mutually exclusive access
  await sharedMemoryController.mutex('myObj', async () => {
    const newObj = await sharedMemoryController.get('myObj');
    newObj.age = newObj.age + 1;
    await sharedMemoryController.set('myObj', newObj);
  });
}

API

setLRUOptions(options)

Set the options of the LRU cache. Only available on the master process.

Note that this will recreate a new LRU cache.

  • options {Object} The same with the options of lru-cache.
    • default : { max: 10000, maxAge: 1000 * 60 * 5 }.
  • returnValue {void}

set(key, value, [callback])

Set an object to the shared memory storage.

  • key {String} The key used to find the object.
  • value {any} The object to set. Note: it must be a serializable object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • result {String}: 'OK' if success.
  • returnValue {Promise<String> | void} This function is an async function. It will return a Promise if there's no callback function.

get(key, [callback])

Get an object from the shared memory storage.

  • key {String} The key used to find the object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • value {any}: The Object to get.
  • returnValue {Promise<any> | void} This function is an async function. It will return a Promise if there's no callback function.

remove(key, [callback])

Remove an object from the shared memory storage.

  • key {String} The key used to find the object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • result {String}: 'OK' if success.
  • returnValue {Promise<String> | void} This function is an async function. It will return a Promise if there's no callback function.

getLock(key, [callback])

Get the lock of an object. If you want to perform mutually exclusive operations, you must get the lock first. If the lock is already get by another process, this operation will be blocked until the lock has been returned.

Remember to release the lock after you finishing the operations!

  • key {String} The key used to find the object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • lockId {String}: The ID of the lock.
  • returnValue {Promise<String> | void} This function is an async function. It will return a Promise if there's no callback function.

releaseLock(key, lockId, [callback])

Release the lock of an object. After releasing the lock, one of other blocked requests can get the lock.

  • key {String} The key used to find the object.
  • lockId {String} The ID of the lock.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • result {String}: 'OK' if success.
  • returnValue {Promise<String> | void} This function is an async function. It will return a Promise if there's no callback function.

mutex(key, func)

Auto get and release the Lock of an object.

  • key {String} The key used to find the object.
  • func {Function} The async function to be called after getting the lock. It will hold the lock before the function is finished.
  • returnValue {Promise<any>} This function is an async function. It will return a Promise, which the resolved value is the same as func.

listen(key, callback)

Listen an object.

  • key {String} The key used to find the object.
  • callback {Function} The function to be called after the value of the object is changed. Callback arguments:
    • value {any}: The new value of the Object.
  • returnValue {void}

setLRU(key, value, [callback])

Set an object to the LRU cache.

  • key {String} The key used to find the object.
  • value {any} The object to set. Note: it must be a serializable object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • result {String}: 'OK' if success.
  • returnValue {Promise<String> | void} This function is an async function. It will return a Promise if there's no callback function.

getLRU(key, [callback])

Get an object from the LRU cache.

  • key {String} The key used to find the object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • value {any}: The Object to get.
  • returnValue {Promise<any> | void} This function is an async function. It will return a Promise if there's no callback function.

removeLRU(key, [callback])

Remove an object from the LRU cache.

  • key {String} The key used to find the object.
  • callback {Function} (optional) The function to be called after successful operations. Callback arguments:
    • result {String}: 'OK' if success.
  • returnValue {Promise<String> | void} This function is an async function. It will return a Promise if there's no callback function.