You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Export RedisClient prototype to support extending.
See examples/extend.js for a simple example. Remove command list from exports while I was in there.
This commit is contained in:
24
examples/extend.js
Normal file
24
examples/extend.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
var redis = require("redis"),
|
||||||
|
client = redis.createClient();
|
||||||
|
|
||||||
|
// Extend the RedisClient prototype to add a custom method
|
||||||
|
// This one converts the results from "INFO" into a JavaScript Object
|
||||||
|
|
||||||
|
redis.RedisClient.prototype.parse_info = function (callback) {
|
||||||
|
this.info(function (err, res) {
|
||||||
|
var lines = res.toString().split("\r\n").sort();
|
||||||
|
var obj = {};
|
||||||
|
lines.forEach(function (line) {
|
||||||
|
var parts = line.split(':');
|
||||||
|
if (parts[1]) {
|
||||||
|
obj[parts[0]] = parts[1];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
callback(obj)
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
client.parse_info(function (info) {
|
||||||
|
console.dir(info);
|
||||||
|
client.quit();
|
||||||
|
});
|
10
index.js
10
index.js
@@ -4,7 +4,8 @@ var net = require("net"),
|
|||||||
sys = require("sys"),
|
sys = require("sys"),
|
||||||
events = require("events"),
|
events = require("events"),
|
||||||
default_port = 6379,
|
default_port = 6379,
|
||||||
default_host = "127.0.0.1";
|
default_host = "127.0.0.1",
|
||||||
|
commands;
|
||||||
|
|
||||||
exports.debug_mode = false;
|
exports.debug_mode = false;
|
||||||
|
|
||||||
@@ -425,6 +426,7 @@ function RedisClient(stream) {
|
|||||||
events.EventEmitter.call(this);
|
events.EventEmitter.call(this);
|
||||||
}
|
}
|
||||||
sys.inherits(RedisClient, events.EventEmitter);
|
sys.inherits(RedisClient, events.EventEmitter);
|
||||||
|
exports.RedisClient = RedisClient;
|
||||||
|
|
||||||
RedisClient.prototype.connection_gone = function (why) {
|
RedisClient.prototype.connection_gone = function (why) {
|
||||||
var self = this;
|
var self = this;
|
||||||
@@ -663,7 +665,7 @@ RedisClient.prototype.end = function () {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// http://code.google.com/p/redis/wiki/CommandReference
|
// http://code.google.com/p/redis/wiki/CommandReference
|
||||||
exports.commands = [
|
commands = [
|
||||||
// Connection handling
|
// Connection handling
|
||||||
"QUIT", "AUTH",
|
"QUIT", "AUTH",
|
||||||
// Commands operating on all value types
|
// Commands operating on all value types
|
||||||
@@ -693,7 +695,7 @@ exports.commands = [
|
|||||||
"PING",
|
"PING",
|
||||||
];
|
];
|
||||||
|
|
||||||
exports.commands.forEach(function (command) {
|
commands.forEach(function (command) {
|
||||||
RedisClient.prototype[command] = function () {
|
RedisClient.prototype[command] = function () {
|
||||||
var args = to_array(arguments);
|
var args = to_array(arguments);
|
||||||
args.unshift(command); // put command at the beginning
|
args.unshift(command); // put command at the beginning
|
||||||
@@ -710,7 +712,7 @@ function Multi(client, args) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.commands.forEach(function (command) {
|
commands.forEach(function (command) {
|
||||||
Multi.prototype[command.toLowerCase()] = function () {
|
Multi.prototype[command.toLowerCase()] = function () {
|
||||||
var args = to_array(arguments);
|
var args = to_array(arguments);
|
||||||
args.unshift(command);
|
args.unshift(command);
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
{ "name" : "redis",
|
{ "name" : "redis",
|
||||||
"version" : "0.3.3",
|
"version" : "0.3.4",
|
||||||
"description" : "Redis client library",
|
"description" : "Redis client library",
|
||||||
"author": "Matt Ranney <mjr@ranney.com>",
|
"author": "Matt Ranney <mjr@ranney.com>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
Reference in New Issue
Block a user