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

相关推荐
betazhou14 分钟前
Oracle dgbroker常规命令管理简介
数据库·oracle·adg·dbbroker
海边夕阳20061 小时前
PostgreSQL性能调优:解决表膨胀、索引碎片和无效索引问题
数据库·经验分享·postgresql·性能优化
陈果然DeepVersion1 小时前
Java大厂面试真题:Spring Boot+微服务+AI智能客服三轮技术拷问实录(四)
spring boot·redis·微服务·kafka·spring security·智能客服·java面试
一 乐1 小时前
个人理财系统|基于java+小程序+APP的个人理财系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·后端·小程序
m0_748248021 小时前
Redis的数据淘汰策略解读
数据库·redis·缓存
哥哥还在IT中2 小时前
让数据库更智能-大模型如何优化我们的SQL查询
数据库·sql
计算机小手2 小时前
探索 Maxwell:高效捕获 MySQL 数据变更的轻量级中间件
数据库·经验分享·mysql·开源软件
IvorySQL2 小时前
使用 PostgreSQL 时间点恢复(Point-In-Time Recovery)的多种数据恢复技术
数据库·postgresql
Freed&2 小时前
《Nginx进阶实战:反向代理、负载均衡、缓存优化与Keepalived高可用》
nginx·缓存·负载均衡
腾讯云云开发2 小时前
小程序数据库权限管理,一看就会!——CloudBase新手指南
前端·数据库·微信小程序