import redis
from hashlib import md5
import configparser
#读取redis数据库配置
config = configparser.ConfigParser()
config.read('util/db_conf')
host = config['redis']['host']
port = config.getint('redis', 'port')
db = config.getint('redis', 'db')
password = config.get('redis', 'password')
class SimpleHash(object):
def __init__(self, cap, seed):
self.cap = cap
self.seed = seed
def hash(self, value):
ret = 0
for i in range(len(value)):
ret += self.seed * ret + ord(value[i])
return (self.cap - 1) & ret
class BloomFilter(object):
def __init__(self, host=host, port=port, db=db, blockNum=1, redis_name='bloomfilter'):
"""
:param host: the host of Redis
:param port: the port of Redis
:param db: witch db in Redis
:param blockNum: one blockNum for about 90,000,000; if you have more strings for filtering, increase it.
:param redis_name: the key's name in Redis
"""
self.r = redis.Redis(host=host, port=port, db=db,password=password)
self.bit_size = 1 << 5 # Redis的String类型最大容量为512M
self.seeds = [5, 7, 11, 13, 31, 37, 61]
self.blockNum = blockNum
self.hashfunc = []
self.name=redis_name
for seed in self.seeds:
self.hashfunc.append(SimpleHash(self.bit_size, seed))
def md5_encrypt(self,str_input):
m5 = md5()
m5.update(str_input.encode(encoding='utf8'))
return m5.hexdigest()
def isContains(self, str_input):
str_input = self.md5_encrypt(str_input)
ret = True
for f in self.hashfunc:
loc = f.hash(str_input)
ret = ret & self.r.getbit(self.name, loc)
return ret
def insert(self, str_input):
str_input = self.md5_encrypt(str_input)
for f in self.hashfunc:
loc = f.hash(str_input)
self.r.setbit(self.name, loc, 1)
def test():
words = ['hello', 'world', 'hello', 'Tom']
bf = BloomFilter()
for word in words:
if bf.isContains(word): # 判断字符串是否存在
print('"{}" has already in redis'.format(word))
else:
bf.insert(word)
print('"{}" has just been inserted into redis'.format(word))
if __name__ == '__main__':
test()
基于redis的布隆过滤器
最新推荐文章于 2025-04-06 23:15:26 发布