Shared memory for the Node.js cluster module.
npm install cluster-shared-memoryCross-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.
javascript
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
$3
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 }`.- 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\
It will return a Promise if there's no callback function.
- 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\
It will return a Promise if there's no callback function.
- 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\
It will return a Promise if there's no callback function.
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\
It will return a Promise if there's no callback function.
- 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\
It will return a Promise if there's no callback function.
- 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\
It will return a Promise, which the resolved value is the same as func.
- 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}
- 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\
It will return a Promise if there's no callback function.
- 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\
It will return a Promise if there's no callback function.
- 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\
It will return a Promise if there's no callback function.