Skip to content

Commit 5458f23

Browse files
committed
redis proviver and examples
1 parent bb8a016 commit 5458f23

File tree

7 files changed

+57
-18
lines changed

7 files changed

+57
-18
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ Currently requires express and connect to run, because it uses `res.on('header',
1212

1313
Clone this repo into your project's node_modules directory. You can also install from npm, but the package may be outdated: https://www.npmjs.org/package/miniprofiler.
1414

15-
Then see [examples/express/server.js](/blob/master/examples/express/server.js) for example use.
15+
Then see [examples/express/server.js](/examples/express/server.js) for example use.
16+
17+
![](/examples/example1.png)
18+
![](/examples/example2.png)
1619

1720
# Want to help?
1821

@@ -27,3 +30,4 @@ Things to do:
2730
- add more providers (mongodb, mysql, redis)
2831
- remove old ids from global storage (when?)
2932
- add eslint to travis
33+
- better eslint (https://github.com/NodeRedis/node_redis/blob/master/.eslintrc)

examples/example1.png

67.3 KB
Loading

examples/example2.png

62.5 KB
Loading

examples/express/server.js

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
var miniprofiler = require('../../lib/miniprofiler.js');
22
var pg = require('pg');
3+
var redis = require('redis');
4+
var redisClient = redis.createClient();
35

46
var express = require('express');
57
var connString = 'postgres://postgres:postgres@localhost/miniprofiler';
68

79
var app = express();
810
app.use(miniprofiler.profile());
911
app.use(miniprofiler.for.pg(pg));
12+
app.use(miniprofiler.for.redis(redis));
1013

1114
app.set('view engine', 'pug');
1215
app.set('views', './examples/views');
@@ -15,24 +18,43 @@ app.get('/', function(req, res) {
1518
res.render('home');
1619
});
1720

18-
app.get('/sleep', function(req, res) {
19-
pg.connect(connString, function(err, client, done) {
20-
client.query('SELECT pg_sleep(1)', [], function(err, result) {
21+
app.get('/redis-set-get', function(req, res) {
22+
redisClient.set('customer', '[email protected]', function() {
23+
redisClient.get('key', function(err, reply) {
24+
res.render('home');
25+
});
26+
});
27+
});
28+
29+
app.get('/pg-sleep', function(req, res) {
30+
pg.connect(connString, function(err, pgClient, done) {
31+
pgClient.query('SELECT pg_sleep(1)', [], function(err, result) {
2132
done();
2233
res.render('home');
2334
});
2435
});
2536
});
2637

27-
app.get('/multi-query', function(req, res) {
28-
pg.connect(connString, function(err, client, done) {
29-
client.query('SELECT pg_sleep(1)', [], function(err, result) {
30-
client.query('SELECT $1::int AS number', ['2'], function(err, result) {
31-
done();
32-
res.render('home');
33-
});
34-
});
35-
});
38+
app.get('/all', function(req, res) {
39+
req.miniprofiler.step('Waiting 1 second', function() {
40+
41+
pg.connect(connString, function(err, pgClient, done) {
42+
pgClient.query('SELECT pg_sleep(1)', [ ], function(err, result) {
43+
44+
req.miniprofiler.step('Get from cache', function() {
45+
46+
redisClient.set('customer', '[email protected]', function() {
47+
redisClient.get('key', function(err, reply) {
48+
res.render('home');
49+
});
50+
});
51+
52+
});
53+
54+
});
55+
});
56+
57+
});
3658
});
3759

3860
app.listen(8080);

examples/views/multi-query.pug

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/miniprofiler.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ _.templateSettings = {
2525
exports.configure = configure;
2626
exports.profile = middleware;
2727
exports.for = {
28-
pg: require('./providers/miniprofiler.pg.js')
28+
pg: require('./providers/miniprofiler.pg.js'),
29+
redis: require('./providers/miniprofiler.redis.js')
2930
};
3031

3132
// GLOBALS
@@ -345,6 +346,8 @@ function step(name, request, call) {
345346
* to have ended the query.
346347
*/
347348
function timeQuery(extension, type, query, executeFunction) {
349+
debug(`timeQuery "${type}" with command "${query}"`);
350+
348351
var time = process.hrtime();
349352
var startDate = Date.now();
350353

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var redisSendCommand;
2+
3+
module.exports = function(redis) {
4+
return function(req, res, next) {
5+
redisSendCommand = redisSendCommand || redis.RedisClient.prototype.internal_send_command;
6+
if (req.miniprofiler.enabled) {
7+
redis.RedisClient.prototype.internal_send_command = function(command, args, callback) {
8+
var query = `${command} ${args}`;
9+
req.miniprofiler.timeQuery('redis', query, redisSendCommand.bind(this), command, args, callback);
10+
};
11+
}
12+
next();
13+
};
14+
};

0 commit comments

Comments
 (0)