由一个初值都为零的bit数组和多个哈希函数构成,用来快速判断集合中是否存在某个元素。
- 高效插入和查询,占用空间少
- 不保存数据信息,只是在内存中做一个是否存在的标记
- 可以添加元素,不能删除元素,否则会增加误判率
- 一个元素如果判断结果:存在时,元素不一定存在;不存在时,则一定不存在
添加key时
使用多个hash函数对key进行hash运算得到一个整数索引值,对位数组长度进行取模运算得到一个位置,每个hash函数都会得到一个不同的位置,将这几个位置都置1就完成了add操作。
查询key时
只要有其中一位是零就表示这个key不存在,但如果都是1,则不一定存在对应的key。
正是基于布隆过滤器的快速检测特性,我们可以在把数据写入数据库时,使用布隆过滤器做个标记。当缓存缺失后,应用查询数据库时,可以通过查询布降过滤器快速判断数据是否存在。如果不存在,就不用再去类据库中查询了。这样一来,即使发生缓存穿透了,大量请求只会查询Redis和布隆过滤器,而不会积压到数据库,也就不会影响数据库的正常运行。布降过滤器可以使用Redis实现,本身就能承担较大的并发访问压力。
简易示例
package com.example.redis;
import com.example.redis.pojo.User;
import com.example.redis.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
public class BitMapAndBloomFilterTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private UserService userService;
public static final String USER_BITMAP_KEY = "user:";
public boolean checkIfInBloomFilter(String key) {
int hashValue = Math.abs(key.hashCode());
long bitIndex = hashValue % (1L << 32);
return redisTemplate.opsForValue().getBit("BloomFilter", bitIndex);
}
@Test
public void test() {
// 将部分数据放入redis和bloomFilter中
// 假设bloomFilter中为白名单数据
User user1 = userService.getById(11);
String key1 = USER_BITMAP_KEY + user1.getId();
redisTemplate.opsForValue().set(key1, user1);
int hashValue1 = Math.abs(key1.hashCode());
long bitIndex1 = hashValue1 % (1L << 32);
redisTemplate.opsForValue().setBit("BloomFilter", bitIndex1, true);
User user2 = userService.getById(12);
String key2 = USER_BITMAP_KEY + user2.getId();
redisTemplate.opsForValue().set(key2, user2);
int hashValue2 = Math.abs(key2.hashCode());
long bitIndex2 = hashValue2 % (1L << 32);
redisTemplate.opsForValue().setBit("BloomFilter", bitIndex2, true);
for (int userId = 11; userId <= 14; userId++) {
System.out.println("--------------------------------------------------------");
String key = USER_BITMAP_KEY + userId;
if (checkIfInBloomFilter(key)) { // bloomFilter中存在该数据,可能是白名单用户
System.out.println(userId + "存在于BloomFilter");
if (redisTemplate.opsForValue().get(key) != null) {
System.out.println(userId + "存在于redis");
System.out.println(userId + "的详细信息:" + redisTemplate.opsForValue().get(key));
continue;
} else {
System.out.println(userId + "不存在于redis");
}
} else { // bloomFilter中不存在该数据,一定不是白名单用户
System.out.println(userId + "不存在于BloomFilter");
continue;
}
// redis中不存在该数据,从数据库中查询
User user = userService.getById(userId);
if (user != null) {
System.out.println(userId + "存在于数据库");
System.out.println(userId + "的详细信息:" + user);
} else {
System.out.println(userId + "不存在于数据库");
}
}
}
}