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';
55const client = createClient ( ) ;
66await client . connect ( ) ;
77
8+ const setName = "user1:favorites" ;
9+
810// if you try to add any data of type other than string you will get an error saying `Invalid argument type`
911// so before adding make sure the value is of type string or convert it using toString() method
1012// https://redis.io/commands/sadd/
11- await client . SADD ( "user1:favorites" , "1" ) ;
13+ await client . SADD ( setName , "1" ) ;
1214
1315// retrieve values of the set defined
1416// https://redis.io/commands/smembers/
15- const favorites = await client . SMEMBERS ( "user1:favorites" ) ;
17+ const favorites = await client . SMEMBERS ( setName ) ;
1618for ( 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 ) ;
1825}
1926
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+
2039await client . quit ( ) ;
You can’t perform that action at this time.
0 commit comments