redis布隆过滤器
可以把布隆过滤器理解为bitmap结构,判断某个对象是否存在时,它可能会误判。但是布隆过滤器也不是特别不精确,只要参数设置得合理,它的精确度也可以控制得相对足够精确,只会有小小的误判概率。
总得来说,当布隆过滤器说某个值存在时,这个值可能不存在;当它说某个值不存在时,那就肯定不存在。
![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://img-home.csdnimg.cn/
/20230724024159.png?origin_url=E%3A%5Cdocs%5Cimage%5Cimage-20240102155318057.png&pos_id=img-wHIVNpUJ-1704182370853)
布隆过滤器在项目开发中非常有用,主要应用在如下方面:
- 黑白名单
- 内容推广过滤器
- 爬虫地址url去重
- 邮件垃圾过滤
- 防止redis的缓存穿透
下载布隆过滤器插件
使用redis的布隆过滤器插件,实现布隆过滤器的功能。
wget https://github.com/RedisBloom/RedisBloom/archive/v2.2.4.tar.gz
编译
安装gcc
yum -y install gcc
makefile
make
拷贝redisbloom.so到容器
[root@localhost RedisBloom-2.2.4]# docker cp redisbloom.so redis_6379:/usr/local/etc/redis
Successfully copied 334kB to redis_6379:/usr/local/etc/redis
修改配置文件
添加redisbloom.so到MODULES模块
# Load modules at startup. If the server is not able to load modules
# it will abort. It is possible to use multiple loadmodule directives.
#
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so
loadmodule /usr/local/etc/redis/redisbloom.so
重启容器
docker restart redis_6379
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 1
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
1:M 20 Jul 2023 14:12:33.987 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 20 Jul 2023 14:12:33.987 # Server initialized
1:M 20 Jul 2023 14:12:33.987 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 20 Jul 2023 14:12:33.988 * Module 'bf' loaded from /usr/local/etc/redis/redisbloom.so
1:M 20 Jul 2023 14:12:33.989 * Loading RDB produced by version 6.2.6
1:M 20 Jul 2023 14:12:33.989 * RDB age 0 seconds
1:M 20 Jul 2023 14:12:33.989 * RDB memory usage when created 0.77 Mb
1:M 20 Jul 2023 14:12:33.989 # Done loading RDB, keys loaded: 0, keys expired: 0.
1:M 20 Jul 2023 14:12:33.989 * DB loaded from disk: 0.001 seconds
1:M 20 Jul 2023 14:12:33.989 * Ready to accept connections
布隆过滤器命令
BF.ADD
BF.ADD key item
127.0.0.1:6379> bf.add usernames tom
(integer) 1
127.0.0.1:6379> bf.add usernames jack
(integer) 1
BF.EXISTS
BF.EXISTS key item
127.0.0.1:6379> bf.exists usernames tom
(integer) 1
127.0.0.1:6379> bf.exists usernames jack
(integer) 1
127.0.0.1:6379> bf.exists usernames jackx
(integer) 0