npm.io
1.0.9 • Published 7 years ago

inter-cluster-cache

Licence
MIT
Version
1.0.9
Deps
3
Size
13 kB
Vulns
24
Weekly
0
DeprecatedThis package is deprecated

Inter Cluster Cache

Build Status

Centralized cache for worker clusters in NodeJS, which doesn't involve Redis or the file system.

Installation

$ npm install --save inter-cluster-cache

or

$ yarn add inter-cluster-cache

Why do I need this?

Suppose you have a small Node.js project which requires you to use clusters, with several worker processes. Each worker process is basically its own instance of the application. This results in sharing a common data source between the master process and workers quite difficult, as it requires utilizing a lot of event emitters.

You could ofcourse use redis, but what if your application is small enough to not require setting up a redis environment.

This npm module will provide you with a centralized cache that can be accessed between your master and all your worker clusters!

Usage

Cache API
Method Parameters Description
create n/a Creates the cache, must be used before using CacheHandler object.
close n/a Closes the cache, must be used before killing master process.
CacheHandler API
Method Parameters Description Resolved promise returns
insert key, value Insert a key / value pair into the cache, the key has to be unique. nothing
set key Set a value for an already existing key. nothing
get key Set a value for an already existing key. value associated with key
delete key Delete a key / value pair from the cache. value associated with key deleted
clear n/a Clear the entire cache. nothing
getCache n/a Retrieve the JavaScript object representing the cache. JavaScript object
Usage

Simple Example:

const { Cache, CacheHandler } = require('inter-cluster-cache');

(async () => {
    const cache = new Cache();
    cache.create();
    const cacheHandler = new CacheHandler();
    
    await cacheHandler.insert(1, 1);

    console.log(await cacheHandler.get(1)); // outputs - 1

    await cacheHandler.set(1, 99);

    console.log(await cacheHandler.get(1)); // outputs - 99

    console.log(await cacheHandler.getCache()); // outputs - { '1': 99 }
    
    await cacheHandler.clear();

    console.log(await cacheHandler.getCache()); // outputs - {}

    cache.close();
})();

Example with clusters:

const { Cache, CacheHandler } = require('inter-cluster-cache');
const cluster = require('cluster');

// In master process.
if (cluster.isMaster) {
    // Define a port number of your choice, default is 3137.
    const port = 3137;

    // Always construct the cache object in master cluster.
    const interClusterCache = new Cache(port);
    const masterCacheHandler = new CacheHandler();
    
    // Make sure to always create the cache or else it will not work.
    interClusterCache.create();
    
    const numWorkers = 3;
    let deathCounter = 0;
    
    for (let i = 0; i < numWorkers; ++i) {
        // Spawn the worker processes.
        const worker = cluster.fork();
    
        worker.on('exit', () => {
            // Upon death of a worker get the cache.
            masterCacheHandler.getCache()
            .then(cache => {
                console.log(cache);
                if (++deathCounter === numWorkers) {
                    // Always close the cache before killing master process.
                    interClusterCache.close();
                    process.exit();
                }
            })
            .catch(err => {
                console.log(err);
                if (++deathCounter === numWorkers) {
                    // Always close the cache before killing master process.
                    interClusterCache.close();
                    process.exit();
                }
            });
        });
    }
// In worker process.
} else {
    const workerId = cluster.worker.id;
    const workerCacheHandler = new CacheHandler();

    // Insert into the key value pair into the cache and then kill the worker.
    workerCacheHandler.insert(workerId, workerId)
    .then(() => process.exit())
    .catch(err => process.exit());
}
Todos
  • Write MORE Tests
  • Exporting cache to JSON
License
  • MIT

Keywords