Skip to content

Commit b68836c

Browse files
Simon Prickettleibale
andauthored
Adds Cuckoo Filter example. (#1843)
Co-authored-by: Leibale Eidelman <[email protected]>
1 parent 86c239e commit b68836c

File tree

2 files changed

+97
-15
lines changed

2 files changed

+97
-15
lines changed

examples/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
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-
| `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-
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch |
12-
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
13-
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
14-
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
15-
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
16-
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
17-
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
18-
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
19-
| `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+
| `count-min-sketch.js` | Estimate the frequency of a given event using the [RedisBloom](https://redisbloom.io) Count-Min Sketch |
12+
| `cuckoo-filter.js` | Space efficient set membership checks with a [Cuckoo Filter](https://en.wikipedia.org/wiki/Cuckoo_filter) using [RedisBloom](https://redisbloom.io) |
13+
| `lua-multi-incr.js` | Define a custom lua script that allows you to perform INCRBY on multiple keys |
14+
| `managing-json.js` | Store, retrieve and manipulate JSON data atomically with [RedisJSON](https://redisjson.io/) |
15+
| `search-hashes.js` | Uses [RediSearch](https://redisearch.io) to index and search data in hashes |
16+
| `search-json.js` | Uses [RediSearch](https://redisearch.io/) and [RedisJSON](https://redisjson.io/) to index and search JSON data |
17+
| `set-scan.js` | An example script that shows how to use the SSCAN iterator functionality |
18+
| `stream-producer.js` | Adds entries to a [Redis Stream](https://redis.io/topics/streams-intro) using the `XADD` command |
19+
| `stream-consumer.js` | Reads entries from a [Redis Stream](https://redis.io/topics/streams-intro) using the blocking `XREAD` command |
20+
| `topk.js` | Use the [RedisBloom](https://redisbloom.io) TopK to track the most frequently seen items. |
2021

2122
## Contributing
2223

examples/cuckoo-filter.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// This example demonstrates the use of the Cuckoo Filter
2+
// in the RedisBloom module (https://redisbloom.io/)
3+
4+
import { createClient } from 'redis';
5+
6+
async function cuckooFilter() {
7+
const client = createClient();
8+
9+
await client.connect();
10+
11+
// Delete any pre-existing Cuckoo Filter.
12+
await client.del('mycuckoo');
13+
14+
// Reserve a Cuckoo Filter with a capacity of 10000 items.
15+
// https://oss.redis.com/redisbloom/Cuckoo_Commands/#cfreserve
16+
try {
17+
await client.cf.reserve('mycuckoo', 10000);
18+
console.log('Reserved Cuckoo Filter.');
19+
} catch (e) {
20+
console.log('Error, maybe RedisBloom is not installed?:');
21+
console.log(e);
22+
}
23+
24+
// Add items to Cuckoo Filter individually with CF.ADD command.
25+
https://oss.redis.com/redisbloom/Cuckoo_Commands/#cfadd
26+
await Promise.all([
27+
client.cf.add('mycuckoo', 'leibale'),
28+
client.cf.add('mycuckoo', 'simon'),
29+
client.cf.add('mycuckoo', 'guy'),
30+
client.cf.add('mycuckoo', 'suze'),
31+
client.cf.add('mycuckoo', 'brian'),
32+
client.cf.add('mycuckoo', 'steve'),
33+
client.cf.add('mycuckoo', 'kyle'),
34+
client.cf.add('mycuckoo', 'josefin'),
35+
client.cf.add('mycuckoo', 'alex'),
36+
client.cf.add('mycuckoo', 'nava'),
37+
]);
38+
39+
// Add items to the Cuckoo Filter only if they don't exist in it...
40+
// https://oss.redis.com/redisbloom/Cuckoo_Commands/#cfaddnx
41+
const nxReply = await Promise.all([
42+
client.cf.addNX('mycuckoo', 'kaitlyn'), // New
43+
client.cf.addNX('mycuckoo', 'rachel'), // New
44+
client.cf.addNX('mycuckoo', 'brian') // Previously added
45+
]);
46+
47+
console.log('Added members to Cuckoo Filter.');
48+
console.log('nxReply:');
49+
50+
// nxReply looks like this:
51+
// [
52+
// true,
53+
// true,
54+
// false
55+
// ]
56+
console.log(nxReply);
57+
58+
// Check whether a member exists with the CF.EXISTS command.
59+
const simonExists = await client.bf.exists('mycuckoo', 'simon');
60+
console.log(`simon ${simonExists ? 'may' : 'does not'} exist in the Cuckoo Filter.`);
61+
62+
// Get stats for the Cuckoo Filter with the CF.INFO command:
63+
const info = await client.cf.info('mycuckoo');
64+
65+
// info looks like this:
66+
// {
67+
// size: 16440,
68+
// numberOfBuckets: 8192,
69+
// numberOfFilters: 1,
70+
// numberOfInsertedItems: 12,
71+
// numberOfDeletedItems: 0,
72+
// bucketSize: 2,
73+
// expansionRate: 1,
74+
// maxIteration: 20
75+
// }
76+
console.log(info);
77+
78+
await client.quit();
79+
}
80+
81+
cuckooFilter();

0 commit comments

Comments
 (0)