架构视界
主页
企业架构
  • 方案总览
  • 业财一体
  • 企业中台
  • 组织与绩效
  • 流程治理
数据收集与分析
  • 转型总览
  • 信息化阶段
  • 数字化解决方案
  • 智数化解决方案
  • 智能化解决方案
  • 产品设计
  • 项目管理
全栈软件工程
AI 原生软件工程
方法论
博客
  • 资源导航
  • 效率工具
  • 展示专区
主页
企业架构
  • 方案总览
  • 业财一体
  • 企业中台
  • 组织与绩效
  • 流程治理
数据收集与分析
  • 转型总览
  • 信息化阶段
  • 数字化解决方案
  • 智数化解决方案
  • 智能化解决方案
  • 产品设计
  • 项目管理
全栈软件工程
AI 原生软件工程
方法论
博客
  • 资源导航
  • 效率工具
  • 展示专区
  • 标签索引
  • 2024年

    • 勇闯大A:美债与美元周期
    • 勇闯大A:交易纪律与自我管理
    • 勇闯大A:交易系统分层
    • 勇闯大A:买卖点与止损规则
    • 勇闯大A:债券与资金流向
    • 勇闯大A:微观、宏观与周期
    • Mac 开发环境:新机配置清单
    • 勇闯大A:内外盘与换手率
    • 会计管理:财报阅读与管理报表
    • 会计基础:记账原理与分录
    • 财务入门:财务体系全景
  • 2023年

    • 境外投资:泰国 BOI 申请实务
  • 2022年

    • 企业架构:故障管理与分级响应
    • 企业架构:技术债务的识别、度量与偿还
  • 2021年

    • Python 数据分析:员工分布可视化
    • Python 数据分析:IP 访问折线图
    • Python 数据分析:IP 词云图
    • Redis:RediSearch 全文检索
    • Rust 基础:第一个 CLI 程序
    • Rust 基础:所有权机制
    • Rust 基础:函数与控制流
    • Rust 基础:变量与数据类型
    • Rust 基础:入门与开发环境
    • 企业架构:分布式系统设计
    • 企业架构:编程式权限设计
    • JVM 调优:启动参数与 GC 日志
    • MyBatis 批量插入:三种写法对比
    • 测试:Spring Boot 分层 Mock
    • Redis:布隆过滤器判断元素存在
    • Jenkins:安装配置与应用部署
    • WebClient:非阻塞 HTTP 客户端
    • Drone:Docker 部署轻量 CI
    • Spring AOP:操作审计日志落库
    • Redisson:分布式锁与 RedLock
    • MyBatis 拦截器:雪花算法主键
    • Netty:线程模型与网络编程
    • Redis:SCAN 游标遍历大键空间
    • Jenkins:单机部署与升级脚本
    • Mac:Neo4j 与 Jupyter 安装
    • JavaFX:OpenJDK 11 环境配置
    • Mac:用 jenv 管理 JDK 版本
    • Redis:有序集合实现延时队列
    • Spring Boot 日志:MDC 串联链路
    • 定时任务:Spring 调度与 Cron
    • GoAccess:Nginx 日志分析
    • MyBatis 拦截器:慢 SQL 分析
    • Lucene:倒排索引与中文分词
    • 分布式锁:三种实现方案对比
    • 学习技巧:减少学习排斥心理
    • 动态数据源:多租户数据路由
    • Compose:Spring Boot 三件套
    • Java 阻塞队列:源码与用法
    • Redis:Docker Compose 哨兵
    • Redis:Docker Compose 主从
    • 网络通信:TCP/IP 与 HTTP 基础
  • 2020年

    • 延时队列:DelayQueue 与 Redis 实现
    • MySQL 基础:员工库导入实践
    • 雪花算法:分布式 ID 生成
    • Redis:地理位置查询
    • 抓包:Wireshark 与 Tcpdump
    • SPI:JDK 与 Spring 扩展机制
    • ZooKeeper:概念与集群搭建
    • Spring Boot:集成 JMH 基准测试
    • Wireshark:抓包工具入门
    • Compose:Redis 与 MySQL
    • CentOS 7:Docker 安装部署
    • Netty:源码阅读环境搭建
    • MySQL 基础:SQL 常见用法
    • CentOS 7:Selenium 运行环境
    • Nginx:HTTPS 证书配置
    • JMH:基准测试与 Spring Boot 集成
    • Spring Boot:指定 Tomcat 版本
    • Eureka:实例上下线告警
    • 钉钉告警:应用异常与实例上下线
    • JVM:内存布局、类加载与调优
    • Git:rebase 合并多次提交
    • CentOS 7:OpenResty 部署
  • 2019年

    • Redis CLI:macOS 安装与使用
    • Nginx:访问日志格式
    • 工程规范:可维护代码的写法
    • IntelliJ IDEA:常用插件
    • SkyWalking:源码阅读环境搭建
    • Spring Boot:Redis 超时配置
    • gRPC:ProtoBuf 定义与代码生成
    • Arthas:Java 线上问题诊断
    • Redis:缓存穿透与击穿防护
    • Docker 部署:Elasticsearch
    • Docker 部署:MySQL 5.7 单机
    • Spring Redis:字符串命令封装
    • Docker 部署:ZooKeeper 运行
    • Docker 部署:Redis 单机运行
    • Dubbo:Spring Boot 微服务集成
    • CentOS 7:CMake 源码编译安装
    • 性能度量:吞吐量与 QPS 指标
    • Java 递归:从笔试题到经典问题
    • CentOS 7:ELK 日志平台部署
    • CentOS 7:SonarQube 部署
    • Selenium:Chrome 浏览器自动化
    • JJWT:令牌生成与解析测试
    • Spring 源码:阅读环境搭建
    • JUnit 4:注解、断言与参数化
    • JWT:令牌结构与签名机制
    • FastDFS:架构与文件存取机制
    • XPath:XML 文档节点定位
    • Redis 基础:锁、队列与事务
    • Redis 基础:数据类型与命令
    • MyBatis:Slf4j 日志接入
    • MyBatis:H2 单元测试与 SQL 日志
    • 源码阅读:开源项目环境搭建
    • Git 配置:命令清单与作用域
    • Java 核心:面向对象与基础类型
    • Java 核心:关键字、异常与反射
    • Java 核心:多线程基础
    • Java 核心:同步关键字与线程池
    • Java 核心:锁与并发工具
    • Java 核心:语言基础与并发
    • Dubbo:远程调用与服务治理
    • Java 集合:框架总览与实现对比
    • Maven:生命周期与常用命令
    • MyBatis:执行流程与插件机制
    • Spring:IoC 容器与依赖注入
    • Spring:AOP 与自动装配
    • Spring:事务与启动流程
    • Spring:IoC、AOP 与事务
    • Spring MVC:请求分发与注解
    • MySQL:架构与索引
    • MySQL:事务、锁与 MVCC
    • MySQL:日志与 SQL 优化
    • MySQL:架构、事务与优化
    • Redis 基础:持久化与线程模型
  • 2018年

    • Java HashMap:扩容机制与哈希冲突
    • Java 集合:HashSet 去重原理
    • Java 基础:交换两个变量的值
    • Spring MVC:零 web.xml 改造
    • Mac 开发环境:新机配置清单
    • Log4j:日志分级与按业务拆分
    • Modbus:工业通信协议与 RTU 帧
    • MySQL 基础:子查询与连接查询
    • MySQL 基础:库与表操作
    • MySQL 基础:表操作与查询
    • MySQL 基础:约束、引擎与数据类型
    • MySQL 基础:库表、约束与类型
    • Java 集合:Vector 与 Stack
    • Java 集合:Vector 与 Stack
    • RabbitMQ:CentOS 7 单机部署
    • Redis:CentOS 7 单机部署
    • MongoDB:CentOS 7 单机部署
    • Linux:常用系统与运维命令
    • 注册中心:Eureka 与 ZooKeeper
    • MySQL:CentOS 7 单机部署
    • Git 分支:主分支与临时分支
    • CentOS 7:Java 开发环境部署
    • Java 集合:LinkedList 双向链表
    • Java 集合:ArrayList 动态数组
    • Spring AOP:注解方式实现切面
