DEV Community

vinayak
vinayak

Posted on • Originally published at thebackendblog.com on

Basic Operations with node_redis

Image description

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);

Enter fullscreen mode Exit fullscreen mode

Installing node_redis in Javascript

npm i redis

Enter fullscreen mode Exit fullscreen mode

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

1529926.png


Hash Commands

  • HSET: Sets the string value of a hash field.

Redis Example

HSET id key "value"

Enter fullscreen mode Exit fullscreen mode

Output

OK

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
Set key value : 0

Enter fullscreen mode Exit fullscreen mode
  • HGET: Gets the value of a hash field stored at the specified key.

Redis Example

HGET id key

Enter fullscreen mode Exit fullscreen mode

Output

value

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
Get key value : value

Enter fullscreen mode Exit fullscreen mode
  • HMGET: Gets the values of all the given hash fields.

Redis Example

HMGET id key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

1) "value"
2) "value1"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
Get key value : ['hello', 'world']

Enter fullscreen mode Exit fullscreen mode
  • HMSET: Sets multiple hash fields to multiple values.

Redis Example

HMSET id key1 "Hello" key2 "World"

Enter fullscreen mode Exit fullscreen mode

Output

OK

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
Set key value : 1

Enter fullscreen mode Exit fullscreen mode
  • HDEL: Deletes one or more hash fields.

Redis Example

HDEL id key1

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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);
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
Deleted key1 : 1

Enter fullscreen mode Exit fullscreen mode
  • HEXISTS: Determines whether a hash field exists or not.

Redis Example

HEXISTS id key1

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
false

Enter fullscreen mode Exit fullscreen mode
  • HGETALL: Gets all the fields and values Stored in a hash.

Redis Example

HGETALL id key1

Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
[Object: null prototype] { key: 'value', key2: 'world' }

Enter fullscreen mode Exit fullscreen mode
  • HINCRBY: Increments the integer value of a hash field by the given number.

Redis Example

HINCRBY id numkey 3

Enter fullscreen mode Exit fullscreen mode

Output

6


HINCRBY id numkey 3

Enter fullscreen mode Exit fullscreen mode

Output

9

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
set numkey 1
10

Enter fullscreen mode Exit fullscreen mode
  • HINCRBYFLOAT: Increments the float value of a hash field by the given amount.

Redis Example

HINCRBYFLOAT id floatkey 0.5

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
set floatkey 1
10.2

Enter fullscreen mode Exit fullscreen mode
  • HKEYS: Gets all the fields in a hash.

Redis Example

HKEYS id

Enter fullscreen mode Exit fullscreen mode

Output

1) "key"
2) "key2"
3) "numkey"
4) "floatkey"
5) "key1"
6) "key11"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
['key', 'key2', 'numkey', 'floatkey', 'key1', 'key11']

Enter fullscreen mode Exit fullscreen mode
  • HLEN: Gets the number of fields in a hash.

Redis Example

HLEN id

Enter fullscreen mode Exit fullscreen mode

Output

4

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
4

Enter fullscreen mode Exit fullscreen mode
  • HSETNX: Sets the value of a hash field, only if the field does not exist.

Redis Example

HSETNX id key1 value1

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
true

Enter fullscreen mode Exit fullscreen mode
  • HVALS: Gets all the values in a hash

Redis Example

HVALS id

Enter fullscreen mode Exit fullscreen mode

Output

1) "value"
2) "world"
3) "10"
4) "10.2"
5) "value1"
6) "value1"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
['value', 'world', '10', '10.2', 'value1', 'value1']

Enter fullscreen mode Exit fullscreen mode
  • HSCAN: Incrementally iterates hash fields and associated values.

Redis Example

HSCAN id curser


HSCAN id 0

Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

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' }
  ]
}

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

Output

1) "list1"
2) "a"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
{ key: 'mylist', element: 'three' }

Enter fullscreen mode Exit fullscreen mode
  • BRPOP: Removes and gets the last element in a list, or blocks until one is available

Redis Example

BRPOP list1 list2 timeout

Enter fullscreen mode Exit fullscreen mode

Output

1) "list1"
2) "hello"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
null

Enter fullscreen mode Exit fullscreen mode
  • LINDEX: Gets an element from a list by its index

Redis Example

LINDEX mylist position


LINDEX mylist 0

Enter fullscreen mode Exit fullscreen mode

Output

"hello"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
nice

Enter fullscreen mode Exit fullscreen mode
  • LINSERT: Insert an element before or after another element in a list

Redis Example

LINSERT mylist BEFORE "World" "There"

Enter fullscreen mode Exit fullscreen mode

Output

3

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
4

Enter fullscreen mode Exit fullscreen mode
  • LLEN: Gets the length of a list

Redis Example

LLEN mylist

Enter fullscreen mode Exit fullscreen mode

Output

2

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
4

Enter fullscreen mode Exit fullscreen mode
  • LPOP: Removes and gets the first element in a list

Redis Example

LPOP mylist

Enter fullscreen mode Exit fullscreen mode

Output

"three"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
nice

Enter fullscreen mode Exit fullscreen mode
  • LPUSH: Prepends one or multiple values to a list

Redis Example

LPUSH mylist "hello"

Enter fullscreen mode Exit fullscreen mode

Output

7

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
3

Enter fullscreen mode Exit fullscreen mode
  • LPUSHX: Prepends a value to a list, only if the list exists

Redis Example

