温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mac下Redis5 BloomFilter安装及怎么与python连用

发布时间:2022-03-29 16:02:57 来源:亿速云 阅读:218 作者:iii 栏目:移动开发

本文小编为大家详细介绍“mac下Redis5 BloomFilter安装及怎么与python连用”,内容详细,步骤清晰,细节处理妥当,希望这篇“mac下Redis5 BloomFilter安装及怎么与python连用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

安装及使用布隆过滤器

Centos7 上 Redis 5.x 安装及使用布隆过滤器(BloomFilter )

1 进入redis安装目录:cd /usr/local/redis-5.0.4
2. 下载插件: git clone https://github.com/RedisBloom/RedisBloom.git  
	# https://github.com/RedisBloom/RedisBloom 如果慢 可以使用外网访问
3. 进入插件目录: cd redisbloom/  (重命名之前为RedisBloom)
4. 执行: make
5. 修改 redis.conf,增加配置: loadmodule /usr/local/redis-5.0.4/redisbloom/redisbloom.so
6. 启动redis:  src/redis-server ./redis.conf
7. 连接客户端: src/redis-cli -p 6379 
8. 测试,先后执行: bf.add users francis     bf.exists users francis  
9. 更多内容可参考: https://oss.redislabs.com/redisbloom/

python的使用
1.第一种方法 连接 redis 使用原生的语句使用

from redis import StrictRedis
from django.conf import settings


class BfRedis:
    def __init__(self, db, host=settings.BF_REDIS_HOST, port=settings.BF_REDIS_PORT, password=settings.BF_REDIS_PASSWORD):
        self.client = StrictRedis(db=db, host=host, port=port, password=password)

    def bf_init(self, key: str, error_rate: float(), size: int):
        res = self.client.execute_command('BF.RESERVE', key, error_rate, size)
        return res

    def bf_exists(self, key, value):
        res = self.client.execute_command('BF.exists', key, value)
        return res

    def bf_add(self, key, value):
        return self.client.execute_command('BF.add', key, value)

    def bf_local_init(self, task_id, error_rate=0.0001, size=10000):
        """
        """
        key = f'bf_{task_id}'
        if self.client.exists(key):
            return True
        res = self.bf_init(key, error_rate, size)
        return res

    def bf_local_add(self, task_id, value):
        key = f'bf_{task_id}'
        res = self.bf_add(key, value)
        return res

    def bf_local_exists(self, task_id, value):
        key = f'bf_{task_id}'
        res = self.bf_exists(key, value)
        return res

    def bf_local_del(self, task_id):
        key = f'bf_{task_id}'
        res = self.client.delete(key)
        return res
# bf_redis = CrawlRedisClient(0)
  1. 使用 python 的工具模块

python2安装:pip install pybloom
python3安装:pip install pybloom-live

demo

from pybloom import BloomFilter, ScalableBloomFilter
bf = BloomFilter(capacity=10000, error_rate=0.001)
bf.add('test')
print 'test' in bf
sbf = ScalableBloomFilter(mode=ScalableBloomFilter.SMALL_SET_GROWTH)
sbf.add('dddd')
print 'ddd' in sbf

BloomFilter是一个定容的过滤器,error_rate是指最大的误报率是0.1%,而ScalableBloomFilter是一个不定容量的布隆过滤器,它可以不断添加元素。add 方法是添加元素,如果元素已经在布隆过滤器中,就返回true,如果不在返回fasle并将该元素添加到过滤器中。判断一个元素是否在过滤器中,只需要使用in运算符即可了。

读到这里,这篇“mac下Redis5 BloomFilter安装及怎么与python连用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI