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
More of Predis
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How do I install and configure a PHP Redis DLL on a Windows machine?
- How can I use Redis with the Yii PHP framework?
- How can I use PHP and Redis to get a reverse range of scores?
- How can I troubleshoot a "PHP Redis went away" error?
- How can I use Predis with a cluster in PHP?
- How can I set a timeout for a Redis connection using PHP?
- How can I check the version of PHP and Redis I am using?
- How do I use yum to install php-redis?
See more codes...