← 返回博客

Redis:缓存穿透与击穿防护

2019年3月26日 #数据与存储 #Redis #Docker

关于Redis场景下简单的处理方式。

源码地址

缓存穿透和缓存击穿

  • 缓存穿透是指用户查询数据,在数据库没有,自然在缓存中也不会有。这样就导致用户查询的时候,在缓存中找不到,每次都要去数据库再查询一遍,然后返回空(相当于进行了两次无用的查询)。这样请求就绕过缓存直接查数据库,这也是经常提的缓存命中率问题。
  • 解决办法
    • 缓存空对象:代码维护较简单,但是效果不好。不过部分场景能使用。

演示环境

  • spring boot 2.4.5
  • mysql
  • Redis
  • 测试环境采用docker-compose

缓存穿透

  • 理想的情况下我们数据缓存到redis里面,客户访问数据的时候直接去缓存查询,如果没有缓存就去数据库查询。
  • 仔细想想这里其实有一个bug,必须是缓存有的数据才会去数据库查询,如果一直都是在查询一个没有得数据咋办呢,缓存会一直落空,而且每次都会去数据库查询。

缓存击穿

这个场景更多是应用在多线程的环境,这里衔接上面的代码,当并发量上来的时候,大量的查询集中查询某一个key,若这个时候key刚好失效了,就会导致请求全部进入数据中。这个就是所谓的缓存击穿。

