$ npm install redis
:warning: Version 4 is still under development and isn't ready for production use. Use at your own risk.
npm install redis@next
import { createClient } from 'redis';
(async () => {
const client = createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
await client.connect();
await client.set('key', 'value');
const value = await client.get('key');
})();
The new interface is clean and cool, but if you have an existing code base, you might want to enable legacy mode.
There is built-in support for all of the out-of-the-box Redis commands. They are exposed using the raw Redis command names (HGET
, HSET
, etc.) and a friendlier camel-cased version (hGet
, hSet
, etc.):
// raw Redis commands
await client.SET('key', 'value');
await client.GET('key');
// friendly JavaScript commands
await client.hSet('key', 'field', 'value');
await client.hGetAll('key');
Modifiers to commands are specified using a JavaScript object:
await client.set('key', 'value', {
EX: 10,
NX: true
});
Replies will be transformed to useful data structures:
await client.hGetAll('key'); // { key1: 'value1', key2: 'value2' }
await client.hKeys('key'); // ['key1', 'key2']
If you want to run commands and arguments that Node Redis doesn't know about (yet!) you can use .sendCommand
:
await client.sendCommand(['SET', 'key', 'value', 'NX']); // 'OK'
await client.sendCommand(['HGETALL', 'key']); // ['key1', 'field1', 'key2', 'field2']
Start transactions with a call to .multi()
and then chain your commands. At the end call .exec()
and you'll get an array back with the results of your commands:
const [ setKeyReply, otherKeyValue ] = await client.multi()
.set('key', 'value')
.get('other-key')
.exec()
]); // ['OK', null]
You can also watch keys by calling .watch()
. If someone else changes them out from underneath you, your transaction will abort:
await client.watch('other-key');
try {
await client.multi()
.set('key', 'value')
.get('other-key')
.exec()
]);
} catch (err) {
// ...
}
Any command can be run on a new connection by specifying the duplicateConnection
option. The newly created connection is closed when the command's Promise
is fulfilled.
This pattern works especially well for blocking commands—such as BLPOP
and BLMOVE
:
const blPopPromise = client.blPop(
client.commandOptions({ duplicateConnection: true }),
'key'
);
await client.lPush('key', ['1', '2']);
await blPopPromise; // '2'
Subscribing to a channel requires a dedicated Redis connection and is easily handled using events:
await subscriber.subscribe('channel', message => {
console.log(message); // 'message'
});
await subscriber.pSubscribe('channe*', (message, channel) => {
console.log(message, channel); // 'message', 'channel'
});
await publisher.publish('channel', 'message');
SCAN
can easily be looped over using async iterators:
for await (const key of client.scanIterator()) {
// use the key!
await client.get(key);
}
This works with HSCAN, SSCAN, and ZSCAN too:
for await (const member of client.hScanIterator('hash')) {}
for await (const { field, value } of client.sScanIterator('set')) {}
for await (const { member, score } of client.zScanIterator('sorted-set')) {}
You can override the default options by just passing them in:
client.scanIterator({
TYPE: 'string', // `SCAN` only
MATCH: 'patter*',
COUNT: 100
});
You can define Lua scripts to create efficient custom commands:
import { createClient } from 'redis';
import { defineScript } from 'redis/dist/lib/lua-script';
(async () => {
const client = createClient({
scripts: {
add: defineScript({
NUMBER_OF_KEYS: 1,
SCRIPT:
'local val = redis.pcall("GET", KEYS[1]);' +
'return val + ARGV[1];',
transformArguments(key: string, toAdd: number): Array<string> {
return [key, number.toString()];
},
transformReply(reply: number): number {
return reply;
}
})
}
});
await client.connect();
await client.set('key', '1');
await client.add('key', 2); // 3
})();
Connecting to a cluster is a bit different. Create the client by specifying the root nodes in your cluster and then use it like a non-clustered client:
import { createCluster } from 'redis';
(async () => {
const cluster = createCluster({
rootNodes: [{
host: '192.168.1.1',
port: 30001
}, {
host: '192.168.1.2',
port: 30002
}]
});
cluster.on('error', (err) => console.log('Redis Cluster Error', err));
await cluster.connect();
await cluster.set('key', 'value');
const value = await cluster.get('key');
})();
Need to use the new client in an existing codebase? You can use legacy mode to preserve backwards compatibility while still getting access to the updated experience:
const client = createClient({
legacyMode: true
});
// legacy mode
client.set('key', 'value', 'NX', (err, reply) => {
// ...
});
// version 4 interface is still accessible
await client.v4.set('key', 'value', {
NX: true
});
If you'd like to contribute, check out the contributing guide.
This repository is licensed under the "MIT" license. See LICENSE.
© 2010 - cnpmjs.org x YWFE | Home | YWFE