Skip to content

Commit 06cb637

Browse files
Simon Prickettleibale
andauthored
Adds Bloom Filter example using RedisBloom. (#1835)
* Adds Bloom Filter example using RedisBloom. * Update bloom-filter.js Co-authored-by: Leibale Eidelman <[email protected]>
1 parent e4601b6 commit 06cb637

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

examples/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
This folder contains example scripts showing how to use Node Redis in different scenarios.
44

5-
| File Name | Description |
6-
|-----------------------------|----------------------------------------------------------------------------------------------------------------|
7-
| `blocking-list-pop.js` | Block until an element is pushed to a list |
8-
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
9-
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
10-
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
11-
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
12-
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
13-
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
14-
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
15-
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
16-
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
17-
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
5+
| File Name | Description |
6+
|-----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------|
7+
| `blocking-list-pop.js` | Block until an element is pushed to a list |
8+
| `bloom-filter.js` | Space efficient set membership checks with a [Bloom Filter](https://en.wikipedia.org/wiki/Bloom_filter) using [RedisBloom](https://redisbloom.io) |
9+
| `command-with-modifiers.js` | Define a script that allows to run a command with several modifiers |
10+
| `connect-as-acl-user.js` | Connect to Redis 6 using an ACL user |
11+
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
12+
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
13+
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
14+
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
15+
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
16+
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
17+
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
18+
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
1819

1920
## Contributing
2021

examples/bloom-filter.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// This example demonstrates the use of the Bloom Filter
2+
// in the RedisBloom module (https://redisbloom.io/)
3+
4+
import { createClient } from 'redis';
5+
6+
async function bloomFilter() {
7+
const client = createClient();
8+
9+
await client.connect();
10+
11+
// Delete any pre-existing Bloom Filter.
12+
await client.del('mybloom');
13+
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+
}
26+
}
27+
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+
]);
47+
48+
console.log('Added members to Bloom Filter.');
49+
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.`);
53+
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+
]);
59+
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.`);
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);
75+
76+
await client.quit();
77+
}
78+
79+
bloomFilter();

0 commit comments

Comments
 (0)