egg-redis
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -19,8 +19,10 @@ 'use strict'; | ||
// Cluster Redis | ||
// cluster: [{ | ||
// host: 'host', | ||
// port: 'port', | ||
// family: 'user', | ||
// password: 'password', | ||
// db: 'db', | ||
// client: [ | ||
// cluster: true, | ||
// nodes: { | ||
// host: 'host', | ||
// port: 'port', | ||
// family: 'user', | ||
// password: 'password', | ||
// db: 'db', | ||
// },{ | ||
@@ -36,3 +38,3 @@ // host: 'host', | ||
// clients: { | ||
// db1: { | ||
// instance1: { | ||
// host: 'host', | ||
@@ -44,3 +46,3 @@ // port: 'port', | ||
// }, | ||
// db2: { | ||
// instance2: { | ||
// host: 'host', | ||
@@ -47,0 +49,0 @@ // port: 'port', |
@@ -7,23 +7,47 @@ 'use strict'; | ||
module.exports = app => { | ||
app.addSingleton('ioredis', createOneClient); | ||
app.addSingleton('redis', createClient); | ||
}; | ||
function createOneClient(config, app) { | ||
assert(config.host && config.port && config.password && config.db, | ||
`[egg-redis] 'host: ${config.host}', 'port: ${config.port}', 'password: ${config.password}', 'db: ${config.db}' are required on config`); | ||
let count = 0; | ||
app.coreLogger.info('[egg-redis] connecting redis://:%s@%s:%s/%s', | ||
config.password, config.host, config.port, config.db); | ||
const client = new Redis(config); | ||
function createClient(config, app) { | ||
let client; | ||
client.on('connect', function () { | ||
if (config.cluster === true) { | ||
assert(config.nodes && config.nodes.length !== 0, '[egg-redis] cluster nodes configuration is required when use cluster redis'); | ||
}); | ||
config.nodes.forEach(client => { | ||
assert(client.host && client.port && client.password !== undefined && client.db, | ||
`[egg-redis] 'host: ${client.host}', 'port: ${client.port}', 'password: ${client.password}', 'db: ${client.db}' are required on config`); | ||
}); | ||
app.coreLogger.info('[egg-redis] cluster connecting start'); | ||
client.on('error', function () { | ||
client = new Redis.Cluster(config); | ||
client.on('connect', function() { | ||
app.coreLogger.info('[egg-redis] cluster connect success'); | ||
}); | ||
client.on('error', function(error) { | ||
app.coreLogger.error(error); | ||
}); | ||
} else { | ||
assert(config.host && config.port && config.password !== undefined && config.db, | ||
`[egg-redis] 'host: ${config.host}', 'port: ${config.port}', 'password: ${config.password}', 'db: ${config.db}' are required on config`); | ||
}); | ||
app.coreLogger.info('[egg-redis] connecting redis://:%s@%s:%s/%s', | ||
config.password, config.host, config.port, config.db); | ||
client.on('reconnect', function () { | ||
client = new Redis(config); | ||
client.on('connect', function() { | ||
app.coreLogger.info('[egg-redis] connect success on redis://:%s@%s:%s/%s', | ||
config.password, config.host, config.port, config.db); | ||
}); | ||
client.on('error', function(error) { | ||
app.coreLogger.error(error); | ||
}); | ||
} | ||
app.beforeStart(function* () { | ||
const result = yield client.time(); | ||
const index = count++; | ||
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, redis currentTime: ${result[0]}`); | ||
}); | ||
@@ -33,29 +57,1 @@ | ||
} | ||
function createClusterClient(config, app) { | ||
config.forEach((client) => { | ||
assert(client.host && client.port && client.password && client.db, | ||
`[egg-redis] 'host: ${client.host}', 'port: ${client.port}', 'password: ${client.password}', 'db: ${client.db}' are required on config`); | ||
}) | ||
/* | ||
app.coreLogger.info('[egg-redis] connecting %s@%s:%s/%s', | ||
config.user, config.host, config.port, config.database); | ||
*/ | ||
const cluster = new Redis.Cluster(config); | ||
cluster.on('connect', function () { | ||
}); | ||
cluster.on('error', function () { | ||
}); | ||
cluster.on('reconnect', function () { | ||
}); | ||
return cluster; | ||
} |
{ | ||
"name": "egg-redis", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Redis plugin for egg", | ||
@@ -16,3 +16,3 @@ "eggPlugin": { | ||
"dependencies": { | ||
"ioredis": "*" | ||
"ioredis": "^2.5.0" | ||
}, | ||
@@ -19,0 +19,0 @@ "devDependencies": { |
@@ -6,7 +6,7 @@ 'use strict'; | ||
host: '127.0.0.1', | ||
port: 3306, | ||
port: 6379, | ||
password: '', | ||
db: 'test', | ||
db: '0', | ||
}, | ||
agent:true | ||
}; |
'use strict'; | ||
const assert = require('assert'); | ||
const request = require('supertest'); | ||
const mm = require('egg-mock'); | ||
const utility = require('utility'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
describe('test/redis.test.js', () => { | ||
let app; | ||
const uid = utility.randomString(); | ||
let app, | ||
app2; | ||
before(() => { | ||
before(function* () { | ||
app = mm.app({ | ||
baseDir: 'apps/redisapp', | ||
}); | ||
return app.ready(); | ||
app2 = mm.app({ | ||
baseDir: 'apps/redisclusterapp', | ||
}); | ||
yield app2.ready(); | ||
}); | ||
beforeEach(function* () { | ||
after(() => { | ||
app.close(); | ||
app2.close(); | ||
}); | ||
afterEach(function* () { | ||
afterEach(mm.restore); | ||
it('should query', done => { | ||
app.redis.set('foo', 'bar'); | ||
app.redis.get('foo', function(err, result) { | ||
assert(result === 'bar'); | ||
done(); | ||
}); | ||
}); | ||
after(done => { | ||
app.redis.end(err => { | ||
app.close(); | ||
done(err); | ||
it('should support cluster config', done => { | ||
app2.redis.set('foo', 'bar'); | ||
app2.redis.get('foo', function(err, result) { | ||
assert(result === 'bar'); | ||
done(); | ||
}); | ||
}); | ||
afterEach(mm.restore); | ||
it('should query', () => { | ||
return request(app.callback()) | ||
.get('/') | ||
.expect(200); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Floating dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
882464
35
58375
0
0
2
+ Addedbluebird@3.7.2(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddouble-ended-queue@2.1.0-0(transitive)
+ Addedflexbuffer@0.0.6(transitive)
+ Addedioredis@2.5.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedms@2.0.0(transitive)
+ Addedredis-commands@1.7.0(transitive)
+ Addedredis-parser@1.3.0(transitive)
- Removed@ioredis/commands@1.2.0(transitive)
- Removeddebug@4.3.5(transitive)
- Removeddenque@2.1.0(transitive)
- Removedioredis@5.4.1(transitive)
- Removedlodash.defaults@4.2.0(transitive)
- Removedlodash.isarguments@3.1.0(transitive)
- Removedms@2.1.2(transitive)
- Removedredis-errors@1.2.0(transitive)
- Removedredis-parser@3.0.0(transitive)
- Removedstandard-as-callback@2.1.0(transitive)
Updatedioredis@^2.5.0