A redis library for node.js
Stability: 1 - Experimental
npm install redjs
var client = redjs.createClient();
client.connect(function(err) {
if (err) {
return console.log(err.message);
}
client.call(['SET', 'keyA', 'listA']);
client.call(['GET', 'keyA'], function(err, reply) {
if (err) {
return console.log(err.message);
}
// null is redis's way to say: it does not exist.
if (reply !== null) {
client
.call(['RPUSH', reply, 'some', 'list', 'elements'])
.call(['LRANGE', reply, 0, -1], function(err, reply) {
console.log(err, reply);
});
}
});
});
Create a new Client
object. Client
extends events.EventEmitter
.
The Client is a bare metal network client for redis. It provides only a basic interface for issuing commands, nothing fancy.
opt_options
contains optional configuration with the following defaults:
{ host: '127.0.0.1',
port: 6379,
reconnect: true,
maxReconnectCount: 10,
reconnectInterval: 5000 }
function()
function(channel, message)
function(channel, numSubscriptions)
function(channel, numSubscriptions)
Emitted after trying to reconnect.
function(err)
Emitted when the underlying socket is fully closed.
function()
See Parser
.
function(err)
function(err)
The current client mode, the values correspond with the array indexes of client.modes
.
console.log('mode id = %d name = %s', client.mode, client.modes[client.mode]);
['OFFLINE', 'CONNECT', 'CLOSE', 'NORMAL']
opt_callback
is an optional function(err)
.
Initiate reconnect The client only tries to reconnect when at least one successful connection could be made.
The only use-case is when a client can't establish the initial connection:
client.connect(function(err) {
if (err) {
client.reconnect();
}
});
Send a command to the redis server. Allows for chaining.
client
.call('SET', 'keyA', '1')
.call('KEYS', '*', function(err, reply) {
console.log(err, reply);
});
Or use an Array
as 1st argument
client.call(['DEL', 'keyA', 'keyB', 'keyC'], function(err, reply) {
console.log(reply);
});
opt_callback
is an optional function()
that is registered as 'close' event listener.
Creates a new Parser
object. Parser
extends Stream
.
function(type, value)
type
is one of 43 (status)
, 45 (error)
, 58 (integer)
, 36 (bulk)
or 42 (multi-bulk)
.
value
depends on type.
Emitted when the parser encounters unexpected data.
function(err)
Example err
object:
{ message: 'Expected +, -, :, $ or *',
rx: '',
offset: 12,
bytes: 19,
c: 13 }
make install
make jshint
make test
make test-cov
(The MIT License)
Copyright (c) 2012 Malte-Thorben Bruns <skenqbx@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.