1 Star 0 Fork 0

Riant/pm2-cluster-memory-cache

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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.

MIT License Copyright (c) 2021 Michal Pavlik Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Fork 自 https://github.com/pavlikm/pm2-cluster-memory-cache,修改其中的 to-item 依赖为本地以来以便修改其 Object.prototype to 为 bugu_to,避免与其他库冲突 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/riant/pm2-cluster-memory-cache.git
git@gitee.com:riant/pm2-cluster-memory-cache.git
riant
pm2-cluster-memory-cache
pm2-cluster-memory-cache
master

搜索帮助