lib/node-redis-dump.js
function(key, data, callback) {
	'use strict';

	/**
	 * Read scores by values.
	 *
	 * @param {Array} values
	 * @param {Function} callback
	 */
	var ReadScores = function(values, callback) {
		var result = [];

		/**
		 * Get scores recursive.
		 */
		var GetRecursive = function() {
			if (!values.length) {
				callback(null, result);
				return;
			}

			var value = values.pop();

			this.getClient().zscore(key, value, function(err, score) {
				if (err) {
					callback(err);
					return;
				}

				result.push(score);
				GetRecursive();
			});
		}.bind(this);

		GetRecursive();
	}.bind(this);

	/**
	 * Read key.
	 *
	 * @param {String} key
	 * @param {String} type
	 * @param {Function} rkCallback
	 */
	var ReadKey = function(key, type, rkCallback) {
		var params = [ key ],
			command = {
				set: 'smembers',
				zset: 'zrange',
				list: 'lrange',
				hash: 'hgetall'
			}[ type ] || 'get';

		if (command.indexOf('range') !== -1) {
			params.push(0);
			params.push(-1);
		}

		params.push(function(err, values) {
			if (err) {
				rkCallback(err);
				return;
			}

			switch (type) {
				case 'zset':
					ReadScores(_.clone(values).reverse(), function(err, scores) {
						rkCallback(null, _.zip(scores, values));
					});
					break;

				default:
					rkCallback(null, values);
					break;
			}
		});

		this.getClient()[ command ].apply(this.getClient(), params);
	}.bind(this);


	switch (this.getExportParams().type) {
		//	Export as redis type.
		case 'redis':
			return function(err, type) {
				var type2PrintSetCommand = {
					string: 'SET',
					set: 'SADD',
					zset: 'ZADD',
					list: 'RPUSH',
					hash: 'HSET'
				};

				if (!data) {
					data = '';
				}

				ReadKey(key, type, function(err, value) {
					if (err) {
						callback(err);
						return;
					}

					var command = type2PrintSetCommand[ type ];

					key = key.trim();

					switch (type) {
						case 'set':
							_.each(value, function(item) {
								data += command + ' "' + key + '" "' + item + "\"\n";
							});
							break;

						case 'zset':
							_.each(value, function(item) {
								data += command + ' "' + key + '" ' + item[0] + ' "' + item[1] + "\"\n";
							});
							break;

						case 'hash':
							_.each(_.pairs(value), function(item) {
								data += command + ' "' + key + '" "' + item[0] + '" "' + item[1] + "\"\n";
							});
							break;

						default:
							data += command + ' "' + key + '" "' + value + "\"\n";
							break;
					}

					callback(null, data);
				});
			};

		//	Export as json type.
		case 'json':
			return function(err, type) {
				if (!data) {
					data = {};
				}

				ReadKey(key, type, function(err, value) {
					if (err) {
						callback(err);
						return;
					}

					switch (type) {
						case 'zset':
							var withoutScores = [];
							_.each(value, function(item) {
								withoutScores.push(item[1]);
							});
							value = withoutScores;
							break;
					}

					data[ key.trim() ] = value;

					callback(null, data);
				});
			};
	}
}
function(key, data, callback) {
	'use strict';

	/**
	 * Read scores by values.
	 *
	 * @param {Array} values
	 * @param {Function} callback
	 */
	var ReadScores = function(values, callback) {
		var result = [];

		/**
		 * Get scores recursive.
		 */
		var GetRecursive = function() {
			if (!values.length) {
				callback(null, result);
				return;
			}

			var value = values.pop();

			this.getClient().zscore(key, value, function(err, score) {
				if (err) {
					callback(err);
					return;
				}

				result.push(score);
				GetRecursive();
			});
		}.bind(this);

		GetRecursive();
	}.bind(this);

	/**
	 * Read key.
	 *
	 * @param {String} key
	 * @param {String} type
	 * @param {Function} rkCallback
	 */
	var ReadKey = function(key, type, rkCallback) {
		var params = [ key ],
			command = {
				set: 'smembers',
				zset: 'zrange',
				list: 'lrange',
				hash: 'hgetall'
			}[ type ] || 'get';

		if (command.indexOf('range') !== -1) {
			params.push(0);
			params.push(-1);
		}

		params.push(function(err, values) {
			if (err) {
				rkCallback(err);
				return;
			}

			switch (type) {
				case 'zset':
					ReadScores(_.clone(values).reverse(), function(err, scores) {
						rkCallback(null, _.zip(scores, values));
					});
					break;

				default:
					rkCallback(null, values);
					break;
			}
		});

		this.getClient()[ command ].apply(this.getClient(), params);
	}.bind(this);


	switch (this.getExportParams().type) {
		//	Export as redis type.
		case 'redis':
			return function(err, type) {
				var type2PrintSetCommand = {
					string: 'SET',
					set: 'SADD',
					zset: 'ZADD',
					list: 'RPUSH',
					hash: 'HSET'
				};

				if (!data) {
					data = '';
				}

				ReadKey(key, type, function(err, value) {
					if (err) {
						callback(err);
						return;
					}

					var command = type2PrintSetCommand[ type ];

					key = key.trim();

					switch (type) {
						case 'set':
							_.each(value, function(item) {
								data += command + ' "' + key + '" "' + item + "\"\n";
							});
							break;

						case 'zset':
							_.each(value, function(item) {
								data += command + ' "' + key + '" ' + item[0] + ' "' + item[1] + "\"\n";
							});
							break;

						case 'hash':
							_.each(_.pairs(value), function(item) {
								data += command + ' "' + key + '" "' + item[0] + '" "' + item[1] + "\"\n";
							});
							break;

						default:
							data += command + ' "' + key + '" "' + value + "\"\n";
							break;
					}

					callback(null, data);
				});
			};

		//	Export as json type.
		case 'json':
			return function(err, type) {
				if (!data) {
					data = {};
				}

				ReadKey(key, type, function(err, value) {
					if (err) {
						callback(err);
						return;
					}

					switch (type) {
						case 'zset':
							var withoutScores = [];
							_.each(value, function(item) {
								withoutScores.push(item[1]);
							});
							value = withoutScores;
							break;
					}

					data[ key.trim() ] = value;

					callback(null, data);
				});
			};
	}
}
function ( key , data , callback ) { 'use strict' ; /**
	 * Read scores by values.
	 *
	 * @param {Array} values
	 * @param {Function} callback
	 */ var ReadScores = function ( values , callback ) { var result = [ ] ; /**
		 * Get scores recursive.
		 */ var GetRecursive = function ( ) { if ( ! values . length ) { callback ( null , result ) ; return ; } var value = values . pop ( ) ; this . getClient ( ) . zscore ( key , value , function ( err , score ) { if ( err ) { callback ( err ) ; return ; } result . push ( score ) ; GetRecursive ( ) ; } ) ; } . bind ( this ) ; GetRecursive ( ) ; } . bind ( this ) ; /**
	 * Read key.
	 *
	 * @param {String} key
	 * @param {String} type
	 * @param {Function} rkCallback
	 */ var ReadKey = function ( key , type , rkCallback ) { var params = [ key ] , command = { set : 'smembers' , zset : 'zrange' , list : 'lrange' , hash : 'hgetall' } [ type ] || 'get' ; if ( command . indexOf ( 'range' ) !== - 1 ) { params . push ( 0 ) ; params . push ( - 1 ) ; } params . push ( function ( err , values ) { if ( err ) { rkCallback ( err ) ; return ; } switch ( type ) { case 'zset' : ReadScores ( _ . clone ( values ) . reverse ( ) , function ( err , scores ) { rkCallback ( null , _ . zip ( scores , values ) ) ; } ) ; break ; default : rkCallback ( null , values ) ; break ; } } ) ; this . getClient ( ) [ command ] . apply ( this . getClient ( ) , params ) ; } . bind ( this ) ; switch ( this . getExportParams ( ) . type ) { //	Export as redis type. case 'redis' : return function ( err , type ) { var type2PrintSetCommand = { string : 'SET' , set : 'SADD' , zset : 'ZADD' , list : 'RPUSH' , hash : 'HSET' } ; if ( ! data ) { data = '' ; } ReadKey ( key , type , function ( err , value ) { if ( err ) { callback ( err ) ; return ; } var command = type2PrintSetCommand [ type ] ; key = key . trim ( ) ; switch ( type ) { case 'set' : _ . each ( value , function ( item ) { data += command + ' "' + key + '" "' + item + "\"\n" ; } ) ; break ; case 'zset' : _ . each ( value , function ( item ) { data += command + ' "' + key + '" ' + item [ 0 ] + ' "' + item [ 1 ] + "\"\n" ; } ) ; break ; case 'hash' : _ . each ( _ . pairs ( value ) , function ( item ) { data += command + ' "' + key + '" "' + item [ 0 ] + '" "' + item [ 1 ] + "\"\n" ; } ) ; break ; default : data += command + ' "' + key + '" "' + value + "\"\n" ; break ; } callback ( null , data ) ; } ) ; } ; //	Export as json type. case 'json' : return function ( err , type ) { if ( ! data ) { data = { } ; } ReadKey ( key , type , function ( err , value ) { if ( err ) { callback ( err ) ; return ; } switch ( type ) { case 'zset' : var withoutScores = [ ] ; _ . each ( value , function ( item ) { withoutScores . push ( item [ 1 ] ) ; } ) ; value = withoutScores ; break ; } data [ key . trim ( ) ] = value ; callback ( null , data ) ; } ) ; } ; } }
Read key callback by type.
Read key callback by type .