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

simple-cluster-cache

v0.0.6

Published

simple cluster object cache

Downloads

16

Readme

simple-cluster-cache Build Status NPM version

Dead simple cluster based in-memory cache that allows the master process and the worker processes to share the same object cache.

Install

$ npm install simple-cluster-cache

Methods

  • get(key, callback) - Gets the value from cache. Returns undefined if the value does not exist.
    • key: The unique cache object key or an array of keys.
    • callback: The optional callback function.
  • set(key, value, ttl, callback) - Sets a cache object for the specified key.
    • key: The unique cache object key.
    • value: The cache object value.
    • ttl: The optional time to live on the cache object. (milliseconds)
    • callback: The optional callback function.
  • del(key, callback) - Deletes a key from cache.
    • key: The unique cache object key or an array of keys.
    • callback: The optional callback function.
  • clear(callback) - Clears the cache.
    • callback: The optional callback function.

Usage

cache.set('hello', 'world', function(err, value) {
  console.log(value);
});

cache.set('world', 'hello', function(err, value) {
  console.log(value);
});

// single get
cache.get('hello', function(err, value) {
  // prints 'world'
  console.log(value);
});

// multi get
cache.get(['hello', 'world'], function(err, value) {
  // prints '{ 'hello': 'world', 'world': 'hello' }'
  console.log(value);
});

// single delete
cache.del('hello', function(err) {
  console.log('deleted');  
});

// multi delete
cache.del(['hello', 'world'], function(err) {
  console.log('deleted');  
});

// clears the entire cache
cache.clear(function() {
  console.log('cache cleared!');
});

Example

'use strict';

var cluster = require('cluster');
var cache = require('simple-cluster-cache');
var express = require('express');
var os = require('os');

if (cluster.isMaster) {

  var cpus = os.cpus().length;
  for (var i = 0; i < cpus; i++) {
    cluster.fork();
  }

  cluster.on('exit', function(worker, code, signal) {
    cluster.fork();
  });

  // preload some keys just to demonstrate it works from the master thread
  cache.clear();
  cache.set('hello', 'world', 5000);
  cache.set('object', { 'hello': 'world' });
  cache.set('array', [ 1, 2, 3 ]);
  cache.set('callback', true, function(err, value) {
    console.log('successfully set "callback" to %s', value);
  });
}
else {
  var app = express();

  // gets the key from cache
  app.get('/:key', function(req, res, next) {
    var key = req.params.key;
    cache.get(key, function(err, value) {
      res.json(value);
    });
  });

  // sets the cache value
  app.post('/:key/:value/:ttl?', function(req, res, next) {
    var key = req.params.key;
    var value = req.params.value;
    var ttl = req.params.ttl;

    cache.set(key, value, ttl, function(err, value) {
      res.json(value);
    });
  });

  // deletes the cache key
  app.delete('/:key', function(req, res, next) {
    var key = req.params.key;
    
    cache.del(key, function(err) {
      res.json(key + ' was deleted.');
    });
  });

  // clears the cache
  app.purge('/clear', function(req, res, next) {
    console.log('clear')
    cache.clear(function() {
      res.json('cache cleared!');
    });
  });

  app.listen(process.env.PORT || 8000);
}