Skip to content

Commit fddef4f

Browse files
added example usage of set datastructure
1 parent a7d5bc7 commit fddef4f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

examples/set.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// An example explaining how to add values to a set and how to retrive them
2+
3+
import { createClient } from 'redis';
4+
5+
const client = createClient();
6+
await client.connect();
7+
8+
// if you try to add any data of type other than string you will get an error saying `Invalid argument type`
9+
// so before adding make sure the value is of type string or convert it using toString() method
10+
// https://redis.io/commands/sadd/
11+
await client.SADD("user1:favorites", "1");
12+
13+
// retrieve values of the set defined
14+
// https://redis.io/commands/smembers/
15+
const favorites = await client.SMEMBERS("user1:favorites");
16+
for (const favorite of favorites) {
17+
console.log(favorite);
18+
}
19+
20+
await client.quit();

0 commit comments

Comments
 (0)