Skip to content

Commit f3462ab

Browse files
author
Simon Prickett
authored
Updates examples. (#2219)
* Updates examples. * Added command link for hset.
1 parent 60ad6aa commit f3462ab

19 files changed

+923
-970
lines changed

examples/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ When adding a new example, please follow these guidelines:
5353
* Use semicolons
5454
* Use `async` and `await`
5555
* Use single quotes, `'hello'` not `"hello"`
56-
* Place your example code in a single `async` function where possible, named according to the file name e.g. `add-to-stream.js` would contain `const addtoStream = async () => { ... };`, and call this function at the end of the file e.g. `addToStream();`
56+
* Use [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) when embedding expressions in strings
5757
* Unless your example requires a connection string, assume Redis is on the default localhost port 6379 with no password
5858
* Use meaningful example data, let's not use `foo`, `bar`, `baz` etc!
5959
* Leave an empty line at the end of your `.js` file
@@ -71,18 +71,17 @@ Here's a starter template for adding a new example, imagine this is stored in `d
7171

7272
// Set up the data in redis-cli using these commands:
7373
// <add your command(s) here, one per line>
74+
//
75+
// Alternatively, add code that sets up the data.
7476

7577
import { createClient } from 'redis';
7678

77-
async function doSomething() {
78-
const client = createClient();
79+
const client = createClient();
7980

80-
await client.connect();
81+
await client.connect();
8182

82-
// Add your example code here...
83+
// Add your example code here...
8384

84-
await client.quit();
85-
}
85+
await client.quit();
8686

87-
doSomething();
8887
```

examples/blocking-list-pop.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@
66

77
import { createClient, commandOptions } from 'redis';
88

9-
async function blockingListPop() {
10-
const client = createClient();
9+
const client = createClient();
1110

12-
await client.connect();
11+
await client.connect();
1312

14-
const keyName = 'keyName';
13+
const keyName = 'keyName';
1514

16-
const blpopPromise = client.blPop(
17-
commandOptions({ isolated: true }),
18-
keyName,
19-
0
20-
);
15+
const blpopPromise = client.blPop(
16+
commandOptions({ isolated: true }),
17+
keyName,
18+
0
19+
);
2120

22-
await client.lPush(keyName, 'value');
21+
await client.lPush(keyName, 'value');
2322

24-
await blpopPromise;
23+
const listItem = await blpopPromise;
2524

26-
console.log('blpopPromise resolved');
27-
console.log(keyName);
25+
console.log('blpopPromise resolved');
26+
// listItem will be:
27+
// {"key":"keyName","element":"value"}
28+
console.log(`listItem is '${JSON.stringify(listItem)}'`);
2829

29-
await client.quit();
30-
}
31-
32-
blockingListPop();
30+
await client.quit();

examples/bloom-filter.js

Lines changed: 64 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,80 @@
11
// This example demonstrates the use of the Bloom Filter
2-
// in the RedisBloom module (https://redisbloom.io/)
2+
// in the RedisBloom module (https://redis.io/docs/stack/bloom/)
33

44
import { createClient } from 'redis';
55

6-
async function bloomFilter() {
7-
const client = createClient();
6+
const client = createClient();
87

9-
await client.connect();
8+
await client.connect();
109

11-
// Delete any pre-existing Bloom Filter.
12-
await client.del('mybloom');
10+
// Delete any pre-existing Bloom Filter.
11+
await client.del('mybloom');
1312

14-
// Reserve a Bloom Filter with configurable error rate and capacity.
15-
// https://oss.redis.com/redisbloom/Bloom_Commands/#bfreserve
16-
try {
17-
await client.bf.reserve('mybloom', 0.01, 1000);
18-
console.log('Reserved Bloom Filter.');
19-
} catch (e) {
20-
if (e.message.endsWith('item exists')) {
21-
console.log('Bloom Filter already reserved.');
22-
} else {
23-
console.log('Error, maybe RedisBloom is not installed?:');
24-
console.log(e);
25-
}
13+
// Reserve a Bloom Filter with configurable error rate and capacity.
14+
// https://redis.io/commands/bf.reserve/
15+
try {
16+
await client.bf.reserve('mybloom', 0.01, 1000);
17+
console.log('Reserved Bloom Filter.');
18+
} catch (e) {
19+
if (e.message.endsWith('item exists')) {
20+
console.log('Bloom Filter already reserved.');
21+
} else {
22+
console.log('Error, maybe RedisBloom is not installed?:');
23+
console.log(e);
2624
}
25+
}
2726

28-
// Add items to Bloom Filter individually with BF.ADD command.
29-
await Promise.all([
30-
client.bf.add('mybloom', 'leibale'),
31-
client.bf.add('mybloom', 'simon'),
32-
client.bf.add('mybloom', 'guy'),
33-
client.bf.add('mybloom', 'suze'),
34-
client.bf.add('mybloom', 'brian'),
35-
client.bf.add('mybloom', 'steve'),
36-
client.bf.add('mybloom', 'kyle'),
37-
client.bf.add('mybloom', 'josefin'),
38-
client.bf.add('mybloom', 'alex'),
39-
client.bf.add('mybloom', 'nava'),
40-
]);
41-
42-
// Add multiple items to Bloom Filter at once with BF.MADD command.
43-
await client.bf.mAdd('mybloom', [
44-
'kaitlyn',
45-
'rachel'
46-
]);
27+
// Add items to Bloom Filter individually with BF.ADD command.
28+
// https://redis.io/commands/bf.add/
29+
await Promise.all([
30+
client.bf.add('mybloom', 'leibale'),
31+
client.bf.add('mybloom', 'simon'),
32+
client.bf.add('mybloom', 'guy'),
33+
client.bf.add('mybloom', 'suze'),
34+
client.bf.add('mybloom', 'brian'),
35+
client.bf.add('mybloom', 'steve'),
36+
client.bf.add('mybloom', 'kyle'),
37+
client.bf.add('mybloom', 'josefin'),
38+
client.bf.add('mybloom', 'alex'),
39+
client.bf.add('mybloom', 'nava'),
40+
]);
4741

48-
console.log('Added members to Bloom Filter.');
42+
// Add multiple items to Bloom Filter at once with BF.MADD command.
43+
// https://redis.io/commands/bf.madd/
44+
await client.bf.mAdd('mybloom', [
45+
'kaitlyn',
46+
'rachel'
47+
]);
4948

50-
// Check whether a member exists with the BF.EXISTS command.
51-
const simonExists = await client.bf.exists('mybloom', 'simon');
52-
console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
49+
console.log('Added members to Bloom Filter.');
5350

54-
// Check whether multiple members exist with the BF.MEXISTS command:
55-
const [ lanceExists, leibaleExists ] = await client.bf.mExists('mybloom', [
56-
'lance',
57-
'leibale'
58-
]);
51+
// Check whether a member exists with the BF.EXISTS command.
52+
// https://redis.io/commands/bf.exists/
53+
const simonExists = await client.bf.exists('mybloom', 'simon');
54+
console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
5955

60-
console.log(`lance ${lanceExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
61-
console.log(`leibale ${leibaleExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
56+
// Check whether multiple members exist with the BF.MEXISTS command.
57+
// https://redis.io/commands/bf.mexists/
58+
const [ lanceExists, leibaleExists ] = await client.bf.mExists('mybloom', [
59+
'lance',
60+
'leibale'
61+
]);
6262

63-
// Get stats for the Bloom Filter with the BF.INFO command:
64-
const info = await client.bf.info('mybloom');
65-
// info looks like this:
66-
//
67-
// {
68-
// capacity: 1000,
69-
// size: 1531,
70-
// numberOfFilters: 1,
71-
// numberOfInsertedItems: 12,
72-
// expansionRate: 2
73-
// }
74-
console.log(info);
63+
console.log(`lance ${lanceExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
64+
console.log(`leibale ${leibaleExists ? 'may' : 'does not'} exist in the Bloom Filter.`);
7565

76-
await client.quit();
77-
}
66+
// Get stats for the Bloom Filter with the BF.INFO command.
67+
// https://redis.io/commands/bf.info/
68+
const info = await client.bf.info('mybloom');
69+
// info looks like this:
70+
//
71+
// {
72+
// capacity: 1000,
73+
// size: 1531,
74+
// numberOfFilters: 1,
75+
// numberOfInsertedItems: 12,
76+
// expansionRate: 2
77+
// }
78+
console.log(info);
7879

79-
bloomFilter();
80+
await client.quit();

examples/command-with-modifiers.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@
33

44
import { createClient } from 'redis';
55

6-
async function commandWithModifiers() {
7-
const client = createClient();
6+
const client = createClient();
87

9-
await client.connect();
10-
await client.del('mykey');
8+
await client.connect();
9+
await client.del('mykey');
1110

12-
let result = await client.set('mykey', 'myvalue', {
13-
EX: 60,
14-
GET: true
15-
});
11+
let result = await client.set('mykey', 'myvalue', {
12+
EX: 60,
13+
GET: true
14+
});
1615

17-
console.log(result); //nil
16+
console.log(result); //null
1817

19-
result = await client.set('mykey', 'newvalue', {
20-
EX: 60,
21-
GET: true
22-
});
18+
result = await client.set('mykey', 'newvalue', {
19+
EX: 60,
20+
GET: true
21+
});
2322

24-
console.log(result); //myvalue
25-
26-
await client.quit();
27-
}
28-
29-
commandWithModifiers();
23+
console.log(result); //myvalue
3024

25+
await client.quit();

examples/connect-as-acl-user.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@
77

88
import { createClient } from 'redis';
99

10-
async function connectWithACLUser() {
11-
const client = createClient({
12-
url: 'redis://testuser:testpassword@127.0.0.1:6379'
13-
});
10+
const client = createClient({
11+
url: 'redis://testuser:testpassword@127.0.0.1:6379'
12+
});
1413

15-
await client.connect();
14+
await client.connect();
1615

17-
// Returns PONG
18-
console.log(`Response from PING command: ${await client.ping()}`);
16+
// Returns PONG
17+
console.log(`Response from PING command: ${await client.ping()}`);
1918

20-
try {
21-
// This will error as this user is not allowed to run this command...
22-
console.log(`Response from GET command: ${await client.get('somekey')}`);
23-
} catch (e) {
24-
console.log(`GET command failed: ${e.message}`);
25-
}
26-
27-
await client.quit();
19+
try {
20+
// This will error as this user is not allowed to run this command...
21+
console.log(`Response from GET command: ${await client.get('somekey')}`);
22+
} catch (e) {
23+
console.log(`GET command failed: ${e.message}`);
2824
}
2925

30-
connectWithACLUser();
26+
await client.quit();

0 commit comments

Comments
 (0)