ToB企服应用市场:ToB评测及商务社交产业平台

标题: 【缓存】缓存雪崩与缓存穿透:高并发体系的隐形杀手 [打印本页]

作者: 勿忘初心做自己    时间: 15 小时前
标题: 【缓存】缓存雪崩与缓存穿透:高并发体系的隐形杀手
缓存雪崩与缓存穿透:高并发体系的隐形杀手

在高并发体系中,缓存是提拔性能的紧张手段。然而,缓存使用不妥也会带来一系列问题,其中最常见的就是缓存雪崩缓存穿透。这两个问题假如不加以办理,大概会导致体系崩溃,甚至引发严重的生产变乱。本文将深入探讨缓存雪崩和缓存穿透的成因,并提供办理方案,最后用Java代码实现。
一、缓存雪崩

1.1 什么是缓存雪崩?

缓存雪崩是指大量缓存数据在同一时间失效,导致所有哀求都直接打到数据库上,数据库刹时承受巨大压力,甚至崩溃。
1.2 缓存雪崩的成因


1.3 办理方案

1.4 Java实现

  1. import java.util.Random;
  2. import java.util.concurrent.TimeUnit;
  3. public class CacheAvalancheSolution {
  4.     private static final int BASE_EXPIRE_TIME = 3600; // 基础过期时间
  5.     private static final int RANDOM_RANGE = 600; // 随机范围
  6.     public static void main(String[] args) {
  7.         // 模拟缓存数据
  8.         String cacheKey = "hot_data";
  9.         String cacheValue = getDataFromCache(cacheKey);
  10.         if (cacheValue == null) {
  11.             // 缓存失效,重新加载
  12.             cacheValue = getDataFromDB();
  13.             setCacheWithRandomExpire(cacheKey, cacheValue);
  14.         }
  15.         System.out.println("Cache Value: " + cacheValue);
  16.     }
  17.     private static String getDataFromCache(String key) {
  18.         // 模拟从缓存中获取数据
  19.         return null; // 假设缓存失效
  20.     }
  21.     private static String getDataFromDB() {
  22.         // 模拟从数据库中获取数据
  23.         return "Data from DB";
  24.     }
  25.     private static void setCacheWithRandomExpire(String key, String value) {
  26.         // 设置缓存,并添加随机过期时间
  27.         int expireTime = BASE_EXPIRE_TIME + new Random().nextInt(RANDOM_RANGE);
  28.         System.out.println("Set cache with expire time: " + expireTime + " seconds");
  29.         // 实际项目中可以使用Redis等缓存工具
  30.     }
  31. }
复制代码
二、缓存穿透

2.1 什么是缓存穿透?

缓存穿透是指查询一个不存在的数据,由于缓存中没有该数据,哀求直接打到数据库上。假如大量哀求查询不存在的数据,数据库大概会被压垮。
2.2 缓存穿透的成因


2.3 办理方案

2.4 Java实现

  1. import com.google.common.hash.BloomFilter;
  2. import com.google.common.hash.Funnels;
  3. public class CachePenetrationSolution {
  4.     private static BloomFilter<String> bloomFilter = BloomFilter.create(
  5.             Funnels.stringFunnel(), 1000000, 0.01); // 布隆过滤器
  6.     public static void main(String[] args) {
  7.         // 模拟查询
  8.         String queryKey = "non_existent_key";
  9.         if (!bloomFilter.mightContain(queryKey)) {
  10.             // 布隆过滤器判断不存在
  11.             System.out.println("Data not exist in bloom filter");
  12.             return;
  13.         }
  14.         String cacheValue = getDataFromCache(queryKey);
  15.         if (cacheValue == null) {
  16.             // 缓存失效,重新加载
  17.             cacheValue = getDataFromDB(queryKey);
  18.             if (cacheValue == null) {
  19.                 // 数据库中没有该数据,缓存空值
  20.                 setCacheWithShortExpire(queryKey, "NULL");
  21.             } else {
  22.                 setCacheWithShortExpire(queryKey, cacheValue);
  23.             }
  24.         }
  25.         System.out.println("Cache Value: " + cacheValue);
  26.     }
  27.     private static String getDataFromCache(String key) {
  28.         // 模拟从缓存中获取数据
  29.         return null; // 假设缓存失效
  30.     }
  31.     private static String getDataFromDB(String key) {
  32.         // 模拟从数据库中获取数据
  33.         return null; // 假设数据库中不存在该数据
  34.     }
  35.     private static void setCacheWithShortExpire(String key, String value) {
  36.         // 设置缓存,并添加较短的过期时间
  37.         System.out.println("Set cache with short expire time for key: " + key);
  38.         // 实际项目中可以使用Redis等缓存工具
  39.     }
  40. }
复制代码
三、总结

缓存雪崩和缓存穿透是高并发体系中常见的缓存问题,假如不加以办理,大概会导致体系崩溃。通过设置差别的逾期时间、使用布隆过滤器、缓存空值等方法,可以有效避免这些问题。在实际项目中,我们须要根据业务场景选择合适的办理方案,确保体系的稳定性和高性能。

关注我,获取更多技术干货!假如你有任何问题或建议,欢迎在批评区留言。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4