1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import redis
class RedisBloomFilter(BloomFilter): SEEDS = [543, 460, 171, 876, 796, 607, 650, 81, 837, 545, 591, 946, 846, 521, 913, 636, 878, 735, 414, 372, 344, 324, 223, 180, 327, 891, 798, 933, 493, 293, 836, 10, 6, 544, 924, 849, 438, 41, 862, 648, 338, 465, 562, 693, 979, 52, 763, 103, 387, 374, 349, 94, 384, 680, 574, 480, 307, 580, 71, 535, 300, 53, 481, 519, 644, 219, 686, 236, 424, 326, 244, 212, 909, 202, 951, 56, 812, 901, 926, 250, 507, 739, 371, 63, 584, 154, 7, 284, 617, 332, 472, 140, 605, 262, 355, 526, 647, 923, 199, 518] def __init__(self, capacity=10000000, error_rate=0.000001, redis_conf=None, redis_key=""): super().__init__(capacity, error_rate) self.seeds = self.SEEDS[0:self.k] self.redis_key = redis_key pool = redis.ConnectionPool(**redis_conf) self.bloom = redis.StrictRedis(connection_pool=pool) if not self.bloom.exists(redis_key): self.bloom.setbit(self.redis_key, self.m - 1, 0) def add(self, value): hashes = self.get_hashes(value) for index in hashes: self.bloom.setbit(self.redis_key, index, 1) def is_exist(self, value): hashes = self.get_hashes(value) exist = True for index in hashes: exist = exist & self.bloom.getbit(self.redis_key, index) return exist def get_hashes(self, value): hashes = [] for seed in self.seeds: hash = mmh3.hash(value, seed) hashes.append(hash % self.m) return hashes
|