Redis布隆過濾器
? 由一個初始值都為零的bit數組和多個哈希函數構成,用來快速判斷集合中是否存在某個元素
目的 | 減少內存占用 |
---|---|
方式 | 不保存數據信息,只是在內存中做一個是否存在的標記flag |
布隆過濾器(英語:Bloom Filter)是 1970 年由布隆提出的。
它實際上是一個很長的二進制數組(00000000)+一系列隨機hash算法映射函數,主要用于判斷一個元素是否在集合中。
通常我們會遇到很多要判斷一個元素是否在某個集合中的業務場景,一般想到的是將集合中所有元素保存起來,然后通過比較確定。
鏈表、樹、哈希表等等數據結構都是這種思路。但是隨著集合中元素的增加,我們需要的存儲空間也會呈現線性增長,最終達到瓶頸。同時檢索速度也越來越慢,上述三種結構的檢索時間復雜度分別為O(n),O(logn),O(1)。這個時候,布隆過濾器(Bloom Filter)就應運而生
特點
- 高效的插入和查詢,占用空間少,返回的結果是不確定性+不完美的
- 一個元素如果判斷結果:存在時,元素不一定存在;不存在,則一定不存在
- 布隆過濾器可以添加元素,但是不能刪除元素,由于涉及hashcode判斷依據,刪掉元素會導致誤判率增加
原理
布隆過濾器(Bloom Filter) 是一種專門用來解決去重問題的高級數據結構。
實質就是一個大型***位數組***和幾個不同的無偏hash函數(無偏表示分布均勻)。由一個初值都為零的bit數組和多個個哈希函數構成,用來快速判斷某個數據是否存在。但是跟 HyperLogLog 一樣,它也一樣有那么一點點不精確,也存在一定的誤判概率
添加key時
使用多個hash函數對key進行hash運算得到一個整數索引值,對位數組長度進行取模運算得到一個位置,
每個hash函數都會得到一個不同的位置,將這幾個位置都置1就完成了add操作。
查詢key時
只要有其中一位是零就表示這個key不存在,但如果都是1,則不一定存在對應的key。
***結論:***有,是可能有 無,是肯定無
hash沖突導致數據不準確
當有變量被加入集合時,通過N個映射函數將這個變量映射成位圖中的N個點,
把它們置為 1(假定有兩個變量都通過 3 個映射函數)。
hash函數
哈希函數的概念是:將任意大小的輸入數據轉換成特定大小的輸出數據的函數,轉換后的數據稱為哈希值或哈希編碼,也叫散列值
如果兩個散列值是不相同的(根據同一函數)那么這兩個散列值的原始輸入也是不相同的。
這個特性是散列函數具有確定性的結果,具有這種性質的散列函數稱為單向散列函數。
散列函數的輸入和輸出不是唯一對應關系的,如果兩個散列值相同,兩個輸入值很可能是相同的,但也可能不同,
這種情況稱為“散列碰撞(collision)”。
用 hash表存儲大數據量時,空間效率還是很低,當只有一個 hash 函數時,還很容易發生哈希碰撞。
代碼實現
Set<Integer> hashCodeSet = new HashSet<>();for (int i = 0; i <200000; i++) {int hashCode = new Object().hashCode();if(hashCodeSet.contains(hashCode)) {System.out.println("出現了重復的hashcode: "+hashCode+"\t 運行到"+i);break;}hashCodeSet.add(hashCode);}System.out.println("Aa".hashCode());System.out.println("BB".hashCode());System.out.println("柳柴".hashCode());System.out.println("柴柕".hashCode());
使用3步驟
-
初始化bitmap
布隆過濾器 本質上 是由長度為 m 的位向量或位列表(僅包含 0 或 1 位值的列表)組成,最初所有的值均設置為 0
-
添加占坑位
當我們向布隆過濾器中添加數據時,為了盡量地址不沖突,會使用多個 hash 函數對 key 進行運算,算得一個下標索引值,然后對位數組長度進行取模運算得到一個位置,每個 hash 函數都會算得一個不同的位置。再把位數組的這幾個位置都置為 1 就完成了 add 操作。
例如,我們添加一個字符串wmyskxz,對字符串進行多次hash(key) → 取模運行→ 得到坑位
-
判斷是否存在
向布隆過濾器查詢某個key是否存在時,先把這個 key 通過相同的多個 hash 函數進行運算,查看對應的位置是否都為 1,
只要有一個位為零,那么說明布隆過濾器中這個 key 不存在;
如果這幾個位置全都是 1,那么說明極有可能存在;
因為這些位置的 1 可能是因為其他的 key 存在導致的,也就是前面說過的hash沖突。。。。。
就比如我們在 add 了字符串wmyskxz數據之后,很明顯下面1/3/5 這幾個位置的 1 是因為第一次添加的 wmyskxz 而導致的;
此時我們查詢一個沒添加過的不存在的字符串inexistent-key,它有可能計算后坑位也是1/3/5 ,這就是誤判了
?
使用場景
-
解決緩存穿透的問題,和redis結合bitmap使用
*緩存穿透是什么*
一般情況下,先查詢緩存redis是否有該條數據,緩存中沒有時,再查詢數據庫。
當數據庫也不存在該條數據時,每次查詢都要訪問數據庫,這就是緩存穿透。
緩存透帶來的問題是,當有大量請求查詢數據庫不存在的數據時,就會給數據庫帶來壓力,甚至會拖垮數據庫。
可以使用布隆過濾器解決緩存穿透的問題
把已存在數據的key存在布隆過濾器中,相當于redis前面擋著一個布隆過濾器。
當有新的請求時,先到布隆過濾器中查詢是否存在:
如果布隆過濾器中不存在該條數據則直接返回;
如果布隆過濾器中已存在,才去查詢緩存redis,如果redis里沒查詢到則再查詢Mysql數據庫
-
黑名單校驗,識別垃圾郵件
發現存在黑名單中的,就執行特定操作。比如:識別垃圾郵件,只要是郵箱在黑名單中的郵件,就識別為垃圾郵件。
假設黑名單的數量是數以億計的,存放起來就是非常耗費存儲空間的,布隆過濾器則是一個較好的解決方案。
把所有黑名單都放在布隆過濾器中,在收到郵件時,判斷郵件地址是否在布隆過濾器中即可。
-
安全連接網址,全球上10億的網址判斷
-
…
案例
-
整體架構
-
springboot+redis+mybatis整合
Mybatis通用Mapper4
-
t_customer用戶表SQL
CREATE TABLE `t_customer` (`id` int(20) NOT NULL AUTO_INCREMENT,`cname` varchar(50) NOT NULL,`age` int(10) NOT NULL,`phone` varchar(20) NOT NULL,`sex` tinyint(4) NOT NULL,`birth` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`),KEY `idx_cname` (`cname`)) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4
-
建springboot的Module
-
改POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhg</groupId><artifactId>SpringBootRedis7Demo1</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.10</version><relativePath/></parent><properties><!-- 依賴版本號 --><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><java.version>1.8</java.version><hutool.version>5.5.8</hutool.version><druid.version>1.1.18</druid.version><mapper.version>4.1.5</mapper.version><pagehelper.version>5.1.4</pagehelper.version><mysql.version>8.0.18</mysql.version><swagger2.version>2.9.2</swagger2.version><swagger-ui.version>2.9.2</swagger-ui.version><mybatis.spring.version>2.1.3</mybatis.spring.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--Mybatis 通用mapper tk單獨使用,自己帶著版本號--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!--mybatis-spring--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.spring.version}</version></dependency><!-- Mybatis Generator --><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.4.0</version><scope>compile</scope><optional>true</optional></dependency><!--通用Mapper--><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>${mapper.version}</version></dependency><!--persistence--><dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0.2</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><resources><resource><directory>${basedir}/src/main/java</directory><includes><include>**/*.xml</include></includes></resource><resource><directory>${basedir}/src/main/resources</directory></resource></resources><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.6</version><configuration><configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile><overwrite>true</overwrite><verbose>true</verbose></configuration><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>${mapper.version}</version></dependency></dependencies></plugin></plugins></build> </project>
-
mgb配置相關src\main\resources路徑下新建
config.properties
#t_customer表包名 package.name=com.zhg.redis7jdbc.driverClass = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/bigdata jdbc.user = root jdbc.password =123456
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><properties resource="config.properties"/><context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"><property name="beginningDelimiter" value="`"/><property name="endingDelimiter" value="`"/><plugin type="tk.mybatis.mapper.generator.MapperPlugin"><property name="mappers" value="tk.mybatis.mapper.common.Mapper"/><property name="caseSensitive" value="true"/></plugin><jdbcConnection driverClass="${jdbc.driverClass}"connectionURL="${jdbc.url}"userId="${jdbc.user}"password="${jdbc.password}"></jdbcConnection><javaModelGenerator targetPackage="${package.name}.entities" targetProject="src/main/java"/><sqlMapGenerator targetPackage="${package.name}.mapper" targetProject="src/main/java"/><javaClientGenerator targetPackage="${package.name}.mapper" targetProject="src/main/java" type="XMLMAPPER"/><table tableName="t_customer" domainObjectName="Customer"><generatedKey column="id" sqlStatement="JDBC"/></table></context> </generatorConfiguration>
-
一鍵生成,雙擊插件mybatis-generator:gererate,一鍵生成entity+mapper接口+xml實現SQL
Springboot+Redis+Mybatis
-
改造Module
-
POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zhg</groupId><artifactId>SpringBootRedis7Demo1</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.10</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><junit.version>4.12</junit.version><log4j.version>1.2.17</log4j.version><lombok.version>1.16.18</lombok.version></properties><dependencies><!--SpringBoot通用依賴模塊--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--jedis--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.3.1</version></dependency><!--lettuce--><!--<dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId><version>6.2.1.RELEASE</version></dependency>--><!--SpringBoot與Redis整合依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!--swagger2--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!--Mysql數據庫驅動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency><!--SpringBoot集成druid連接池--><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency><!--mybatis和springboot整合--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.0</version></dependency><!--hutool--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.2.3</version></dependency><!--persistence--><dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0.2</version></dependency><!--通用Mapper--><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>4.1.5</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><!--通用基礎配置junit/devtools/test/log4j/lombok/--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
-
YML
server:port: 7777 spring:application:name: redis7_studyswagger2:enabled: truemvc:pathmatch:matching-strategy: ant_path_matcher #============redis=====================redis:database: 0host: 127.0.0.1port: 6379password:lettuce:pool:max-active: 8max-wait: -1msmax-idle: 8min-idle: 0 #============alibaba.druid=====================datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/bigdata?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghaiusername: rootpassword: zhg.168..druid:test-while-idle: false #============log===================== logging:level:root: infocom.zhg.redis7: infopattern:console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n"file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n"file:name: D:/mylogs2023/redis7_study.log #============mybatis===================== mybatis:mapper-locations: classpath:mapper/*.xmltype-aliases-package: com.zhg.redis7.entities
-
主啟動
@SpringBootApplication @MapperScan("com.zhg.redis7.mapper") //tk.mybatis.spring.annotation.MapperScan; public class Redis7Main {public static void main(String[] args){SpringApplication.run(Redis7Main.class,args);} }
-
業務類
CustomerSerivce
@Service @Slf4j public class CustomerSerivce {public static final String CACHE_KEY_CUSTOMER = "customer:";@Resourceprivate CustomerMapper customerMapper;@Resourceprivate RedisTemplate redisTemplate;public void addCustomer(Customer customer){int i = customerMapper.insertSelective(customer);if(i > 0){//到數據庫里面,重新撈出新數據出來,做緩存customer=customerMapper.selectByPrimaryKey(customer.getId());//緩存keyString key=CACHE_KEY_CUSTOMER+customer.getId();//往mysql里面插入成功隨后再從mysql查詢出來,再插入redisredisTemplate.opsForValue().set(key,customer);}}public Customer findCustomerById(Integer customerId){Customer customer = null;//緩存key的名稱String key=CACHE_KEY_CUSTOMER+customerId;//1 查詢rediscustomer = (Customer) redisTemplate.opsForValue().get(key);//redis無,進一步查詢mysqlif(customer==null){//2 從mysql查出來customercustomer=customerMapper.selectByPrimaryKey(customerId);// mysql有,redis無if (customer != null) {//3 把mysql撈到的數據寫入redis,方便下次查詢能redis命中。redisTemplate.opsForValue().set(key,customer);}}return customer;}}
CustomerController
@Api(tags = "客戶Customer接口+布隆過濾器講解") @RestController @Slf4j public class CustomerController{@Resourceprivate CustomerSerivce customerSerivce;@ApiOperation("數據庫初始化2條Customer數據")@RequestMapping(value = "/customer/add", method = RequestMethod.POST)public void addCustomer() {for (int i = 0; i < 2; i++) {Customer customer = new Customer();customer.setCname("customer"+i);customer.setAge(new Random().nextInt(30)+1);customer.setPhone("1381111xxxx");customer.setSex((byte) new Random().nextInt(2));customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));customerSerivce.addCustomer(customer);}}@ApiOperation("單個用戶查詢,按customerid查用戶信息")@RequestMapping(value = "/customer/{id}", method = RequestMethod.GET)public Customer findCustomerById(@PathVariable int id) {return customerSerivce.findCustomerById(id);} }
RedisConfig和swaggerconfig詳見Sringboot整合Redis
啟動測試Swagger是否OK
-
-
添加布隆過濾器案例
-
BloomFilterInit白名單
import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component;import javax.annotation.PostConstruct; import javax.annotation.Resource;/*** 布隆過濾器白名單初始化工具類,一開始就設置一部分數據為白名單所有,* 白名單業務默認規定:布隆過濾器有,redis也有。*/ @Component @Slf4j public class BloomFilterInit {@Resourceprivate RedisTemplate redisTemplate;@PostConstruct//初始化白名單數據,故意差異化數據演示效果......public void init(){//白名單客戶預加載到布隆過濾器String uid = "customer:12";//1 計算hashcode,由于可能有負數,直接取絕對值int hashValue = Math.abs(uid.hashCode());//2 通過hashValue和2的32次方取余后,獲得對應的下標坑位long index = (long) (hashValue % Math.pow(2, 32));log.info(uid+" 對應------坑位index:{}",index);//3 設置redis里面bitmap對應坑位,該有值設置為1redisTemplate.opsForValue().setBit("whitelistCustomer",index,true);} }
-
CheckUtils
import lombok.extern.slf4j.Slf4j; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component;import javax.annotation.Resource;@Component @Slf4j public class CheckUtils {@Resourceprivate RedisTemplate redisTemplate;public boolean checkWithBloomFilter(String checkItem,String key){int hashValue = Math.abs(key.hashCode());long index = (long) (hashValue % Math.pow(2, 32));boolean existOK = redisTemplate.opsForValue().getBit(checkItem, index);log.info("----->key:"+key+"\t對應坑位index:"+index+"\t是否存在:"+existOK);return existOK;} }
-
CustomerSerivce
@Service @Slf4j public class CustomerSerivce {public static final String CACHE_KEY_CUSTOMER = "customer:";@Resourceprivate CustomerMapper customerMapper;@Resourceprivate RedisTemplate redisTemplate;@Resourceprivate CheckUtils checkUtils;public void addCustomer(Customer customer){int i = customerMapper.insertSelective(customer);if(i > 0){//到數據庫里面,重新撈出新數據出來,做緩存customer=customerMapper.selectByPrimaryKey(customer.getId());//緩存keyString key=CACHE_KEY_CUSTOMER+customer.getId();//往mysql里面插入成功隨后再從mysql查詢出來,再插入redisredisTemplate.opsForValue().set(key,customer);}}public Customer findCustomerById(Integer customerId){Customer customer = null;//緩存key的名稱String key=CACHE_KEY_CUSTOMER+customerId;//1 查詢rediscustomer = (Customer) redisTemplate.opsForValue().get(key);//redis無,進一步查詢mysqlif(customer==null){//2 從mysql查出來customercustomer=customerMapper.selectByPrimaryKey(customerId);// mysql有,redis無if (customer != null) {//3 把mysql撈到的數據寫入redis,方便下次查詢能redis命中。redisTemplate.opsForValue().set(key,customer);}}return customer;}/*** BloomFilter → redis → mysql* 白名單:whitelistCustomer* @param customerId* @return*/public Customer findCustomerByIdWithBloomFilter (Integer customerId){Customer customer = null;//緩存key的名稱String key = CACHE_KEY_CUSTOMER + customerId;//布隆過濾器check,無是絕對無,有是可能有//===============================================if(!checkUtils.checkWithBloomFilter("whitelistCustomer",key)){log.info("白名單無此顧客信息:{}",key);return null;}//===============================================//1 查詢rediscustomer = (Customer) redisTemplate.opsForValue().get(key);//redis無,進一步查詢mysqlif (customer == null) {//2 從mysql查出來customercustomer = customerMapper.selectByPrimaryKey(customerId);// mysql有,redis無if (customer != null) {//3 把mysql撈到的數據寫入redis,方便下次查詢能redis命中。redisTemplate.opsForValue().set(key, customer);}}return customer;} }
-
CustomerController
@ApiOperation("BloomFilter案例講解") @RequestMapping(value = "/customerbloomfilter/{id}", method = RequestMethod.GET) public Customer findCustomerByIdWithBloomFilter(@PathVariable int id) throws ExecutionException, InterruptedException {return customerSerivce.findCustomerByIdWithBloomFilter(id); }
-
測試
布隆過濾器優缺點
-
優點
高效地插入和查詢,內存占用bit空間少
-
缺點
不能刪除元素。
因為刪掉元素會導致誤判率增加,因為hash沖突同一個位置可能存的東西是多個共有的,你刪除一個元素的同時可能也把其它的刪除了。
tomer == null) {
//2 從mysql查出來customer
customer = customerMapper.selectByPrimaryKey(customerId);
// mysql有,redis無
if (customer != null) {
//3 把mysql撈到的數據寫入redis,方便下次查詢能redis命中。
redisTemplate.opsForValue().set(key, customer);
}
}
return customer;
}
}
- CustomerController```java
@ApiOperation("BloomFilter案例講解")
@RequestMapping(value = "/customerbloomfilter/{id}", method = RequestMethod.GET)
public Customer findCustomerByIdWithBloomFilter(@PathVariable int id) throws ExecutionException, InterruptedException
{return customerSerivce.findCustomerByIdWithBloomFilter(id);
}
- 測試
布隆過濾器優缺點
-
優點
高效地插入和查詢,內存占用bit空間少
-
缺點
不能刪除元素。
因為刪掉元素會導致誤判率增加,因為hash沖突同一個位置可能存的東西是多個共有的,你刪除一個元素的同時可能也把其它的刪除了。
存在誤判,不能精準過濾