9951 explained code solutions for 126 technologies


predisHow can I use a Redis Bloom Filter in PHP?


A Redis Bloom Filter is a probabilistic data structure used to test whether an element is a member of a set. It can be used in PHP to check if a given element is already present in the set.

Example code

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

// Create a new Bloom Filter with a capacity of 1000
$redis->bfCreate('myBloomFilter', 1000);

// Add an element to the Bloom Filter
$redis->bfAdd('myBloomFilter', 'myElement');

// Check if the element is present in the Bloom Filter
$exists = $redis->bfExists('myBloomFilter', 'myElement');

echo $exists ? 'Element exists' : 'Element does not exist';

Output example

Element exists

Code explanation

  • $redis = new Redis(); - Create a new Redis instance.
  • $redis->connect('127.0.0.1', 6379); - Connect to the Redis server.
  • $redis->bfCreate('myBloomFilter', 1000); - Create a new Bloom Filter with a capacity of 1000.
  • $redis->bfAdd('myBloomFilter', 'myElement'); - Add an element to the Bloom Filter.
  • $exists = $redis->bfExists('myBloomFilter', 'myElement'); - Check if the element is present in the Bloom Filter.
  • echo $exists ? 'Element exists' : 'Element does not exist'; - Output the result of the check.

Helpful links

Edit this code on GitHub