Redis高级篇之布隆过滤器

版本

  1. SpringBoot 2.6.1
  2. Mybatis-Plus 3.5.3.1
  3. Guava 23.0

pom.xml


<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.1version>
        <relativePath/> 
    parent>
    <groupId>com.lichigroupId>
    <artifactId>generatorartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>generatorname>
    <description>generatordescription>
    <properties>
        <java.version>17java.version>
    properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>23.0version>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plusartifactId>
            <version>3.5.3.1version>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.5.3.1version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.17version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        <dependency>
            <groupId>org.freemarkergroupId>
            <artifactId>freemarkerartifactId>
            <version>2.3.32version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
    <repositories>
        <repository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
        <repository>
            <id>spring-snapshotsid>
            <name>Spring Snapshotsname>
            <url>https://repo.spring.io/snapshoturl>
            <releases>
                <enabled>falseenabled>
            releases>
        repository>
    repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestonesid>
            <name>Spring Milestonesname>
            <url>https://repo.spring.io/milestoneurl>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        pluginRepository>
        <pluginRepository>
            <id>spring-snapshotsid>
            <name>Spring Snapshotsname>
            <url>https://repo.spring.io/snapshoturl>
            <releases>
                <enabled>falseenabled>
            releases>
        pluginRepository>
    pluginRepositories>

project>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://ip地址:3306/ai_room?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root
    password: 密码

  redis:
    cluster:
      nodes:
        - ip地址:6381
        - ip地址:6382
        - ip地址:6383
        - ip地址:6384
        - ip地址:6385
        - ip地址:6386
      max-redirects: 3
    password: 111111
    lettuce:
      cluster:
        refresh:
          adaptive: true
          period: 2000
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 10
        min-idle: 5
  mvc:
    path match:
      matching-strategy: ant_path_matcher

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.lichi.generator.entity

布隆过滤器核心代码

    public static final BloomFilter<String> bloomFilter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), 100, 0.03);
	// 添加到布隆过滤器
	bloomFilter.put(id);
	@ApiOperation("查询用户")
    @ResponseBody
    @GetMapping("/get/{id}")
    public User get(@PathVariable("id") String id) {
        Boolean flag = bloomFilter.mightContain(id);
        if(flag) {
            System.out.println("==========布隆过滤器判断可能存在==========");
            User user = (User) redisTemplate.opsForValue().get(userKey + id);
            // 1 如果 redis 中不存在
            if(user == null) {
                // 2 查询 mysql 数据库
                user = userService.getById(id);
                // 3 如果 mysql 存在
                if(user != null) {
                    // 4 回写数据到 redis
                    redisTemplate.opsForValue().set(userKey + id, user);
                }
            }
            return user;
        }
        System.out.println("==========布隆过滤器判断不存在,可疑非法攻击==========");
        return null;
    }

你可能感兴趣的:(redis,数据库,缓存)