• Stars
    star
    267
  • Rank 148,860 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

redis plugin for egg

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 ioredis@4 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

More Repositories

1

egg

๐Ÿฅš Born to build better enterprise frameworks and apps with Node.js & Koa
JavaScript
18,650
star
2

examples

Store all egg examples in one place
JavaScript
1,748
star
3

awesome-egg

Awesome Egg.js Web Framework and Plugin.
869
star
4

egg-sequelize

Sequelize for Egg.js
JavaScript
609
star
5

egg-mongoose

JavaScript
437
star
6

egg-graphql

JavaScript
371
star
7

egg-mysql

MySQL plugin for egg
TypeScript
328
star
8

egg-ant-design-pro

showcase for Egg loves Ant Design
JavaScript
318
star
9

egg-validate

validate plugin for egg
JavaScript
259
star
10

egg-security

Security plugin for egg, force performance too.
JavaScript
237
star
11

egg-socket.io

socket.io plugin for eggjs.
JavaScript
237
star
12

egg-cluster

cluster manager for egg
JavaScript
218
star
13

egg-core

A core Pluggable framework based on koa.
JavaScript
217
star
14

tegg

Strong Type framework with eggjs.
TypeScript
185
star
15

egg-bin

egg developer tool
TypeScript
183
star
16

egg-multipart

multipart plugin for egg
JavaScript
166
star
17

egg-ts-helper

๐Ÿณ Generate TypeScript definition files(d.ts) for Egg
TypeScript
157
star
18

egg-init

Init egg app helper tools
JavaScript
151
star
19

egg-view-vue

vue view plugin for egg
JavaScript
150
star
20

egg-cors

CORS plugin for egg
JavaScript
150
star
21

egg-logger

Egg logger
JavaScript
150
star
22

vscode-eggjs

vscode extension for https://eggjs.org/
TypeScript
145
star
23

egg-mock

Mock library for egg testing.
JavaScript
144
star
24

egg-router-plus

The missing router feature for eggjs
JavaScript
140
star
25

egg-scripts

deploy tool for egg projects
JavaScript
112
star
26

egg-rest

Restful API plugin for egg
JavaScript
107
star
27

egg-passport

passport plugin for egg
JavaScript
106
star
28

egg-view-react

egg view plugin for react
JavaScript
89
star
29

egg-view-nunjucks

nunjucks view plugin for egg
JavaScript
89
star
30

egg-static

static server plugin for egg
JavaScript
89
star
31

egg-schedule

Schedule plugin for egg
JavaScript
88
star
32

egg-oss

aliyun oss plugin for egg
JavaScript
87
star
33

egg-grpc

grpc plugin for egg
JavaScript
81
star
34

egg-sofa-rpc

SOFARPC plugin for egg
JavaScript
80
star
35

egg-userrole

user role plugin for egg
JavaScript
77
star
36

eslint-config-egg

Node Style Guide for Egg.
JavaScript
67
star
37

egg-view-ejs

egg view plugin for ejs.
JavaScript
64
star
38

egg-onerror

error handler for egg
Mustache
56
star
39

egg-alinode

alinode plugin for egg
JavaScript
54
star
40

egg-logrotator

Log rotate plugin for egg
JavaScript
53
star
41

egg-view-assets

Manage frontend assets in development and production.
JavaScript
52
star
42

egg-router

router for eggjs, fork from koa-router with some additional features
JavaScript
50
star
43

egg-orm

Object relational mapping for Egg framework
JavaScript
49
star
44

aliyun-egg

node web framework for aliyun, base on eggjs
JavaScript
49
star
45

egg-cancan

cancancan like authorization plugin for Egg.js
JavaScript
48
star
46

egg-session

session plugin for egg
JavaScript
47
star
47

egg-healthy

Liveness and Readiness health check for egg application
JavaScript
45
star
48

egg-passport-local

wrap passport-local strategy for egg-passport
JavaScript
45
star
49

egg-aop

AOP plugin for eggjs, add DI, AOP support.
TypeScript
44
star
50

egg-session-redis

redis store for egg session
JavaScript
43
star
51

egg-showcase-aliyun-blog

A blog showcase for aliyun-egg
JavaScript
42
star
52

egg-view

JavaScript
41
star
53

jar2proxy

transfer java facade jar to proxyjs
Java
41
star
54

