Redis高階5-布隆過濾器

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步驟

  1. 初始化bitmap

    布隆過濾器 本質上 是由長度為 m 的位向量或位列表(僅包含 0 或 1 位值的列表)組成,最初所有的值均設置為 0

  2. 添加占坑位

    當我們向布隆過濾器中添加數據時,為了盡量地址不沖突,會使用多個 hash 函數對 key 進行運算,算得一個下標索引值,然后對位數組長度進行取模運算得到一個位置,每個 hash 函數都會算得一個不同的位置。再把位數組的這幾個位置都置為 1 就完成了 add 操作。

    例如,我們添加一個字符串wmyskxz,對字符串進行多次hash(key) → 取模運行→ 得到坑位

    請添加圖片描述

  3. 判斷是否存在

    向布隆過濾器查詢某個key是否存在時,先把這個 key 通過相同的多個 hash 函數進行運算,查看對應的位置是否都為 1,

    只要有一個位為零,那么說明布隆過濾器中這個 key 不存在;

    如果這幾個位置全都是 1,那么說明極有可能存在;

    因為這些位置的 1 可能是因為其他的 key 存在導致的,也就是前面說過的hash沖突。。。。。

    就比如我們在 add 了字符串wmyskxz數據之后,很明顯下面1/3/5 這幾個位置的 1 是因為第一次添加的 wmyskxz 而導致的;

    此時我們查詢一個沒添加過的不存在的字符串inexistent-key,它有可能計算后坑位也是1/3/5 ,這就是誤判了

? 請添加圖片描述

使用場景

  1. 解決緩存穿透的問題,和redis結合bitmap使用

    *緩存穿透是什么*

    一般情況下,先查詢緩存redis是否有該條數據,緩存中沒有時,再查詢數據庫。

    當數據庫也不存在該條數據時,每次查詢都要訪問數據庫,這就是緩存穿透。

    緩存透帶來的問題是,當有大量請求查詢數據庫不存在的數據時,就會給數據庫帶來壓力,甚至會拖垮數據庫。

    可以使用布隆過濾器解決緩存穿透的問題

    把已存在數據的key存在布隆過濾器中,相當于redis前面擋著一個布隆過濾器。

    當有新的請求時,先到布隆過濾器中查詢是否存在:

    如果布隆過濾器中不存在該條數據則直接返回;

    如果布隆過濾器中已存在,才去查詢緩存redis,如果redis里沒查詢到則再查詢Mysql數據庫

    請添加圖片描述

  2. 黑名單校驗,識別垃圾郵件

    發現存在黑名單中的,就執行特定操作。比如:識別垃圾郵件,只要是郵箱在黑名單中的郵件,就識別為垃圾郵件。

    假設黑名單的數量是數以億計的,存放起來就是非常耗費存儲空間的,布隆過濾器則是一個較好的解決方案。

    把所有黑名單都放在布隆過濾器中,在收到郵件時,判斷郵件地址是否在布隆過濾器中即可。

  3. 安全連接網址,全球上10億的網址判斷

案例

  1. 整體架構

    請添加圖片描述

  2. 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

  3. 添加布隆過濾器案例

  • 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沖突同一個位置可能存的東西是多個共有的,你刪除一個元素的同時可能也把其它的刪除了。

    存在誤判,不能精準過濾

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/67135.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/67135.shtml
英文地址,請注明出處:http://en.pswp.cn/web/67135.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

DeepSeek學術題目選擇效果怎么樣?

論文選題 一篇出色的論文背后&#xff0c;必定有一個“智慧的選題”在撐腰。選題足夠好文章就能順利登上高水平期刊&#xff1b;選題不行再精彩的寫作也只能“當花瓶”。然而許多寶子們常常忽視這個環節&#xff0c;把大量時間花在寫作上&#xff0c;選題時卻像抓鬮一樣隨便挑一…

第五節 MATLAB命令

本節的內容將提供常用的一些MATLAB命令。 在之前的篇章中我們已經知道了MATLAB數值計算和數據可視化是一個交互式程序&#xff0c;在它的命令窗口中您可以在MATLAB提示符“>>”下鍵入命令。 MATLAB管理會話的命令 MATLAB提供管理會話的各種命令。如下表所示&#xff1a;…

