🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

mtr-cache

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mtr-cache

Library ini akan menyimpan response ke dalam Redis' cache ### Dependency Library ini menggunakan package [**bluebird v3.5.5**](https://www.npmjs.com/package/bluebird) dan [**redis v2.8.0**](https://www.npmjs.com/package/redis). ### Installation ```bas

1.1.0
latest
npm
Version published
Maintainers
1
Created
Source

Health Check Library

Library ini akan menyimpan response ke dalam Redis' cache

Dependency

Library ini menggunakan package bluebird v3.5.5 dan redis v2.8.0.

Installation

npm i git+ssh://git@repository.meteor.asia/mtr-platform/libs/redis-cache#v1.1.0

Penggunaan

Deklarasikan module library di dalam file app.js.

const { setRedis } = require('mtr-cache');

const redisHost = env.REDIS_HOST || '127.0.0.1';
const redisPort = Number(env.REDIS_PORT) || '6379';
setRedis(redisHost, redisPort);

Buat sebuah middleware untuk melakukan pengecekan cache, middleware dapat dibuat secara individu per endpoint atau dapat dibuat secara global pada app.js. Berikut adalah contoh bila dibuat secara individu per endpoint:

const { response } = require('mtr-response');
const { mwCache } = require('mtr-cache');

const { env } = process;
const svc = env.SVC_NAME || 'Meteor';

module.exports = async (req, res, next) => {
  const {
    'x-forwarded-path': Path, 'x-forwarded-method': Method, 'x-app-key': appKey, 'x-forwarded-authorization': Authorization,
  } = req.headers;
  const key = `${svc}-svc_p:${(path.split('?')[0])}_m:${Method}_k:${appKey}_a:${Authorization}`;
  await mwCache(key)
    .then(ok => response(200, ok, res))
    .catch(err => response(400, err, res));
};

Menyimpan Cache

Berikut adalah contoh penggunaanya:

const { storeCache } = require('mtr-cache');

const svcName = env.SVC_NAME || 'Meteor';

module.exports = async (req, res) => {
  const key = `${svcName}-svc_p:${(path.split('?')[0])}_m:${method}_k:${xAppKey}_a:${auth !== undefined ? auth : '-'}`;
  const redisVal = 'OK';
  const ttl = 15;
  await storeCache(key, redisVal, ttl)
    .then(() => response(200, { key, redisVal }, res))
    .catch(err => response(400, err, res));
};

Pembersihan Cache

Seluruh cache dari satu service dapat dibersihkan dengan function purgeCache(keypart). Berikut adalah contoh penggunaannya:

const { response } = require('mtr-response');
const { purgeCache } = require('mtr-cache');

const svcName = env.SVC_NAME || 'Meteor';

module.exports = async (req, res) => {
  const replies = await purgeCache(`${svcName}-svc`);
  if (replies) response(200, 'Cache purged!', res);
  response(400, 'Cache purge failed!', res);
};

FAQs

Package last updated on 08 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts