New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

egg-cache-9

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-cache-9

An easy-to-use egg cache plugin based on node-cache-9

latest
Source
npmnpm
Version
1.1.2
Version published
Maintainers
1
Created
Source

egg-cache-9

node version NPM version npm download

The plugin implements an easy-to-use caching function based on node-cache-9, which supports caching data into memory and caching data to redis.

中文说明

Install

$ npm i egg-cache-9 --save

Usage

// {app_root}/config/plugin.js
exports.cache9 = {
  enable: true,
  package: 'egg-cache-9',
};
// {app_root}/config/config.default.js
exports.cache9 = {
  client: {
    class: 'memory',
    ttl: 300,
  }
};


// {app_root}/app/****.js
const cache = app.cache9;

//  get data from a data source or cache
let data = await cache.get('key', async ()=>{ /* get data from data source and return it */ });
cache.renew('key'); // update expiration time
await cache.clear('key'); // clear cache

// get a lot of datas from a data source or cache
let {list, json} = await cache.getM('key', ids, obj=>obj.id, async (lst)=>{ /* get data from data source and return it */ });
cache.renewM('key', ids); // update expiration time
await cache.clearM('key', ids); //clear cache
await cache.clearM('key'); //clear cache
// {app_root}/config/config.default.js
exports.cache9 = {
  default: {
    ttl: 300,
  },
  clients: {
    cacheA: {
      class: 'memory',
    },
    cacheB: {
      class: 'redis',
      rds: { host: '127.0.0.1' }
    }
  }
};


// {app_root}/app/****.js
const cacheA = app.cache9.get('cacheA');
const cacheB = app.cache9.get('cacheB');

// get data from a data source or cache
let data = await cacheA.get('key', async ()=>{ /* get data from data source and return it */ });
cacheA.renew('key'); // update expiration time
await cacheA.clear('key'); //clear cache

// get a lot of datas from a data source or cache
let {list, json} = await cacheB.getM('key', ids, obj=>obj.id, async (lst)=>{ /* get data from data source and return it */ });
cacheB.renewM('key', ids); // update expiration time
await cacheB.clearM('key', ids); //clear cache
await cacheB.clearM('key'); //clear cache

see node-cache-9 for more detail.

Use with egg-redis

When using the egg-redis plugin, you can specify the redis client directly in the configuration.

exports.cache9 = {
  default: {
    ttl: 300,
  },
  clients: {
    cacheA: { // The memory cache needs to set both pubRedis and subRedis as string
      class: 'memory',
      pubRedis: 'cachePub', // app.redis.get('cachePub')
      subRedis: 'cacheSub', // app.redis.get('cacheSub')
    },
    cacheB: { // redis cache can set redis as string
      class: 'redis',
      redis: 'cache', // app.redis.get('cache')
    },
    cacheC: { // If you have not configured getRedis, redis and rds, use app.redis
      class: 'redis',
    }
  }
};

Configuration

see config/config.default.js for more detail.

Unit tests

Run redis-server in localhost first

npm run test

License

MIT
This README was translate by google

Keywords

egg

FAQs

Package last updated on 09 Jul 2020

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