常用解决思路

这里注意列举常用方案,根据实际情况使用

空值缓存

在访问缓存key之前,设置另一个短期key来锁住当前key的访问。这个方案可以降低持续缓存落空的情况。至少数据库被穿透的概率小很多了。

  • Redis方案
    • 如上图所示,当缓存和数据库中都没找到的时候就将查询条件作为key并在缓存中插入一个空的数据,并指定好过期时间。这样下次的请求就会被缓存命中。

代码实现

项目结构

.
├── docker-compose.yml # docker-compose 配置文件
├── docker-config
│   ├── mysql
│   │   ├── init
│   │   │   └── 1_init.sql # 初始化测试库
│   │   └── my.cnf
│   ├── pwd.txt
│   └── redis
│       └── redis.conf
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── cn
    │   │       └── z201
    │   │           └── redis
    │   │               ├── AppApplication.java
    │   │               ├── AppApplicationController.java
    │   │               ├── AppApplicationServiceImpl.java
    │   │               └── RedisConfig.java
    │   └── resources
    │       ├── application-dev.yml
    │       ├── application-test.yml
    │       ├── application.yml
    │       └── logback.xml
    └── test
        └── java
            └── cn
                └── z201
                    └── redis
                        └── AppApplicationTest.java # 单元测试


Docker-compose配置文件

version : '3'

networks:
  network-redis-cache-breakdown:
    driver: bridge

services:
  web:
    container_name: cn.z201.docker-redis-cache-breakdown
    build:
      context: .
      dockerfile: .
    image: cn.z201.docker-redis-cache-breakdown
    networks:
      - network-redis-cache-breakdown
    expose:
      - '9001'
    ports:
      - '9001:9001'
    depends_on: # 等待其它服务启动完成
      - mysql
      - redis
    links:
      - mysql
      - redis
    healthcheck:
      test: "/bin/netstat -anpt|grep 9001"
      interval: 30s
      timeout: 3s
      retries: 1
  mysql:
    image: mysql:5.7
    container_name: mysql5.7-dev-5
    networks:
      - network-redis-cache-breakdown
    expose:
      - '3306'
    ports:
      - '3306:3306'
    volumes:
      - ./docker-config/mysql/my.cnf:/etc/mysql/my.cnf # 映射数据库配置文件
      - ./docker-config/mysql/init:/docker-entrypoint-initdb.d # 初始化数据库
    command: [
      '--character-set-server=utf8mb4',
      '--collation-server=utf8mb4_unicode_ci',
      '--lower_case_table_names=1',
      '--default-time-zone=+8:00']
    environment:
      - MYSQL_ROOT_PASSWORD=root # 设置root密码
    healthcheck:
      test: "/bin/netstat -anpt|grep 3306"
      interval: 30s
      timeout: 3s
      retries: 1
  redis:
    image: redis:5.0.5
    container_name: redis5.0.6-dev-5
    networks:
      - network-redis-cache-breakdown
    expose:
      - '6379'
    ports:
      - '6379:6379'
    volumes:
      - ./docker-config/redis/redis.conf:/etc/redis.conf # 映射数据库配置文件
    command: redis-server /etc/redis.conf # 启动redis命令
    healthcheck:
      test: "/bin/netstat -anpt|grep 6379"
      interval: 30s
      timeout: 3s
      retries: 1

