Redis篇--常见问题篇2--缓存雪崩(过期时间分散,缓存预热,多级缓存)

1、概述

缓存雪崩是指在短时间内,大量的缓存同时失效或过期,导致大量的请求穿透到后端数据库或服务,从而引发系统负载骤增,甚至可能导致系统崩溃。这种情况通常发生在缓存的过期时间设置不合理时,所有缓存的过期时间相同,导致它们在同一时间点失效。

示意图:

2、解决方案

(1)、设置不同的过期时间(推荐)

为每个缓存项设置一个 随机的过期时间,而不是统一的过期时间。这样可以避免所有缓存项在同一时间点失效,分散了缓存失效的时间窗口,减少了对数据库的压力。
示例:

java 复制代码
 // 设置随机化的 TTL
  int baseTtl = 60; // 基础 TTL 为 60 秒
  int randomOffset = new Random().nextInt(20); // 随机偏移0-19秒
  redisTemplate.opsForValue().set("key", "value", baseTtl + randomOffset, TimeUnit.SECONDS);

(2)、缓存预热

在系统启动时或定期进行缓存预热,提前加载或有策略的定期加载一些常用的数据库数据到缓存中,确保缓存中有足够的数据,减少缓存失效时的冲击。

(3)、多级缓存

使用多级缓存策略,例如:本地缓存+分布式缓存。当分布式缓存失效时,本地缓存可以继续提供服务,减少对数据库的直接访问。

应用:本地缓存(如Caffeine、Guava Cache)来缓存频繁访问的数据,同时使用分布式缓存(如Redis、Memcached)来存储全局共享的数据。

本地缓存的TTL可以设置得比分布式缓存更短,以减少对分布式缓存的依赖。

多级缓存示例(Caffine+Redis)
第一步:导入依赖

java 复制代码
<dependency>
   <groupId>com.github.ben-manes.caffeine</groupId>
   <artifactId>caffeine</artifactId>
   <version>2.9.0</version>
</dependency>

第二步:注入配置类

java 复制代码
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public Cache<String,Object> caffeineCache() {    // 初始化注入CaffeineCache到容器
        return Caffeine.newBuilder()
                .initialCapacity(100)  // 初始容量为100
                .maximumSize(1000)   // 最大存1000个key
                .expireAfterWrite(1, TimeUnit.MINUTES)   // 设置1分钟过期
                .build();
    }
}

第三步:测试类

java 复制代码
import com.github.benmanes.caffeine.cache.Cache;
import com.zw.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "redis3", method = {RequestMethod.POST, RequestMethod.GET})
public class RedisController3 extends BaseController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    // 本地缓存CaffeineCache
    @Resource
    private Cache<String, Object> caffeineCache;

    // 模拟数据库查询
    private Object queryFromDatabase(String key) {
        // 模拟数据库查询逻辑
        System.out.println("Querying from database for key: " + key);
        return null; // 假设数据库中没有该数据
    }

    /**
     * 获取数据,优先从本地缓存获取,其次从 Redis 获取,最后从数据库获取
     * @param key 数据的唯一标识
     * @return 数据
     */
    @RequestMapping("/get")
    public Object getTest(String key) {
        // 1. 尝试从本地缓存获取
        Object cachedData = caffeineCache.getIfPresent(key);
        if (cachedData != null) {
            if ("NULL".equals(cachedData)) {
                return null; // 返回空对象
            }
            return cachedData;
        }

        // 2. 尝试从 Redis 获取
        cachedData = redisTemplate.opsForValue().get(key);
        if (cachedData != null) {
            if ("NULL".equals(cachedData)) {
                caffeineCache.put(key, "NULL"); // 将空对象放入本地缓存
                return null;
            }
            caffeineCache.put(key, cachedData); // 将数据放入本地缓存
            return cachedData;
        }

        // 3. 从数据库获取数据
        Object data = queryFromDatabase(key);
        if (data != null) {
            if (data != null) {
                // 更新本地缓存和 Redis
                caffeineCache.put(key, data.toString());
                redisTemplate.opsForValue().set(key, data, getRandomTtl(), TimeUnit.SECONDS);
            } else {
                // 缓存空对象
                caffeineCache.put(key, "NULL");
                redisTemplate.opsForValue().set(key, "NULL", 60, TimeUnit.SECONDS); // 空对象过期时间为 60 秒
            }
        }

        // 4. 返回旧的缓存数据(如果有)
        return caffeineCache.getIfPresent(key);
    }

    /**
     * 获取随机化的 TTL,避免所有缓存项在同一时间点失效
     *
     * @return 随机化的 TTL(秒)
     */
    private long getRandomTtl() {
        int baseTtl = 60; // 基础 TTL 为 60 秒
        int randomOffset = (int) (Math.random() * 10); // 随机偏移 0-9 秒
        return baseTtl + randomOffset;
    }
}

第四步:测试验证

可以正常查询有的值

没有的key直接返回null

相关推荐
ChineHe1 小时前
Redis数据类型篇002_详解Strings核心命令与存储结构
数据库·redis·缓存
清水白石0082 小时前
《从零到进阶:Pydantic v1 与 v2 的核心差异与零成本校验实现原理》
数据库·python
电商API&Tina2 小时前
京东 API 数据采集接口接入与行业分析
运维·服务器·网络·数据库·django·php
柠檬叶子C2 小时前
PostgreSQL 忘记 postgres 密码怎么办?(已解决)
数据库·postgresql
864记忆3 小时前
Qt创建连接注意事项
数据库·qt·nginx
毕设十刻3 小时前
基于Vue的迅读网上书城22f4d(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
薛定谔的猫19824 小时前
Langchain(十二)LangGraph 实战入门:用流程图思维构建 LLM 工作流
数据库·microsoft
坐吃山猪4 小时前
ChromaDB02-代码实战
数据库·向量数据库·chromadb
摇滚侠4 小时前
MySQL 中 utf8mb4 字符集,字母a占几个字节,一个汉字占几个字节 / MySQL 中 utf8mb3 字符集,字母a占几个字节,一个汉字占几个字节
数据库·mysql
ChineHe4 小时前
Redis数据类型篇001_数据类型梳理与选择指南
数据库·redis·缓存