Docker核心命令與Yocto項目的高效應用

隨著軟件開發逐漸向分布式和容器化方向演進&#xff0c;Docker 已成為主流的容器化技術之一。它通過標準化的環境配置、資源隔離和高效的部署流程&#xff0c;大幅提高了開發和構建效率。Yocto 項目作為嵌入式 Linux 系統構建工具&#xff0c;與 Docker 的結合進一步增強了開發…

Qt 5.14.2 學習記錄 —— ?? QFile和多線程

文章目錄 1、QFile1、打開2、讀寫3、關閉4、程序5、其它功能 2、多線程1、演示2、鎖 3、條件變量和信號量 1、QFile Qt有自己的一套文件體系&#xff0c;不過Qt也可以使用C&#xff0c;C&#xff0c;Linux的文件操作。使用Qt的文件體系和Qt自己的一些類型更好配合。 管理寫入讀…

【全棧】SprintBoot+vue3迷你商城-擴展:vue3項目創建及目錄介紹

【全棧】SprintBootvue3迷你商城-擴展&#xff1a;vue3項目創建及目錄介紹 往期的文章都在這里啦&#xff0c;大家有興趣可以看一下 【全棧】SprintBootvue3迷你商城&#xff08;1&#xff09; 【全棧】SprintBootvue3迷你商城&#xff08;2&#xff09; 【全棧】SprintBootvu…

使用Aardio庫在Python中創建桌面應用:簡單指南

引言 隨著軟件開發需求的不斷增長&#xff0c;開發者們需要更加靈活和高效的工具來快速構建應用程序。Python以其簡潔易讀的語法和強大的社區支持而聞名&#xff0c;但在創建圖形用戶界面&#xff08;GUI&#xff09;時&#xff0c;可能會遇到一些挑戰。Aardio作為一種輕量級的…

多版本并發控制:MVCC的作用和基本原理

多版本并發控制&#xff1a;MVCC的作用和基本原理 1、MVCC簡介1.1 快照讀與當前讀的區別1.1.1 快照讀1.1.2 當前讀 1.2 數據庫的讀寫問題1.3 MVCC的作用 2、MVCC實現原理之ReadView2.1 什么是ReadView2.2 ReadView的設計思路2.3 MVCC整體操作流程 1、MVCC簡介 1.1 快照讀與當前…

神經網絡|(二)sigmoid神經元函數

【1】引言 在前序學習進程中&#xff0c;我們已經了解了基本的二元分類器和神經元的構成&#xff0c;文章學習鏈接為&#xff1a; 神經網絡|(一)加權平均法&#xff0c;感知機和神經元-CSDN博客 在此基礎上&#xff0c;我們認識到神經元本身在做二元分類&#xff0c;是一種非…

Qt中QVariant的使用

1.使用QVariant實現不同類型數據的相加 方法&#xff1a;通過type函數返回數值的類型&#xff0c;然后通過setValue來構造一個QVariant類型的返回值。 函數&#xff1a; QVariant mainPage::dataPlus(QVariant a, QVariant b) {QVariant ret;if ((a.type() QVariant::Int) &a…

BAHD酰基轉移酶對紫草素的手性催化-文獻精讀105

Two BAHD Acyltransferases Catalyze the Last Step in the Shikonin/Alkannin Biosynthetic Pathway 兩個BAHD酰基轉移酶催化了紫草素/左旋紫草素生物合成途徑中的最后一步 一個BAHD酰基轉移酶專門催化紫草素的酰基化&#xff0c;而另一個BAHD酰基轉移酶則僅催化紫草素的對映…

Avalonia+ReactiveUI跨平臺路由:打造絲滑UI交互的奇幻冒險

一、引言 在當今數字化時代&#xff0c;跨平臺應用開發已成為大勢所趨。開發者們迫切需要一種高效、靈活的方式&#xff0c;能夠讓應用程序在不同操作系統上無縫運行&#xff0c;為用戶提供一致的體驗。Avalonia 和 ReactiveUI 的組合&#xff0c;宛如一對天作之合的舞者&…