工具类

package cn.z201.redis;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author z201.coding@gmail.com
 **/
@Service
@Slf4j
public class AppApplicationServiceImpl {

    public static final String INFO = "INFO:";

    private static String LOCK_PREFIX = "LOCK:P";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据用户组件获取数据
     *
     * @param id
     * @return
     */
    public Map<String, Object> findById(String id) {
        String sql = "SELECT * FROM person WHERE id = ?";
        Map<String, Object> person = jdbcTemplate.queryForMap(sql, id);
        return person;
    }
	
		// 获取全部数据  
    public List<Map<String, Object>> all() {
        List<Map<String, Object>> personList =
                jdbcTemplate.queryForList("SELECT * FROM person");
        return personList;
    }

  // redis set ex px 
    private boolean lock(String key, long timeout) {
        Boolean result = redisTemplate.opsForValue().setIfAbsent(LOCK_PREFIX, key, Duration.ofSeconds(timeout));
        if (result) {
            log.info("lock");
        }
        return result;
    }

  // redis单节点解锁
    private boolean unlock(String key) {
        String script = "if redis.call('get',KEYS[1]) == ARGV[1]"
                + "then"
                + "     return redis.call('del',KEYS[1])"
                + "else "
                + "     return 0 "
                + "end";
        String[] args = new String[]{key};
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>(script, Long.class);
        Object result = redisTemplate.execute(redisScript, Collections.singletonList(LOCK_PREFIX), args);
        if (Objects.equals(result, 1L)) {
            log.info("unlock ok");
            return true;
        }
        return false;
    }

  // 空值响应
     public Map<String, Object> findCacheById(String id, Long lockOutTime, Long emptyOutTime) {
        Map<String, Object> result = new HashMap<>();
        Gson gson = new GsonBuilder().create();
        String key = INFO + id;
        Object data = redisTemplate.opsForValue().get(key);
        if (data == null) {
            try {
                if (lock(id, lockOutTime)) {
                    log.info("init data ");
                    result = findById(id);
                    if (CollectionUtils.isEmpty(result)) {
                        // 插入一个空格进去
                        log.info("set empty ");
                        result = new HashMap<>();
                        redisTemplate.opsForValue().set(key, gson.toJson(result), emptyOutTime, TimeUnit.MILLISECONDS);
                    } else {
                        log.info("set data ");
                        redisTemplate.opsForValue().set(key, gson.toJson(result));
                    }
                }
            } finally {
                unlock(id);
            }
        } else {
            result = gson.fromJson(data.toString(), Map.class);
            if (!CollectionUtils.isEmpty(result)) {
                log.info("get cache");
            }
        }
        return result;
    }
}

测试入口

单元测试

