Skip to content

Commit 59d2e9c

Browse files
authored
Update set.js
1 parent f3e64d8 commit 59d2e9c

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

examples/set.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
1-
// An example explaining how to add values to a set and how to retrive them
1+
// An example explaining how to add values to a set and how to retrieve them
22

33
import { createClient } from 'redis';
44

55
const client = createClient();
66
await client.connect();
77

8-
const setName = "user1:favorites";
8+
const SET_NAME = 'user1:favorites';
99

10-
// if you try to add any data of type other than string you will get an error saying `Invalid argument type`
11-
// so before adding make sure the value is of type string or convert it using toString() method
10+
// add an item to the set
11+
// set members can only be `string | Buffer` so make sure to convert values if needed,
1212
// https://redis.io/commands/sadd/
13-
await client.SADD(setName, "1");
13+
await client.SADD(SET_NAME, '1');
1414

15-
// retrieve values of the set defined
15+
// retrieve all the members of the set in one batch (be careful using it with "large" sets)
1616
// https://redis.io/commands/smembers/
17-
const favorites = await client.SMEMBERS(setName);
17+
const favorites = await client.SMEMBERS(SET_NAME);
1818
for (const favorite of favorites) {
1919
console.log(favorite);
2020
}
2121

22-
// alternate way to retrieve data from set
22+
// retrieve all the members of the set in batches using SSCAN (useful with large sets)
23+
// https://redis.io/commands/sscan/
2324
for await (const member of client.sScanIterator(setName)) {
2425
console.log(member);
2526
}
2627

27-
// another alternate way to retrieve values from the set
28-
// https://redis.io/commands/sscan/
29-
let iCursor = 0;
30-
do {
31-
const { cursor, members } = await client.SSCAN(setName, iCursor);
32-
members.forEach((member) => {
33-
console.log(member);
34-
});
35-
iCursor = cursor;
36-
} while (iCursor !== 0)
37-
38-
3928
await client.quit();

0 commit comments

Comments
 (0)