|
| 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