package cn.z201.redis;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Type;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = AppApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE)
@AutoConfigureMockMvc
// 指定单元测试方法顺序
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class AppApplicationTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    AppApplicationServiceImpl appApplicationService;

    @Test
    @Disabled
    public void dataView() {
        List<Map<String, Object>> personList =
                appApplicationService.all();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        log.info("personList \n {}", gson.toJson(personList));
        Map<String, Object> objectMap = appApplicationService.findById(personList.get(0).get("id").toString());
        log.info("person \n {}", gson.toJson(objectMap));
    }

    private void clean() {
        Set<String> keys =
                redisTemplate.keys("*");
        redisTemplate.delete(keys);
    }

    private void testCacheBreakdownInfo() {
        int count = 10;
        String id = "3";
        CountDownLatch countDownLatch = new CountDownLatch(count);
        ExecutorService executorService = Executors.newFixedThreadPool(count);
        for (int i = 0; i < count; i++) {
            executorService.execute(() -> {
                log.info(" user {} ", appApplicationService.findCacheById(id, 1000L, 10000L));
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                countDownLatch.countDown();
            });
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
    }

    @Test
    @Disabled
    public void test() throws InterruptedException {
        clean();
        for (int i = 0; i < 2; i++) {
            long startTimes = System.currentTimeMillis();
            testCacheBreakdownInfo();
            long endTimes = System.currentTimeMillis();
            long runTime = (endTimes - startTimes);
            log.info("执行完毕:  {}ms", runTime);
            Thread.sleep(1000L);
        }
        clean();
    }

}
  • Console
[pool-2-thread-3]  lock
[pool-2-thread-3]  init data 
[pool-2-thread-3]  HikariPool-1 - Starting...
[pool-2-thread-8]  unlock ok
[pool-2-thread-4]   user {} 
[pool-2-thread-5]   user {} 
[pool-2-thread-9]   user {} 
[pool-2-thread-6]   user {} 
[pool-2-thread-7]   user {} 
[pool-2-thread-10]   user {} 
[pool-2-thread-8]   user {} 
[pool-2-thread-2]   user {} 
[pool-2-thread-1]   user {} 
[pool-2-thread-3]  HikariPool-1 - Start completed.
[pool-2-thread-3]  set data 
[pool-2-thread-3]   user {id=3, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=13100077310, sex=true, birth_date=1998-03-02 20:23:08.0, identity_card_number=460501198803127323} 
[main]  执行完毕:  1336ms
[pool-3-thread-7]  get cache
[pool-3-thread-10]  get cache
[pool-3-thread-9]  get cache
[pool-3-thread-4]  get cache
[pool-3-thread-6]  get cache
[pool-3-thread-5]  get cache
[pool-3-thread-2]  get cache
[pool-3-thread-3]  get cache
[pool-3-thread-1]  get cache
[pool-3-thread-8]  get cache
[pool-3-thread-3]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-10]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-7]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-1]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-8]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-9]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-5]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-2]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-4]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[pool-3-thread-6]   user {id=3.0, full_name=黛玉赵, email=daiyu.zhao@gmail.com, telephone_number=1.310007731E10, sex=true, birth_date=Mar 2, 1998 8:23:08 PM, identity_card_number=4.605011988031273E17} 
[main]  执行完毕:  1025ms

接口测试

package cn.z201.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author z201.coding@gmail.com
 **/
@RestController
public class AppApplicationController {

    @Autowired
    AppApplicationServiceImpl appApplicationService;

    @RequestMapping(value = "{id}")
    public Object index(@PathVariable(required = false) String id) {
        Map<String, Object> data = new HashMap<>();
        data.put("code", "200");
        if (null == id) {
            data.put("data", "请求参数不合法");
        }else{
            data.put("data", appApplicationService.findCacheById(id,1000L,10000L));
        }
        return data;
    }
}

  • Console
  docker-run curl http://127.0.0.1:9001/redis/1
{"code":"200","data":{"id":1,"full_name":"灵芸黄","email":"lingyun.huang@gmail.com","telephone_number":13842681484,"sex":true,"birth_date":"1996-05-23T21:17:45.000+00:00","identity_card_number":361405197211160626}}%     

小结

用 Docker Compose 编排 Spring Boot 与 MySQL、Redis 复现缓存穿透与击穿,以 setIfAbsent 加锁、Lua 比对后解锁,查无结果写入带过期时间的空值,十线程并发只回源一次。

最近更新: 2026/11/20 22:16
Contributors: 庆峰
Prev
Arthas:Java 线上问题诊断
Next
Docker 部署:Elasticsearch