LPUSHX mylist "Hello"

Enter fullscreen mode Exit fullscreen mode

Output

2

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
4

Enter fullscreen mode Exit fullscreen mode
  • LRANGE: Gets a range of elements from a list

Redis Example

LRANGE mylist -3 2

Enter fullscreen mode Exit fullscreen mode

Output

1) "one"
2) "two"
3) "three"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
['value1', 'one', 'world', 'hello']

Enter fullscreen mode Exit fullscreen mode
  • LREM: Removes elements from a list

Redis Example

LREM mylist -2 "hello"

Enter fullscreen mode Exit fullscreen mode

Output

2

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1

Enter fullscreen mode Exit fullscreen mode
  • LSET: Sets the value of an element in a list by its index

Redis Example

LSET mylist 0 "four"

Enter fullscreen mode Exit fullscreen mode

Output

OK

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
OK

Enter fullscreen mode Exit fullscreen mode
  • LTRIM: Trims a list to the specified range

Redis Example

 LTRIM mylist 1 -1

Enter fullscreen mode Exit fullscreen mode

Output

OK

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
OK

Enter fullscreen mode Exit fullscreen mode
  • RPOP: Removes and gets the last element in a list

Redis Example

 RPOP mylist

Enter fullscreen mode Exit fullscreen mode

Output

hello

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
hello

Enter fullscreen mode Exit fullscreen mode
  • RPOPLPUSH: Removes the last element in a list, appends it to another list, and returns it

Redis Example

 RPOPLPUSH mylist myotherlist

Enter fullscreen mode Exit fullscreen mode

Output

world

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
world

Enter fullscreen mode Exit fullscreen mode
  • RPUSH: Appends one or multiple values to a list

Redis Example

RPUSH mylist "hello"

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1

Enter fullscreen mode Exit fullscreen mode
  • RPUSHX: Appends a value to a list, only if the list exists and returns the length of the list

Redis Example

RPUSHX mylist "world"

Enter fullscreen mode Exit fullscreen mode

Output

2

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
2

Enter fullscreen mode Exit fullscreen mode

Set Commands

  • SADD: Adds one or more members to a set

Redis Example

SADD myset "Hello"

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1

Enter fullscreen mode Exit fullscreen mode
  • SCARD: Gets the number of members in a set

Redis Example

SCARD myset

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1

Enter fullscreen mode Exit fullscreen mode
  • SDIFF: Subtracts multiple sets

Redis Example

SDIFF key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

1) "a"
2) "b"

Enter fullscreen mode Exit fullscreen mode

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();
    });
  });
});

Enter fullscreen mode Exit fullscreen mode

Output

1
1
['a']

Enter fullscreen mode Exit fullscreen mode
  • SDIFFSTORE: Subtracts multiple sets and stores the resulting set in a key

Redis Example

SDIFFSTORE key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

2

Enter fullscreen mode Exit fullscreen mode

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();
    });
  });
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1
1
1

Enter fullscreen mode Exit fullscreen mode
  • SINTER: Intersects multiple sets

Redis Example

SINTER key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

1
1
['a']

Enter fullscreen mode Exit fullscreen mode

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();
    });
  });
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1
1
['a']

Enter fullscreen mode Exit fullscreen mode
  • SINTERSTORE: Intersects multiple sets and stores the resulting set in a key

Redis Example

SINTERSTORE key key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
    });
  });
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1
1
1

Enter fullscreen mode Exit fullscreen mode
  • SISMEMBER: Determines if a given value is a member of a set

Redis Example

SISMEMBER myset "one"

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
false

Enter fullscreen mode Exit fullscreen mode
  • SMEMBERS: Gets all the members in a set

Redis Example

SMEMBERS myset

Enter fullscreen mode Exit fullscreen mode

Output

1) "Hello"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
['Hello']

Enter fullscreen mode Exit fullscreen mode
  • SMOVE: Moves a member from one set to another

Redis Example

 SMOVE myset myotherset "two"

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
    });
  });
});

Enter fullscreen mode Exit fullscreen mode

Output

1
1
true

Enter fullscreen mode Exit fullscreen mode
  • SPOP: Removes and returns a random member from a set

Redis Example

SPOP myset

Enter fullscreen mode Exit fullscreen mode

Output

three

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
Hello

Enter fullscreen mode Exit fullscreen mode
  • SRANDMEMBER: Gets one or multiple random members from a set

Redis Example

SRANDMEMBER myset -5

Enter fullscreen mode Exit fullscreen mode

Output

1) "s"
2) "w"
3) "s"
4) "a"
5) "a"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
a

Enter fullscreen mode Exit fullscreen mode
  • SREM: Removes one or more members from a set

Redis Example

SREM myset "a"

Enter fullscreen mode Exit fullscreen mode

Output

1

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1

Enter fullscreen mode Exit fullscreen mode
  • SUNION: Adds multiple sets

Redis Example

SUNION key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

1) "a"

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
['a']

Enter fullscreen mode Exit fullscreen mode
  • SUNIONSTORE: Adds multiple sets and stores the resulting set in a key

Redis Example

SUNIONSTORE key key1 key2

Enter fullscreen mode Exit fullscreen mode

Output

5

Enter fullscreen mode Exit fullscreen mode

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();
});

Enter fullscreen mode Exit fullscreen mode

Output

Redis Client Connected
1

Enter fullscreen mode Exit fullscreen mode

Latest comments (0)