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

pm2-cluster-cache

v2.1.7

Published

pm2 cluster memory cache with ttl and more possibilities of data store in cluster

Downloads

327

Readme

Node.js CI CodeQL

pm2 cluster memory cache

A cluster memory cache for pm2 with some different possibilities of data store. From version 1.0.5 is safe to use without pm2 too, but storage type will be forced to 'self'.

Instalation

npm install pm2-cluster-cache --save

Tests

npm run test

Remember: testing environment may vary depending on storage types. Therefore tests are divided into 4 separate running tests. If you run npm run test, all test will be performed, but you can run specific test, for example npm run test-cluster for cluster type, or npm run test-master for master type, and so on. Every test will create server with max processes, and test against api running on this cluster server.

Usage

const pm2ClusterCache = require('pm2-cluster-cache');
let cache = pm2ClusterCache.init({storage: "cluster"});

//set value to cache for 1s
cache.set('key', 'data', 1000).then(() => {
    console.log('ok');
});

//get cached value
cache.get('key', "someDefaultValue").then(result => {
    console.log(result);
}); 

//get value from cache with metadata
cache.withMeta().get('key', "someDefaultValue").then(result => {
    console.log(result.data);
    console.log(result.metadata);
});

//get cluster data map
cache.keys().then(map => {
    console.log(map);
});

API

  • init(options) - create new cluster cache. Options object can have following keys:
    • defaultTtl - default time to live for keys in ms. Default value is 1000ms.
    • logger - default console. May be any class with impemented methods log and warn.
    • storage - can be one of self, all, master, cluster. Default value is cluster.
      • self - store to actual process, read from actual process. Every process has his own cache, so this cache is not shared between processes.
      • all - store to all processes, read from actual process. Data are duplicated and on every process is stored full replica of all data. If one process restarts, other process are not affected with cache misses.
      • master - store to master, read from master. If actual process restarts, other process are not affected with cache miss, if actual process is not master. If master restarts, every process in cluster will lost all cached data.
      • cluster - store to specific process, read from specific process. Every process in cluster has part of data, so if one process restarts, other process will lost only part of data. Targer process for storage is detemined by key.
  • set(key, value, [ttl]) - store value under key, with given ttl (in ms).
  • get(key, [defaultValue]) - get value stored under key 'key'.
  • inc(key) - increment key by 1. If key not exists, creates it with value 0 and returns.
  • dec(key) - decrement key by 1. If key not exists, creates it with value 0 and returns.
  • delete(key) - removes key from all process where is given key stored. Returns in Promise array of processes deleted from.
  • flush() - removes all keys from all processes.
  • keys() - returns in Promise map of cluster with numbers of stored keys.
  • withMeta() - returns decorated get and set method that are returning metadata with data.

metadata object is object with keys:

  • storedOn - array of int. Processes that have key stored.
  • readFrom - int. Proccess which provided data.
  • servedBy - int. Actual process.

PM2 metrics

with command pm2 describe <your app name> you can see in Code metrics cache hit rate and miss rate on every process.