|
1 | 1 | // 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/) |
3 | 3 |
|
4 | 4 | import { createClient } from 'redis';
|
5 | 5 |
|
6 |
| -async function bloomFilter() { |
7 |
| - const client = createClient(); |
| 6 | +const client = createClient(); |
8 | 7 |
|
9 |
| - await client.connect(); |
| 8 | +await client.connect(); |
10 | 9 |
|
11 |
| - // Delete any pre-existing Bloom Filter. |
12 |
| - await client.del('mybloom'); |
| 10 | +// Delete any pre-existing Bloom Filter. |
| 11 | +await client.del('mybloom'); |
13 | 12 |
|
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); |
26 | 24 | }
|
| 25 | +} |
27 | 26 |
|
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 | +]); |
47 | 41 |
|
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 | +]); |
49 | 48 |
|
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.'); |
53 | 50 |
|
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.`); |
59 | 55 |
|
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 | +]); |
62 | 62 |
|
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.`); |
75 | 65 |
|
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); |
78 | 79 |
|
79 |
| -bloomFilter(); |
| 80 | +await client.quit(); |
0 commit comments