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-node-cache

v0.1.5

Published

A cluster aware wrapper for node-cache

Downloads

587

Readme

cluster-node-cache

NPM version

NPM

Simple and fast NodeJS internal caching that works in a clustered environment.

This module is a wrapper for node-cache that allows it to work in a clustered environment - without it, you would have an instance of node-cache per core. It's API and functionality is mostly similar.

Note

This does not yet support the the 2.0 release of node-cache.

Install

bash npm install cluster-node-cache

Examples:

Initialize (INIT):

var cluster = require('cluster');
var myCache = require('cluster-node-cache')(cluster);

Options

  • stdTTL: (default: 0) the standard ttl as number in seconds for every generated cache element.
    0 = unlimited
  • checkperiod: (default: 600) The period in seconds, as a number, used for the automatic delete check interval.
    0 = no periodic check.
var cluster = require('cluster');
var myCache = require('cluster-node-cache')(cluster, {stdTTL: 100, checkperiod: 900});

Optional CLS support

var cluster = require('cluster');
var cls     = require('continuation-local-storage');
var ns      = cls.createNamespace('myNamespace');
var myCache = require('cluster-node-cache')(cluster,{},ns);

Store a key (SET):

myCache.set(key, val, [ttl])

Sets a key value pair. It is possible to define a ttl (in seconds).
Returns true on success.

obj = { my: "Special", variable: 42 };
myCache.set("myKey", obj).then(function(result) {
  console.log(result.err);
  console.log(result.success);
});

Retrieve a key (GET):

myCache.get(key)

Gets a saved value from the cache. Returns a undefined if not found or expired.

eyCache.get("myKey").then(function(results) {
  if(results.err) {
    console.log(results.err);
  } else {
    console.log(results.value.myKey);
  }
});

Get multiple keys (MGET):

myCache.get([ key1, key2, ...])

Gets multiple saved values from the cache.

myCache.get(["keyA", "keyB"]).then(function(results) {
  if(results.err) {
    console.log(results.err);
  } else {
    console.log(results.value);
  }
});

Delete a key (DEL):

myCache.del(key)

Delete a key. Returns the number of deleted entries. A delete will never fail.

myCache.del("myKey").then(function(results) {
  if(!results.err) {
    console.log(results.count);
  }
});

Change TTL (TTL):

myCache.ttl(key, ttl)

Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false.
If the ttl-argument isn't passed the default-TTL will be used.

myCache = new NodeCache({ stdTTL: 100 })
myCache.ttl( "myKey", 100).then(function(results) {
  if(!results.err) {
    console.log(results.changed); // true
  }
});

List keys (KEYS)

myCache.keys()

Returns an array of all existing keys.

myCache.keys().then(function(results) {
  if(!results.err) {
    console.log(results.keys);
  }
});

Statistics (STATS):

myCache.getStats()

Returns the statistics.

myCache.getStats().then(function(results) {
  console.log(results);
  /*
    {
      keys: 0,    // global key count
      hits: 0,    // global hit count
      misses: 0,  // global miss count
      ksize: 0,   // global key size count
      vsize: 0    // global value size count
    }
  */
});

Flush all data (FLUSH):

myCache.flushAll()

Flush all data.

myCache.flushAll().then(function(results) {
  console.log(results);
  /*
    {
      keys: 0,    // global key count
      hits: 0,    // global hit count
      misses: 0,  // global miss count
      ksize: 0,   // global key size count
      vsize: 0    // global value size count
    }
  */
});

Pull requests welcomed !

  • Especially if they contain tests better than the current terrible ones :)

The ISC License

Copyright (c) 2015, Lee Turner

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.