File tree Expand file tree Collapse file tree 2 files changed +22
-18
lines changed Expand file tree Collapse file tree 2 files changed +22
-18
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -5,16 +5,35 @@ import { createClient } from 'redis';
5
5
const client = createClient ( ) ;
6
6
await client . connect ( ) ;
7
7
8
+ const setName = "user1:favorites" ;
9
+
8
10
// if you try to add any data of type other than string you will get an error saying `Invalid argument type`
9
11
// so before adding make sure the value is of type string or convert it using toString() method
10
12
// https://redis.io/commands/sadd/
11
- await client . SADD ( "user1:favorites" , "1" ) ;
13
+ await client . SADD ( setName , "1" ) ;
12
14
13
15
// retrieve values of the set defined
14
16
// https://redis.io/commands/smembers/
15
- const favorites = await client . SMEMBERS ( "user1:favorites" ) ;
17
+ const favorites = await client . SMEMBERS ( setName ) ;
16
18
for ( const favorite of favorites ) {
17
- console . log ( favorite ) ;
19
+ console . log ( favorite ) ;
20
+ }
21
+
22
+ // alternate way to retrieve data from set
23
+ for await ( const member of client . sScanIterator ( setName ) ) {
24
+ console . log ( member ) ;
18
25
}
19
26
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
+
20
39
await client . quit ( ) ;
You can’t perform that action at this time.
0 commit comments