egg-dubbo-rpc

dubbo rpc plugin for egg
JavaScript
40
star
55

egg-boilerplate-ts

Boilerplate for egg typescript project
TypeScript
40
star
56

egg-i18n

i18n plugin for egg
JavaScript
39
star
57

doctools

doctools for eggjs
JavaScript
38
star
58

egg-prometheus

Prometheus plugin for Egg.js
JavaScript
38
star
59

egg-http-proxy

http proxy for egg
JavaScript
38
star
60

egg-cloud

egg cloud provider tools for developers to quickly build some of the common patterns in distributed systems
JavaScript
38
star
61

egg-rpc-generator

RPC tools for egg framework
JavaScript
37
star
62

egg-errors

Errors for Egg.js.
TypeScript
31
star
63

egg-dingtalk

egg plugin for dingtalk
JavaScript
30
star
64

egg-cookies

cookies module for egg, base on pillarjs/cookies
JavaScript
30
star
65

egg-rpc

rpc plugin for eggjs
JavaScript
29
star
66

egg-tracer

tracer plugin for egg
JavaScript
29
star
67

create-egg

so you could use `npm init egg showcase` to init egg project
JavaScript
28
star
68

docker

Egg official docker image
JavaScript
27
star
69

egg-websocket

egg plugin for websocket
JavaScript
26
star
70

egg-path-matching

url path match/ignore support string, regexp and function
JavaScript
26
star
71

egg-lru

egg lru-cache plugin
JavaScript
25
star
72

egg-boilerplate-microservice

Boilerplate for egg microservice project
JavaScript
25
star
73

egg-watcher

Watcher plugin for egg
JavaScript
25
star
74

egg-parameters

Merge all parameters (ctx.params, ctx.request.query, ctx.request.body) into ctx.params like Rails application.
JavaScript
24
star
75

egg-development-proxyagent

[DEPRECATED] A proxy adapter for debugging httpclient on egg.
JavaScript
21
star
76

egg-userservice

userservice plugin for egg
JavaScript
21
star
77

egg-zookeeper

egg plugin for zookeeper
JavaScript
20
star
78

benchmark

benchmark for egg
JavaScript
20
star
79

egg-opentracing

Implementation of opentracing in Egg.js
JavaScript
19
star
80

egg-datahub

Macaca DataHub plugin for Egg.js
JavaScript
19
star
81

egg-ons

aliyun ons plugin for egg
JavaScript
18
star
82

egg-validate-schema

validate by json-schema plugin for egg
JavaScript
17
star
83

egg-development-proxyworker

**[DEPRECATED]** A proxy worker for debugging worker on egg
JavaScript
17
star
84

egg-boilerplate-simple

Boilerplate for egg simple project
JavaScript
16
star
85

eggjs.github.io

eggjs docs site
JavaScript
16
star
86

egg-logger-sls

Logger transport for aliyun sls.
JavaScript
15
star
87

egg-logview

Provide a log files viewer for development purpose.
JavaScript
15
star
88

egg-ci

Auto gen ci config file
JavaScript
15
star
89

egg-jsonp

jsonp support for egg, with security check inside
JavaScript
14
star
90

egg-boilerplate-plugin

Boilerplate for egg plugin
JavaScript
14
star
91

koa-express-adapter

The adapter for the migration from express to koa
JavaScript
14
star
92

egg-boilerplate-alipay-tiny

ๆ”ฏไป˜ๅฎๅฐ็จ‹ๅบๆœๅŠก็ซฏ่„šๆ‰‹ๆžถ
JavaScript
14
star
93

egg-view-handlebars

egg view plugin for handlebars
JavaScript
12
star
94

egg-passport-github

github passport plugin for egg
JavaScript
11
star
95

egg-leancloud

leancloud plugin for Egg.js
JavaScript
11
star
96

egg-instrument

Compute the duration of an operation in local environment.
JavaScript
11
star
97

egg-utils

Utils for all egg projects.
TypeScript
11
star
98

egg-tslint-to-eslint

Migrate tslint-config-egg to eslint-config-egg in ts project.
JavaScript
9
star
99

egg-lookout

SOFALookout plugin for egg
JavaScript
9
star
100

egg-host

A host plugin for egg. It supports modify the host config, just like edit the /etc/hosts.
JavaScript
9
star