redis的安装及布隆过滤器安装

本文详细介绍了如何在Docker环境中安装Redis,包括配置文件管理、容器创建、布隆过滤器插件的下载、编译和加载过程,以及使用BF.ADD和BF.EXISTS命令进行操作。
摘要由CSDN通过智能技术生成

redis安装

IP

mysql: 172.18.12.2 ~ 12.9

redis: 172.18.12.10 ~172.18.12.19

创建redis文件

/usr/local/software

mkdir redis

redis下创建6379文件夹

mkdir 6380

创建conf、data文件夹

/usr/local/software/redis/6380

    [root@localhost redis]# cd 6380
    [root@localhost 6379]# mkdir conf
    [root@localhost 6379]# mkdir data

上传redis.conf配置文件

创建容器

    docker run -it \
    --name redis_6380 \
    --privileged \
    -p 6380:6379 \
    --network wn_docker_net \
    --ip 172.18.12.11 \
    --sysctl net.core.somaxconn=1024 \
    -e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" \
    -v /usr/local/software/redis/6379/conf/redis.conf:/usr/local/etc/redis/redis.conf \
    -v /usr/local/software/redis/6379/data/:/data \
    -d redis \
    /usr/local/etc/redis/redis.conf

成功结果:

上传插件

解压

    [root@localhost 6379]# tar -zxvf RedisBloom-2.2.4

成功:

redis布隆过滤器

可以把布隆过滤器理解为bitmap结构,判断某个对象是否存在时,它可能会误判。但是布隆过滤器也不是特别不精确,只要参数设置得合理,它的精确度也可以控制得相对足够精确,只会有小小的误判概率。
总得来说,当布隆过滤器说某个值存在时,这个值可能不存在;当它说某个值不存在时,那就肯定不存在

image-20240102155318057

布隆过滤器在项目开发中非常有用,主要应用在如下方面:

  • 黑白名单
  • 内容推广过滤器
  • 爬虫地址url去重
  • 邮件垃圾过滤
  • 防止redis的缓存穿透
  • image-20240118163626726

下载布隆过滤器插件

使用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模块

使用

vim redis.conf

查看redis.conf文件内容

    # 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

使用

docker logs redis_6380

查看redis

                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1:M 18 Jan 2024 19:20:11.792 # Server initialized
1:M 18 Jan 2024 19:20:11.792 # 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 18 Jan 2024 19:20:11.792 # Module /usr/local/etc/redis/redisbloom.so failed to load: /usr/local/etc/redis/redisbloom.so: cannot open shared object file: No such file or directory
1:M 18 Jan 2024 19:20:11.792 # Can't load module from /usr/local/etc/redis/redisbloom.so: server aborting
[root@localhost conf]# 

布隆过滤器命令

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

  • 17
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值