CLion開發Qt桌面

IDE&#xff1a;CLion Qt Qt版本&#xff1a;5.12 學習正點原子的嵌入式Linux開發板時&#xff0c;使用Qt Creator寫代碼不是很方便&#xff0c;遂嘗試使用CLion搭建Qt開發環境。 一、CLion的Qt環境搭建 1&#xff0c;配置工具鏈 找到Qt的安裝目錄&#xff0c;此處為E:\Tools\…

【學術會議-第五屆機械設計與仿真國際學術會議(MDS 2025) 】前端開發:技術與藝術的完美融合

重要信息 大會官網&#xff1a;www.icmds.net 大會時間&#xff1a;2025年02月28日-03月02日 大會地點&#xff1a;中國-大連 會議簡介 2025年第五屆機械設計與仿真國際學術會議&#xff08;MDS 2025) 將于2025年02月28-3月02日在中國大連召開。MDS 2025將圍繞“機械設計”…

《DeepSeek R1:開源大模型的破局者》

驚爆&#xff01;中國開源大模型震撼登場 在人工智能領域的激烈競爭中&#xff0c;一場震撼全球的技術革命正悄然發生。2025 年 1 月 20 日晚&#xff0c;一家來自中國的人工智能初創公司 ——DeepSeek&#xff08;深度求索&#xff09;&#xff0c;如同一顆耀眼的新星&#x…

84,【8】BUUCTF WEB [羊城杯 2020]Blackcat

進入靶場 音樂硬控我3分鐘 回去看源碼 <?php // 檢查 POST 請求中是否包含 Black-Cat-Sheriff 和 One-ear 字段 // 如果任意一個字段為空&#xff0c;則輸出錯誤信息并終止腳本執行 if(empty($_POST[Black-Cat-Sheriff]) || empty($_POST[One-ear])){die(請提供 Black-C…

人工智能:從基礎到前沿

目錄 目錄 1. 引言 2. 人工智能基礎 2.1 什么是人工智能&#xff1f; 2.2 人工智能的歷史 2.3 人工智能的分類 3. 機器學習 3.1 機器學習概述 3.2 監督學習 3.3 無監督學習 3.4 強化學習 4. 深度學習 4.1 深度學習概述 4.2 神經網絡基礎 4.3 卷積神經網絡&#…

漏洞情報:為什么、要什么和怎么做

漏洞一直是網絡攻防的焦點所在&#xff0c;因為漏洞直接或間接影響安全性的核心方面——權限。攻擊者挖掘和利用漏洞&#xff0c;獲取非授權的權限&#xff1b;防御方定位和消除漏洞&#xff0c;監測和阻斷漏洞的利用&#xff0c;使攻擊者無法利用漏洞達到其目的。漏洞信息本質…

leetcode——刪除鏈表的倒數第N個節點(java)

給你一個鏈表&#xff0c;刪除鏈表的倒數第 n 個結點&#xff0c;并且返回鏈表的頭結點。 示例 1&#xff1a; 輸入&#xff1a;head [1,2,3,4,5], n 2 輸出&#xff1a;[1,2,3,5] 示例 2&#xff1a; 輸入&#xff1a;head [1], n 1 輸出&#xff1a;[] 示例 3&#xf…

正則表達式以及Qt中的使用

目錄 一、正則表達式 1、基本匹配&#xff1a; 2、元字符&#xff1a; 2.1 .運算符&#xff1a; 2.2 字符集&#xff1a; 2.3 重復次數&#xff1a; 2.4 量詞{} 2.5 特征標群() 2.6 或運算符 2.7 \反斜線轉碼特殊字符 2.8 錨點 3、簡寫字符 4、零寬度斷言 4.1 正…

【第一天】零基礎入門刷題Python-算法篇-數據結構與算法的介紹(持續更新)

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 前言一、Python數據結構與算法的詳細介紹1.基本概念2.Python中的數據結構1. 列表&#xff08;List&#xff09;2. 元組&#xff08;Tuple&#xff09;3. 字典&#…