All Projects → hexindai → egg-cache

hexindai / egg-cache

Licence: MIT license
💾 Cache plugin for eggjs

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to egg-cache

Egg Graphql
Stars: ✭ 345 (+475%)
Mutual labels:  egg-plugin, eggjs
Egg Mongo Native
MongoDB egg.js plugin using native driver.
Stars: ✭ 69 (+15%)
Mutual labels:  egg-plugin, eggjs
Egg 24time
A Twitter-like news and social server for Egg. 微信小程序社区全栈解决方案
Stars: ✭ 493 (+721.67%)
Mutual labels:  egg-plugin, eggjs
Egg Cancan
cancancan like authorization plugin for Egg.js
Stars: ✭ 47 (-21.67%)
Mutual labels:  egg-plugin, eggjs
Egg Oauth2 Server
🌟 OAuth2 server plugin for egg.js based on node-oauth2-server
Stars: ✭ 174 (+190%)
Mutual labels:  egg-plugin, eggjs
egg-logger-sls
Logger transport for aliyun sls.
Stars: ✭ 14 (-76.67%)
Mutual labels:  egg-plugin, eggjs
Egg Authz
egg-authz is an authorization middleware for Egg.js based on Casbin
Stars: ✭ 50 (-16.67%)
Mutual labels:  egg-plugin, eggjs
egg-parameters
Merge all parameters (ctx.params, ctx.request.query, ctx.request.body) into ctx.params like Rails application.
Stars: ✭ 24 (-60%)
Mutual labels:  egg-plugin, eggjs
Egg Mp
EggJS插件:微信公众平台基本服务
Stars: ✭ 153 (+155%)
Mutual labels:  egg-plugin, eggjs
Egg Router Plus
The missing router feature for eggjs
Stars: ✭ 117 (+95%)
Mutual labels:  egg-plugin, eggjs
Egg Sofa Rpc
SOFARPC plugin for egg
Stars: ✭ 71 (+18.33%)
Mutual labels:  egg-plugin, eggjs
egg-rbac
Role Based Access Control for eggjs
Stars: ✭ 32 (-46.67%)
Mutual labels:  egg-plugin, eggjs
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+213.33%)
Mutual labels:  egg-plugin, eggjs
egg-sentry
Sentry Plugin For Egg.js
Stars: ✭ 18 (-70%)
Mutual labels:  egg-plugin, eggjs
egg-cookies
cookies module for egg, base on pillarjs/cookies
Stars: ✭ 28 (-53.33%)
Mutual labels:  egg-plugin
egg-sequelize-auto
Automatically generate bare sequelize models from your database, adjustment for egg
Stars: ✭ 27 (-55%)
Mutual labels:  egg-plugin
ves
Vue SSR(Server Side Render) Web Framework for Egg
Stars: ✭ 23 (-61.67%)
Mutual labels:  eggjs
egg-view-assets
Manage frontend assets in development and production.
Stars: ✭ 51 (-15%)
Mutual labels:  eggjs
eggjs-oAuth2-server
基于eggjs的oAuth2.0授权服务端,包含完整流程实例
Stars: ✭ 118 (+96.67%)
Mutual labels:  eggjs
egg-grpc
grpc plugin for egg
Stars: ✭ 79 (+31.67%)
Mutual labels:  egg-plugin

egg-cache

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Based on cache-manager

All store engine based on cache-manager can be used.

中文文档

Installation

npm i egg-cache -S

or

yarn add egg-cache

Configuration

// config/plugin.js
exports.cache = {
  enable: true,
  package: 'egg-cache',
};
// config/config.default.js
exports.cache = {
  default: 'memory',
  stores: {
    memory: {
      driver: 'memory',
      max: 100,
      ttl: 0,
    },
  },
};

Usage

await app.cache.set('foo', 'bar', 60, { foo: 'bar' });

await app.cache.get('foo'); // 'bar'

await app.cache.has('foo'); // true

await app.cache.del('foo');
await app.cache.get('foo', 'default');  // 'default'

await app.cache.has('foo'); // false

// closure
await app.cache.set('foo', () => {
  return 'bar';
}); // 'bar'

// Promise
await app.cache.set('foo', () => {
  return Promise.resolve('bar');
});  // 'bar'

// Get cached value. If it's not existed, get and save the value from closure
await app.cache.get('foo', () => {
  return 'bar';
}); // 'bar'

// You can declare an `expire` option
await app.cache.get('foo', () => {
  return 'bar';
}, 60, {
  foo: 'bar'
});

//  foo was cached
await app.cache.get('foo'); // 'bar'

// clear cache
await app.cache.reset();

Add store

  1. config: the store in the configuration uses the driver instead.
// config/config.default.js

const redisStore = require('cache-manager-ioredis');

exports.cache = {
  default: 'memory',
  stores: {
    memory: {
      driver: 'memory',
      max: 100,
      ttl: 0,
    },
    redis: { // full config: https://github.com/dabroek/node-cache-manager-ioredis#single-store
      driver: redisStore,
      host: 'localhost',
      port: 6379,
      password: '',
      db: 0,
      ttl: 600,
      valid: _ => _ !== null,
    },
  },
};
  1. usage
const store = app.cache.store('redis');

await store.set('foo', 'bar');
await store.get('foo'); // 'bar'

await store.del('foo');
await store.has('foo'); // false

Api

cache.set(name, value, [expire=null, options=null]);

Set Cache

  • name cache name
  • value cache value
  • expire (Optional) expire(default from config file,the unit is second, 0 means nerver expire)
  • options (Optional) Refer to cache-manager)

cache.get(name, [defaultValue=null, expire=null, options=null]);

cache.del(name);

cache.has(name);

cache.store(name, [options=null]);

cache.reset();

Default configuration

Refer to config/config.default.js

Unit Test

npm test

Issue

Refer to Issues.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].