node_redis
node_redis is a modern, high-performance Redis client for Node.js. It has over 3,658,902 Weekly Downloads and has in-build support for all Redis commands. it uses the raw Redis command with all capital letters as well as a camel-cased version of these commands.
Examples:
// Redis commands syntex
await client.SET('key', 'field', 'value');
await client.GET('key');
// camel cased commands
await client.rPush('key', 'value');
await client.lRange('key', from, to);
Installing node_redis in Javascript
npm i redis
Javascript Datatype Mapping with Redis Type
Javascript Datatype | Redis Type |
---|---|
String | string |
Array of String | list |
Array of String | set |
Integer | number |
String | float |
Object | hash |
Redis Command Using node_redis
Hash Commands
- HSET: Sets the string value of a hash field.
Redis Example
HSET id key "value"
Output
OK
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HSET('id', 'key', 'value').then((res) => {
console.log('Set key value : ', res);
client.quit();
});
Output
Redis Client Connected
Set key value : 0
- HGET: Gets the value of a hash field stored at the specified key.
Redis Example
HGET id key
Output
value
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.get('key').then((res) => {
console.log('Get key value : ', res);
client.quit();
});
Output
Redis Client Connected
Get key value : value
- HMGET: Gets the values of all the given hash fields.
Redis Example
HMGET id key1 key2
Output
1) "value"
2) "value1"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HMGET('id', ['key1', 'key2']).then((res) => {
console.log('Get key value : ', res);
client.quit();
});
Output
Redis Client Connected
Get key value : ['hello', 'world']
- HMSET: Sets multiple hash fields to multiple values.
Redis Example
HMSET id key1 "Hello" key2 "World"
Output
OK
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HSET('id', ['key1', 'hello', 'key2', 'world']).then((res) => {
console.log('Set key value : ', res);
client.quit();
});
Output
Redis Client Connected
Set key value : 1
- HDEL: Deletes one or more hash fields.
Redis Example
HDEL id key1
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HDEL('id', 'key1').then((res) => {
console.log('Deleted key1 : ', res);
});
Output
Redis Client Connected
Deleted key1 : 1
- HEXISTS: Determines whether a hash field exists or not.
Redis Example
HEXISTS id key1
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HEXISTS('id', 'key1').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
false
- HGETALL: Gets all the fields and values Stored in a hash.
Redis Example
HGETALL id key1
Output
1) "key"
2) "value"
3) "key2"
4) "world"
5) "numkey"
6) "10"
7) "floatkey"
8) "10.2"
9) "key1"
10) "value1"
11) "key11"
12) "value1"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HGETALL('id').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
[Object: null prototype] { key: 'value', key2: 'world' }
- HINCRBY: Increments the integer value of a hash field by the given number.
Redis Example
HINCRBY id numkey 3
Output
6
HINCRBY id numkey 3
Output
9
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HSET('id', 'numkey', 9).then((res) => {
console.log('set numkey', res);
});
client.HINCRBY('id', 'numkey', 1).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
set numkey 1
10
- HINCRBYFLOAT: Increments the float value of a hash field by the given amount.
Redis Example
HINCRBYFLOAT id floatkey 0.5
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HSET('id', 'floatkey', 9.1).then((res) => {
console.log('set floatkey', res);
});
client.HINCRBYFLOAT('id', 'floatkey', 1.1).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
set floatkey 1
10.2
- HKEYS: Gets all the fields in a hash.
Redis Example
HKEYS id
Output
1) "key"
2) "key2"
3) "numkey"
4) "floatkey"
5) "key1"
6) "key11"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HKEYS('id').then((keys) => {
console.log(keys);
client.quit();
});
Output
Redis Client Connected
['key', 'key2', 'numkey', 'floatkey', 'key1', 'key11']
- HLEN: Gets the number of fields in a hash.
Redis Example
HLEN id
Output
4
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HLEN('id').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
4
- HSETNX: Sets the value of a hash field, only if the field does not exist.
Redis Example
HSETNX id key1 value1
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HSETNX('id', 'key1', 'value1').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
true
- HVALS: Gets all the values in a hash
Redis Example
HVALS id
Output
1) "value"
2) "world"
3) "10"
4) "10.2"
5) "value1"
6) "value1"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HVALS('id').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
['value', 'world', '10', '10.2', 'value1', 'value1']
- HSCAN: Incrementally iterates hash fields and associated values.
Redis Example
HSCAN id curser
HSCAN id 0
Output
1) "0"
2) 1) "key"
2) "value"
3) "key2"
4) "world"
5) "numkey"
6) "10"
7) "floatkey"
8) "10.2"
9) "key1"
10) "value1"
11) "key11"
12) "value1"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.HSCAN('id', 0).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
{
cursor: 0,
tuples: [
{ field: 'key', value: 'value' },
{ field: 'key2', value: 'world' },
{ field: 'numkey', value: '10' },
{ field: 'floatkey', value: '10.2' },
{ field: 'key1', value: 'value1' },
{ field: 'key11', value: 'value1' }
]
}
List commands
- BLPOP: It is the blocking version of LPOP as it Removes and gets the first element in a list, or blocks until one is available
Redis Example
BLPOP list1 list2 timeout
Output
1) "list1"
2) "a"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.BLPOP('mylist', 2).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
{ key: 'mylist', element: 'three' }
- BRPOP: Removes and gets the last element in a list, or blocks until one is available
Redis Example
BRPOP list1 list2 timeout
Output
1) "list1"
2) "hello"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.BRPOP('mylist', 1).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
null
- LINDEX: Gets an element from a list by its index
Redis Example
LINDEX mylist position
LINDEX mylist 0
Output
"hello"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LINDEX('mylist', 0).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
nice
- LINSERT: Insert an element before or after another element in a list
Redis Example
LINSERT mylist BEFORE "World" "There"
Output
3
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LINSERT('mylist', 'BEFORE', 'nice', 'three').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
4
- LLEN: Gets the length of a list
Redis Example
LLEN mylist
Output
2
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LLEN('mylist').then((length) => {
console.log(length);
client.quit();
});
Output
Redis Client Connected
4
- LPOP: Removes and gets the first element in a list
Redis Example
LPOP mylist
Output
"three"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LPOP('mylist').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
nice
- LPUSH: Prepends one or multiple values to a list
Redis Example
LPUSH mylist "hello"
Output
7
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LPUSH('mylist', 'one').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
3
- LPUSHX: Prepends a value to a list, only if the list exists
Redis Example
LPUSHX mylist "Hello"
Output
2
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LPUSHX('mylist', 'value1').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
4
- LRANGE: Gets a range of elements from a list
Redis Example
LRANGE mylist -3 2
Output
1) "one"
2) "two"
3) "three"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LRANGE('mylist', 0, -1).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
['value1', 'one', 'world', 'hello']
- LREM: Removes elements from a list
Redis Example
LREM mylist -2 "hello"
Output
2
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LREM('mylist', 0, 'hello').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
1
- LSET: Sets the value of an element in a list by its index
Redis Example
LSET mylist 0 "four"
Output
OK
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LSET('mylist', 0, 'Hello').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
OK
- LTRIM: Trims a list to the specified range
Redis Example
LTRIM mylist 1 -1
Output
OK
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.LTRIM('mylist', 1, -1).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
OK
- RPOP: Removes and gets the last element in a list
Redis Example
RPOP mylist
Output
hello
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.RPOP('mylist').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
hello
- RPOPLPUSH: Removes the last element in a list, appends it to another list, and returns it
Redis Example
RPOPLPUSH mylist myotherlist
Output
world
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.RPOPLPUSH('mylist', 'myotherlist').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
world
- RPUSH: Appends one or multiple values to a list
Redis Example
RPUSH mylist "hello"
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.RPUSH('mylist', 'hello').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
1
- RPUSHX: Appends a value to a list, only if the list exists and returns the length of the list
Redis Example
RPUSHX mylist "world"
Output
2
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.RPUSHX('mylist', 'world').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
2
Set Commands
- SADD: Adds one or more members to a set
Redis Example
SADD myset "Hello"
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SADD('myset', 'Hello').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
1
- SCARD: Gets the number of members in a set
Redis Example
SCARD myset
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SCARD('myset').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
1
- SDIFF: Subtracts multiple sets
Redis Example
SDIFF key1 key2
Output
1) "a"
2) "b"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SADD('key1', 'a', 'b', 'c', 'e').then((res) => {
console.log(res);
client.SADD('key2', 'a', 'b', 'd').then((res) => {
console.log(res);
client.SDIFF('key1', 'key2').then((res) => {
console.log(res);
client.quit();
});
});
});
Output
1
1
['a']
- SDIFFSTORE: Subtracts multiple sets and stores the resulting set in a key
Redis Example
SDIFFSTORE key1 key2
Output
2
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SADD('key1', 'a', 'b', 'c', 'e').then((res) => {
console.log(res);
client.SADD('key2', 'a', 'b', 'd').then((res) => {
console.log(res);
client.SDIFFSTORE('key1', 'key2').then((res) => {
console.log(res);
client.quit();
});
});
});
Output
Redis Client Connected
1
1
1
- SINTER: Intersects multiple sets
Redis Example
SINTER key1 key2
Output
1
1
['a']
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SADD('key1', 'a', 'b', 'c', 'e').then((res) => {
console.log(res);
client.SADD('key2', 'a', 'b', 'd').then((res) => {
console.log(res);
client.SINTER('key1', 'key2').then((res) => {
console.log(res);
client.quit();
});
});
});
Output
Redis Client Connected
1
1
['a']
- SINTERSTORE: Intersects multiple sets and stores the resulting set in a key
Redis Example
SINTERSTORE key key1 key2
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SADD('key1', 'a', 'b', 'c', 'e').then((res) => {
console.log(res);
client.SADD('key2', 'a', 'b', 'd').then((res) => {
console.log(res);
client.SINTERSTORE('key1', 'key2').then((res) => {
console.log(res);
client.quit();
});
});
});
Output
Redis Client Connected
1
1
1
- SISMEMBER: Determines if a given value is a member of a set
Redis Example
SISMEMBER myset "one"
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SISMEMBER('myset', 'one').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
false
- SMEMBERS: Gets all the members in a set
Redis Example
SMEMBERS myset
Output
1) "Hello"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SMEMBERS('myset').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
['Hello']
- SMOVE: Moves a member from one set to another
Redis Example
SMOVE myset myotherset "two"
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SADD('key6', 'a', 'b', 'c', 'e').then((res) => {
console.log(res);
client.SADD('key5', 'a', 'b', 'd', 'x').then((res) => {
console.log(res);
client.SMOVE('key5', 'key6', 'e').then((res) => {
console.log(res);
client.quit();
});
});
});
Output
1
1
true
- SPOP: Removes and returns a random member from a set
Redis Example
SPOP myset
Output
three
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SPOP('myset').then((reply) => {
console.log(reply);
client.quit();
});
Output
Redis Client Connected
Hello
- SRANDMEMBER: Gets one or multiple random members from a set
Redis Example
SRANDMEMBER myset -5
Output
1) "s"
2) "w"
3) "s"
4) "a"
5) "a"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SRANDMEMBER('myset', -5).then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
a
- SREM: Removes one or more members from a set
Redis Example
SREM myset "a"
Output
1
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SREM('myset', 'a').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
1
- SUNION: Adds multiple sets
Redis Example
SUNION key1 key2
Output
1) "a"
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SUNION('key1', 'key2').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
['a']
- SUNIONSTORE: Adds multiple sets and stores the resulting set in a key
Redis Example
SUNIONSTORE key key1 key2
Output
5
JavaScript Example
const redis = require('redis');
const client = redis.createClient();
client.on('error', (err) => console.log('Redis Client Error', err));
client.on('connect', () => console.log('Redis Client Connected'));
client.connect();
client.SUNIONSTORE('key', 'key1', 'key2').then((res) => {
console.log(res);
client.quit();
});
Output
Redis Client Connected
1
Top comments (0)