Skip to content

Commit

Permalink
Add prefix option
Browse files Browse the repository at this point in the history
Fixes redis#323

Add key prefix tests

Add changelog entry for prefix
  • Loading branch information
Ruben Bridgewater committed Nov 23, 2015
1 parent a8c3675 commit f877c39
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 270 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ limits total amount of connection tries. Setting this to 1 will prevent any reco
* `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.
* `tls`: an object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback),
to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel).
* `prefix`: *null*; pass a string to prefix all used keys with that string as prefix e.g. 'namespace:test'

```js
var redis = require("redis"),
Expand Down
9 changes: 5 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ Changelog

Features

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

Bugfixes

Expand Down
41 changes: 0 additions & 41 deletions generate_commands.js

This file was deleted.

54 changes: 27 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ var Queue = require('double-ended-queue');
var Command = require('./lib/command');
var events = require('events');
var parsers = [];
// This static list of commands is updated from time to time.
// ./lib/commands.js can be updated with generate_commands.js
var commands = require('./lib/commands');
var commands = require('redis-commands');
var connection_id = 0;
var default_port = 6379;
var default_host = '127.0.0.1';
Expand All @@ -22,7 +20,7 @@ function debug (msg) { if (exports.debug_mode) { console.error(msg); } }

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

// hiredis might not be installed
// Hiredis might not be installed
try {
parsers.push(require('./lib/parsers/hiredis'));
} catch (err) {
Expand Down Expand Up @@ -189,7 +187,7 @@ RedisClient.prototype.unref = function () {
}
};

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

// expose info key/vals to users
// Expose info key/vals to users
this.server_info = obj;

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

if (args === undefined) {
Expand Down Expand Up @@ -810,7 +809,14 @@ RedisClient.prototype.send_command = function (command, args, callback) {
if (typeof this.options.rename_commands !== 'undefined' && this.options.rename_commands[command]) {
command = this.options.rename_commands[command];
}

if (this.options.prefix) {
prefix_keys = commands.getKeyIndexes(command, args);
i = prefix_keys.pop();
while (i !== undefined) {
args[i] = this.options.prefix + args[i];
i = prefix_keys.pop();
}
}
// Always use 'Multi bulk commands', but if passed any Buffer args, then do multiple writes, one for each arg.
// This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
command_str = '*' + (args.length + 1) + '\r\n$' + command.length + '\r\n' + command + '\r\n';
Expand Down Expand Up @@ -943,23 +949,7 @@ function Multi(client, args) {
}
}

RedisClient.prototype.multi = RedisClient.prototype.MULTI = function (args) {
var multi = new Multi(this, args);
multi.exec = multi.EXEC = multi.exec_transaction;
return multi;
};

RedisClient.prototype.batch = RedisClient.prototype.BATCH = function (args) {
return new Multi(this, args);
};

commands.forEach(function (fullCommand) {
var command = fullCommand.split(' ')[0];

// Skip all full commands that have already been added instead of overwriting them over and over again
if (RedisClient.prototype[command]) {
return;
}
commands.list.forEach(function (command) {

RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command] = function (key, arg, callback) {
if (Array.isArray(key)) {
Expand Down Expand Up @@ -1006,7 +996,17 @@ commands.forEach(function (fullCommand) {
};
});

// store db in this.select_db to restore it on reconnect
RedisClient.prototype.multi = RedisClient.prototype.MULTI = function (args) {
var multi = new Multi(this, args);
multi.exec = multi.EXEC = multi.exec_transaction;
return multi;
};

RedisClient.prototype.batch = RedisClient.prototype.BATCH = function (args) {
return new Multi(this, args);
};

// Store db in this.select_db to restore it on reconnect
RedisClient.prototype.select = RedisClient.prototype.SELECT = function (db, callback) {
var self = this;
return this.send_command('select', [db], function (err, res) {
Expand Down Expand Up @@ -1138,7 +1138,7 @@ Multi.prototype.exec_transaction = function (callback) {
this._client.cork(len + 2);
this.wants_buffers = new Array(len);
this.send_command('multi', []);
// drain queue, callback will catch 'QUEUED' or error
// Drain queue, callback will catch 'QUEUED' or error
for (var index = 0; index < len; index++) {
var args = this.queue.get(index).slice(0);
var command = args.shift();
Expand Down
197 changes: 0 additions & 197 deletions lib/commands.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"posttest": "jshint ."
},
"dependencies": {
"double-ended-queue": "^2.1.0-0"
"double-ended-queue": "^2.1.0-0",
"redis-commands": "^1.0.1"
},
"engines": {
"node": ">=0.10.0"
Expand Down
Loading

0 comments on commit f877c39

Please sign in to comment.