All Projects → eggjs → Egg Redis

eggjs / Egg Redis

Licence: mit
redis plugin for egg

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Egg Redis

Egg Socket.io
socket.io plugin for eggjs.
Stars: ✭ 209 (-10.68%)
Mutual labels:  egg, egg-plugin
Egg Schedule
Schedule plugin for egg
Stars: ✭ 76 (-67.52%)
Mutual labels:  egg, egg-plugin
Egg Authz
egg-authz is an authorization middleware for Egg.js based on Casbin
Stars: ✭ 50 (-78.63%)
Mutual labels:  egg, egg-plugin
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (-19.66%)
Mutual labels:  egg, egg-plugin
Egg Security
Security plugin for egg, force performance too.
Stars: ✭ 204 (-12.82%)
Mutual labels:  egg, egg-plugin
Egg View
Stars: ✭ 35 (-85.04%)
Mutual labels:  egg, egg-plugin
Egg View React Ssr
Egg React Server Side Render (SSR) Plugin
Stars: ✭ 55 (-76.5%)
Mutual labels:  egg, egg-plugin
Egg 24time
A Twitter-like news and social server for Egg. 微信小程序社区全栈解决方案
Stars: ✭ 493 (+110.68%)
Mutual labels:  egg, egg-plugin
Egg Oauth2 Server
🌟 OAuth2 server plugin for egg.js based on node-oauth2-server
Stars: ✭ 174 (-25.64%)
Mutual labels:  egg, egg-plugin
Egg Router Plus
The missing router feature for eggjs
Stars: ✭ 117 (-50%)
Mutual labels:  egg, egg-plugin
Egg Valid
👮Validation plugin for eggjs
Stars: ✭ 10 (-95.73%)
Mutual labels:  egg, egg-plugin
Egg Multipart
multipart plugin for egg
Stars: ✭ 145 (-38.03%)
Mutual labels:  egg, egg-plugin
Egg Sequelize
Sequelize for Egg.js
Stars: ✭ 540 (+130.77%)
Mutual labels:  egg, egg-plugin
Egg Cancan
cancancan like authorization plugin for Egg.js
Stars: ✭ 47 (-79.91%)
Mutual labels:  egg, egg-plugin
Egg Restfulapi
🏅 基于Egg.js 2.0 & {mongoose,jwt}RESTful API 模板,用于快速集成开发RESTful前后端分离的服务端。
Stars: ✭ 524 (+123.93%)
Mutual labels:  egg, egg-plugin
Egg View Ejs
egg view plugin for ejs.
Stars: ✭ 54 (-76.92%)
Mutual labels:  egg, egg-plugin
Egg Graphql
Stars: ✭ 345 (+47.44%)
Mutual labels:  egg, egg-plugin
Egg Mongoose
Stars: ✭ 386 (+64.96%)
Mutual labels:  egg, egg-plugin
Egg Passport
passport plugin for egg
Stars: ✭ 98 (-58.12%)
Mutual labels:  egg, egg-plugin
Egg View Vue
vue view plugin for egg
Stars: ✭ 136 (-41.88%)
Mutual labels:  egg, egg-plugin

egg-redis

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

Redis client(support redis portocal) based on ioredis for egg framework

Install

$ npm i egg-redis --save

redis Plugin for egg, support egg application access to redis.

This plugin based on ioredis, if you want to know specific usage, you should refer to the document of ioredis.

Configuration

Change ${app_root}/config/plugin.js to enable redis plugin:

exports.redis = {
  enable: true,
  package: 'egg-redis',
};

Configure redis information in ${app_root}/config/config.default.js:

Single Client

config.redis = {
  client: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth',
    db: 0,
  },
}

Multi Clients

config.redis = {
  clients: {
    foo: {                 // instanceName. See below
      port: 6379,          // Redis port
      host: '127.0.0.1',   // Redis host
      password: 'auth',
      db: 0,
    },
    bar: {
      port: 6379,
      host: '127.0.0.1',
      password: 'auth',
      db: 1,
    },
  }
}

Sentinel

config.redis = {
  client: {
    sentinels: [{          // Sentinel instances
      port: 26379,         // Sentinel port
      host: '127.0.0.1',   // Sentinel host
    }],
    name: 'mymaster',      // Master name
    password: 'auth',
    db: 0
  },
}

No password

Redis support no authentication access, but we are highly recommand you to use redis requirepass in redis.conf.

$vim /etc/redis/redis.conf

requirepass xxxxxxxxxx  // xxxxxxxxxx is your password

Because it may be cause security risk, refer:

If you want to access redis with no password, use password: null.

See ioredis API Documentation for all available options.

Customize ioredis version

egg-redis using [email protected] now, if you want to use other version of ioredis, you can pass the instance by config.redis.Redis:

// config/config.default.js
config.redis = {
  Redis: require('ioredis'), // customize ioredis version, only set when you needed
  client: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth',
    db: 0,
  },
}

weakDependent

config.redis = {
  client: {
    port: 6379,          // Redis port
    host: '127.0.0.1',   // Redis host
    password: 'auth',
    db: 0,
    weakDependent: true, // this redis instance won't block app start
  },
}

Usage

In controller, you can use app.redis to get the redis instance, check ioredis to see how to use.

// app/controller/home.js

module.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      // set
      await app.redis.set('foo', 'bar');
      // get
      ctx.body = await app.redis.get('foo');
    }
  };
};

Multi Clients

If your Configure with multi clients, you can use app.redis.get(instanceName) to get the specific redis instance and use it like above.

// app/controller/home.js

module.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      // set
      await app.redis.get('instance1').set('foo', 'bar');
      // get
      ctx.body = await app.redis.get('instance1').get('foo');
    }
  };
};

Clients Depend on Redis Cluster

Before you start to use Redis Cluster, please checkout the document first, especially confirm cluster-enabled yes in Redis Cluster configuration file.

In controller, you also can use app.redis to get the redis instance based on Redis Cluster.

// app/config/config.default.js

exports.redis = {
   client: {
     cluster: true,
     nodes: [{
       host: '127.0.0.1',
       port: '6379',
       family: 'user',
       password: 'password',
       db: 'db',
     }, {
       host: '127.0.0.1',
       port: '6380',
       family: 'user',
       password: 'password',
       db: 'db',
     }]
   },
};

// app/controller/home.js

module.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      // set
      await app.redis.set('foo', 'bar');
      // get
      ctx.body = await app.redis.get('foo');
    }
  };
};

Questions & Suggestions

Please open an issue here.

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].