Skip to content

Commit f877c39

Browse files
author
Ruben Bridgewater
committed
Add prefix option
Fixes #323 Add key prefix tests Add changelog entry for prefix
1 parent a8c3675 commit f877c39

File tree

7 files changed

+154
-270
lines changed

7 files changed

+154
-270
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ limits total amount of connection tries. Setting this to 1 will prevent any reco
214214
* `rename_commands`: *null*; pass a object with renamed commands to use those instead of the original functions. See the [redis security topics](http://redis.io/topics/security) for more info.
215215
* `tls`: an object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback),
216216
to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel).
217+
* `prefix`: *null*; pass a string to prefix all used keys with that string as prefix e.g. 'namespace:test'
217218

218219
```js
219220
var redis = require("redis"),

changelog.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ Changelog
55

66
Features
77

8-
- Added `tls` option to iniate a connection to a redis server behind a TLS proxy. Thanks ([@paddybyers](https://github.com/paddybyers)
9-
- Added a *url* option to pass the connection url with the options object ([@BridgeAR](https://github.com/BridgeAR)
10-
- Added `client.duplicate([options])` to duplicate the current client and return a new one with the same options ([@BridgeAR](https://github.com/BridgeAR)
11-
- Improve performance by up to 20% on almost all use cases ([@BridgeAR](https://github.com/BridgeAR)
8+
- Added `tls` option to iniate a connection to a redis server behind a TLS proxy. Thanks ([@paddybyers](https://github.com/paddybyers))
9+
- Added `prefix` option to auto key prefix any command with the provided prefix (([@luin](https://github.com/luin) & [@BridgeAR](https://github.com/BridgeAR)))
10+
- Added `url` option to pass the connection url with the options object ([@BridgeAR](https://github.com/BridgeAR))
11+
- Added `client.duplicate([options])` to duplicate the current client and return a new one with the same options ([@BridgeAR](https://github.com/BridgeAR))
12+
- Improve performance by up to 20% on almost all use cases ([@BridgeAR](https://github.com/BridgeAR))
1213

1314
Bugfixes
1415

generate_commands.js

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

index.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ var Queue = require('double-ended-queue');
99
var Command = require('./lib/command');
1010
var events = require('events');
1111
var parsers = [];
12-
// This static list of commands is updated from time to time.
13-
// ./lib/commands.js can be updated with generate_commands.js
14-
var commands = require('./lib/commands');
12+
var commands = require('redis-commands');
1513
var connection_id = 0;
1614
var default_port = 6379;
1715
var default_host = '127.0.0.1';
@@ -22,7 +20,7 @@ function debug (msg) { if (exports.debug_mode) { console.error(msg); } }
2220

2321
exports.debug_mode = /\bredis\b/i.test(process.env.NODE_DEBUG);
2422

25-
// hiredis might not be installed
23+
// Hiredis might not be installed
2624
try {
2725
parsers.push(require('./lib/parsers/hiredis'));
2826
} catch (err) {
@@ -189,7 +187,7 @@ RedisClient.prototype.unref = function () {
189187
}
190188
};
191189

192-
// flush provided queues, erroring any items with a callback first
190+
// Flush provided queues, erroring any items with a callback first
193191
RedisClient.prototype.flush_and_error = function (error, queue_names) {
194192
var command_obj;
195193
queue_names = queue_names || ['offline_queue', 'command_queue'];
@@ -457,7 +455,7 @@ RedisClient.prototype.on_info_cmd = function (err, res) {
457455
key = 'db' + i;
458456
}
459457

460-
// expose info key/vals to users
458+
// Expose info key/vals to users
461459
this.server_info = obj;
462460

463461
if (!obj.loading || obj.loading === '0') {
@@ -684,7 +682,7 @@ RedisClient.prototype.return_reply = function (reply) {
684682
} else {
685683
this.pub_sub_mode = true;
686684
}
687-
// subscribe commands take an optional callback and also emit an event, but only the first response is included in the callback
685+
// Subscribe commands take an optional callback and also emit an event, but only the first response is included in the callback
688686
// TODO - document this or fix it so it works in a more obvious way
689687
if (command_obj && typeof command_obj.callback === 'function') {
690688
command_obj.callback(null, reply[1]);
@@ -723,6 +721,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
723721
command_str = '',
724722
buffer_args = false,
725723
big_data = false,
724+
prefix_keys,
726725
buffer = this.options.return_buffers;
727726

728727
if (args === undefined) {
@@ -810,7 +809,14 @@ RedisClient.prototype.send_command = function (command, args, callback) {
810809
if (typeof this.options.rename_commands !== 'undefined' && this.options.rename_commands[command]) {
811810
command = this.options.rename_commands[command];
812811
}
813-
812+
if (this.options.prefix) {
813+
prefix_keys = commands.getKeyIndexes(command, args);
814+
i = prefix_keys.pop();
815+
while (i !== undefined) {
816+
args[i] = this.options.prefix + args[i];
817+
i = prefix_keys.pop();
818+
}
819+
}
814820
// Always use 'Multi bulk commands', but if passed any Buffer args, then do multiple writes, one for each arg.
815821
// This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
816822
command_str = '*' + (args.length + 1) + '\r\n$' + command.length + '\r\n' + command + '\r\n';
@@ -943,23 +949,7 @@ function Multi(client, args) {
943949
}
944950
}
945951

946-
RedisClient.prototype.multi = RedisClient.prototype.MULTI = function (args) {
947-
var multi = new Multi(this, args);
948-
multi.exec = multi.EXEC = multi.exec_transaction;
949-
return multi;
950-
};
951-
952-
RedisClient.prototype.batch = RedisClient.prototype.BATCH = function (args) {
953-
return new Multi(this, args);
954-
};
955-
956-
commands.forEach(function (fullCommand) {
957-
var command = fullCommand.split(' ')[0];
958-
959-
// Skip all full commands that have already been added instead of overwriting them over and over again
960-
if (RedisClient.prototype[command]) {
961-
return;
962-
}
952+
commands.list.forEach(function (command) {
963953

964954
RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command] = function (key, arg, callback) {
965955
if (Array.isArray(key)) {
@@ -1006,7 +996,17 @@ commands.forEach(function (fullCommand) {
1006996
};
1007997
});
1008998

1009-
// store db in this.select_db to restore it on reconnect
999+
RedisClient.prototype.multi = RedisClient.prototype.MULTI = function (args) {
1000+
var multi = new Multi(this, args);
1001+
multi.exec = multi.EXEC = multi.exec_transaction;
1002+
return multi;
1003+
};
1004+
1005+
RedisClient.prototype.batch = RedisClient.prototype.BATCH = function (args) {
1006+
return new Multi(this, args);
1007+
};
1008+
1009+
// Store db in this.select_db to restore it on reconnect
10101010
RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, callback) {
10111011
var self = this;
10121012
return this.send_command('select', [db], function (err, res) {
@@ -1138,7 +1138,7 @@ Multi.prototype.exec_transaction = function (callback) {
11381138
this._client.cork(len + 2);
11391139
this.wants_buffers = new Array(len);
11401140
this.send_command('multi', []);
1141-
// drain queue, callback will catch 'QUEUED' or error
1141+
// Drain queue, callback will catch 'QUEUED' or error
11421142
for (var index = 0; index < len; index++) {
11431143
var args = this.queue.get(index).slice(0);
11441144
var command = args.shift();

lib/commands.js

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"posttest": "jshint ."
2121
},
2222
"dependencies": {
23-
"double-ended-queue": "^2.1.0-0"
23+
"double-ended-queue": "^2.1.0-0",
24+
"redis-commands": "^1.0.1"
2425
},
2526
"engines": {
2627
"node": ">=0.10.0"

0 commit comments

Comments
 (0)