news 2026/4/18 3:13:01

多级缓存必要性

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
多级缓存必要性

Java多级缓存设计:应对微博明星官宣的高并发场景

一、多级缓存原理与必要性

1.1 为什么需要多级缓存?

单级缓存的问题:

  • 性能瓶颈:所有请求都打到同一缓存层,压力集中
  • 容错性差:缓存层故障直接影响整体可用性
  • 网络开销:分布式缓存频繁网络IO
  • 热点数据压力:明星官宣等热点事件导致缓存击穿

1.2 多级缓存的核心思想

客户端 → 本地缓存(L1) → 分布式缓存(L2) → 数据库 ↑ ↑ ↑ 最快访问 内存级 共享缓存 毫秒级响应 纳秒级访问 微秒级访问

二、多级缓存设计架构

2.1 典型四级缓存架构

// 架构示意┌─────────────────────────────────────────┐ │ 客户端缓存(L0)│ │(App/Web端缓存,HTTP缓存)│ └─────────────────────────────────────────┘ │ ┌─────────────────────────────────────────┐ │ 本地缓存(L1)│ │(Caffeine/GuavaCache,JVM进程内)│ └─────────────────────────────────────────┘ │ ┌─────────────────────────────────────────┐ │ 分布式缓存(L2)│ │(RedisCluster/RedisSentinel)│ └─────────────────────────────────────────┘ │ ┌─────────────────────────────────────────┐ │ 数据库缓存/持久层(L3)│ │(MySQLQueryCache/数据库连接池)│ └─────────────────────────────────────────┘

2.2 核心设计要点

publicclassMultiLevelCacheConfig{// 1. 本地缓存配置@BeanpublicCache<String,Object>localCache(){returnCaffeine.newBuilder().maximumSize(10_000)// 最大容量.expireAfterWrite(5,TimeUnit.SECONDS)// 短暂过期时间.expireAfterAccess(2,TimeUnit.SECONDS).recordStats()// 记录统计信息.build();}// 2. 分布式缓存配置@BeanpublicRedisCacheManagerredisCacheManager(RedisConnectionFactoryfactory){RedisCacheConfigurationconfig=RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(30))// 比本地缓存长.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(newJackson2JsonRedisSerializer<>(Object.class)));returnRedisCacheManager.builder(factory).cacheDefaults(config).build();}}

三、具体实现方案

3.1 缓存加载策略

@ComponentpublicclassMultiLevelCacheService{@AutowiredprivateCache<String,Object>localCache;@AutowiredprivateRedisTemplate<String,Object>redisTemplate;@AutowiredprivateDataServicedataService;/** * 多级缓存读取流程 */publicObjectgetWithMultiLevel(Stringkey){// 1. 查询本地缓存 (L1)Objectvalue=localCache.getIfPresent(key);if(value!=null){recordCacheHit("local");returnvalue;}// 2. 查询分布式缓存 (L2)value=redisTemplate.opsForValue().get(key);if(value!=null){// 回填本地缓存localCache.put(key,value);recordCacheHit("redis");returnvalue;}// 3. 防止缓存击穿:使用分布式锁StringlockKey="lock:"+key;RLocklock=redissonClient.getLock(lockKey);try{if(lock.tryLock(100,10,TimeUnit.MILLISECONDS)){// 双重检查value=redisTemplate.opsForValue().get(key);if(value!=null){localCache.put(key,value);returnvalue;}// 4. 查询数据库 (L3)value=dataService.getFromDB(key);if(value!=null){// 写入各级缓存redisTemplate.opsForValue().set(key,value,60,TimeUnit.SECONDS);localCache.put(key,value);}else{// 缓存空值防止缓存穿透cacheNullValue(key);}returnvalue;}else{// 等待其他线程加载Thread.sleep(50);returnredisTemplate.opsForValue().get(key);}}finally{lock.unlock();}}}

3.2 热点数据特殊处理

@ComponentpublicclassHotspotCacheManager{// 热点数据本地缓存(更长时间)privateCache<String,Object>hotspotCache=Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(30,TimeUnit.SECONDS).build();// 热点Key探测privateConcurrentHashMap<String,AtomicInteger>keyAccessCounter=newConcurrentHashMap<>();/** * 热点探测与特殊缓存 */publicObjectgetWithHotspotDetection(Stringkey){// 访问计数keyAccessCounter.computeIfAbsent(key,k->newAtomicInteger(0)).incrementAndGet();// 判断是否为热点(例如:10秒内访问超过100次)if(isHotspotKey(key)){// 从热点专用缓存获取Objectvalue=hotspotCache.getIfPresent(key);if(value!=null){returnvalue;}// 热点数据预加载和特殊缓存value=loadHotspotData(key);hotspotCache.put(key,value);// 延长分布式缓存时间redisTemplate.opsForValue().set(key,value,300,TimeUnit.SECONDS);returnvalue;}// 普通数据走常规流程returnmultiLevelCacheService.getWithMultiLevel(key);}privatebooleanisHotspotKey(Stringkey){AtomicIntegercounter=keyAccessCounter.get(key);returncounter!=null&&counter.get()>100;}}

3.3 缓存一致性保证

@ComponentpublicclassCacheConsistencyManager{@AutowiredprivateRedisPubSubredisPubSub;/** * 缓存更新时的多级同步 */@TransactionalpublicvoidupdateData(Stringkey,ObjectnewValue){// 1. 更新数据库dataService.updateDB(key,newValue);// 2. 删除各级缓存(先删后更新策略)deleteMultiLevelCache(key);// 3. 异步更新缓存cacheUpdateExecutor.execute(()->{// 延迟双删try{Thread.sleep(500);deleteMultiLevelCache(key);}catch(InterruptedExceptione){Thread.currentThread().interrupt();}});}privatevoiddeleteMultiLevelCache(Stringkey){// 删除本地缓存localCache.invalidate(key);// 删除分布式缓存redisTemplate.delete(key);// 发布缓存失效消息(其他节点监听)redisPubSub.publish("cache.invalidate",key);}/** * 监听缓存失效消息 */@RedisListener(topic="cache.invalidate")publicvoidonCacheInvalidate(Stringkey){localCache.invalidate(key);}}

四、完整的多级缓存实现示例

4.1 缓存管理器

@Slf4j@ComponentpublicclassMultiLevelCacheManager{// 各级缓存配置@Data@ConfigurationProperties(prefix="cache.multi-level")publicstaticclassCacheConfig{privateLocalCacheConfiglocal=newLocalCacheConfig();privateRedisCacheConfigredis=newRedisCacheConfig();@DatapublicstaticclassLocalCacheConfig{privateintmaximumSize=10000;privatelongexpireAfterWrite=5000;// msprivatelongexpireAfterAccess=2000;// ms}@DatapublicstaticclassRedisCacheConfig{privatelongdefaultExpire=30000;// msprivatelonghotspotExpire=300000;// msprivateStringkeyPrefix="cache:";}}@AutowiredprivateCacheConfigconfig;// 本地缓存实例privateLoadingCache<String,Object>localCache;@PostConstructpublicvoidinit(){localCache=Caffeine.newBuilder().maximumSize(config.getLocal().getMaximumSize()).expireAfterWrite(config.getLocal().getExpireAfterWrite(),TimeUnit.MILLISECONDS).expireAfterAccess(config.getLocal().getExpireAfterAccess(),TimeUnit.MILLISECONDS).recordStats().build(key->loadFromRedis(key));}/** * 核心获取方法 */publicObjectget(Stringkey){try{// 1. 尝试本地缓存returnlocalCache.get(key);}catch(Exceptione){log.warn("Local cache get failed for key: {}",key,e);// 2. 降级到Redistry{Objectvalue=redisTemplate.opsForValue().get(config.getRedis().getKeyPrefix()+key);if(value!=null){// 异步回填本地缓存CompletableFuture.runAsync(()->localCache.put(key,value));}returnvalue;}catch(Exceptionex){log.error("Redis cache get failed for key: {}",key,ex);// 3. 最后尝试数据库returndataService.getFromDB(key);}}}/** * 带降级的批量获取(适用于微博Feed流) */publicMap<String,Object>batchGet(List<String>keys){Map<String,Object>result=newHashMap<>();List<String>missingKeys=newArrayList<>();// 1. 批量查询本地缓存for(Stringkey:keys){Objectvalue=localCache.getIfPresent(key);if(value!=null){result.put(key,value);}else{missingKeys.add(key);}}// 2. 批量查询Redis(使用pipeline优化)if(!missingKeys.isEmpty()){List<Object>redisValues=redisTemplate.executePipelined(connection->{for(Stringkey:missingKeys){connection.stringCommands().get((config.getRedis().getKeyPrefix()+key).getBytes());}returnnull;});// 处理Redis结果并回填本地缓存for(inti=0;i<missingKeys.size();i++){Stringkey=missingKeys.get(i);Objectvalue=redisValues.get(i);if(value!=null){result.put(key,value);localCache.put(key,value);}}}returnresult;}}

4.2 监控与降级

@ComponentpublicclassCacheMonitor{@AutowiredprivateLoadingCache<String,Object>localCache;@AutowiredprivateMeterRegistrymeterRegistry;privateGaugelocalCacheSize;privateCountercacheHitCounter;privateCountercacheMissCounter;@PostConstructpublicvoidinitMetrics(){// 监控本地缓存指标localCacheSize=Gauge.builder("cache.local.size",localCache,cache->cache.estimatedSize()).register(meterRegistry);cacheHitCounter=Counter.builder("cache.hit.total").tag("level","local").register(meterRegistry);cacheMissCounter=Counter.builder("cache.miss.total").tag("level","local").register(meterRegistry);// 定时采集缓存统计ScheduledExecutorServiceexecutor=Executors.newSingleThreadScheduledExecutor();executor.scheduleAtFixedRate(this::recordStats,1,1,TimeUnit.MINUTES);}privatevoidrecordStats(){CacheStatsstats=localCache.stats();Metrics.counter("cache.hit.rate").increment((long)(stats.hitRate()*100));Metrics.counter("cache.miss.rate").increment((long)(stats.missRate()*100));// 记录命中率到日志if(stats.hitRate()<0.8){log.warn("Local cache hit rate is low: {}",stats.hitRate());}}/** * 动态调整缓存策略 */@Scheduled(fixedDelay=60000)publicvoidadjustCachePolicy(){CacheStatsstats=localCache.stats();// 根据命中率动态调整if(stats.hitRate()>0.9){// 命中率高,可以适当增加缓存时间// ...}elseif(stats.hitRate()<0.6){// 命中率低,可能需要调整缓存策略// ...}}}

五、配置与部署建议

5.1 application.yml配置

# 多级缓存配置cache:multi-level:local:maximum-size:10000expire-after-write:5000expire-after-access:2000redis:default-expire:30000hotspot-expire:300000key-prefix:"weibo:cache:"cluster:nodes:redis1:6379,redis2:6379,redis3:6379# 热点检测配置hotspot:detection:enabled:truethreshold:100# 10秒内访问次数window-seconds:10preload:true# 是否预加载# 降级配置circuit-breaker:cache:enabled:truefailure-threshold:50timeout-ms:100

5.2 部署架构建议

┌─────────────────────────────────────────────────────┐ │ Load Balancer (Nginx) │ └─────────────────────────────────────────────────────┘ │ ┌───────────────┼───────────────┐ │ │ │ ┌─────┐ ┌─────┐ ┌─────┐ │App 1│ │App 2│ │App 3│ (Java应用集群) │L1 │ │L1 │ │L1 │ (本地缓存) └─────┘ └─────┘ └─────┘ │ │ │ └───────────────┼───────────────┘ │ ┌─────────────────────────────────────────────────────┐ │ Redis Cluster (L2缓存) │ │ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ │ │Redis│ │Redis│ │Redis│ │Redis│ │ │ └─────┘ └─────┘ └─────┘ └─────┘ │ └─────────────────────────────────────────────────────┘ │ ┌─────────────────────────────────────────────────────┐ │ 数据库集群 (MySQL集群) │ │ 主从复制 + 读写分离 │ └─────────────────────────────────────────────────────┘

六、针对微博场景的特殊优化

6.1 明星官宣场景处理

@ComponentpublicclassCelebrityAnnouncementHandler{// 预加载机制@EventListenerpublicvoidhandleAnnouncementEvent(CelebrityEventevent){StringcelebrityId=event.getCelebrityId();// 1. 预热缓存preloadCelebrityData(celebrityId);// 2. 动态扩容缓存容量adjustCacheCapacity(celebrityId);// 3. 设置特殊缓存策略setSpecialCachePolicy(celebrityId);}privatevoidpreloadCelebrityData(StringcelebrityId){// 提前加载相关数据到各级缓存List<String>cacheKeys=generateCacheKeys(celebrityId);cacheKeys.parallelStream().forEach(key->{// 从数据库加载Objectdata=dataService.getCelebrityData(key);// 写入Redis,设置较长TTLredisTemplate.opsForValue().set(key,data,1,TimeUnit.HOURS);// 推送到消息队列,让其他节点也预热kafkaTemplate.send("cache-preload",key);});}}

七、总结

关键设计原则:

  1. 分层设计:L0→L1→L2→L3,逐层降级
  2. 容量规划:各级缓存容量呈倒金字塔形
  3. 过期策略:越靠近用户,过期时间越短
  4. 一致性保障:通过消息同步或延迟双删
  5. 降级熔断:任何一级缓存失败不影响整体可用性
  6. 热点探测:动态识别并特殊处理热点数据

性能预期:

  • 本地缓存命中:< 1ms
  • Redis缓存命中:1-5ms
  • 数据库查询:10-100ms
  • 整体命中率:> 99%

这种多级缓存设计能有效应对微博明星官宣等突发高并发场景,通过分级存储、热点探测、预加载等策略,保证系统在高并发下的稳定性和性能。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/17 21:40:03

attn_scores注意力分计算-记录

querys 是 2*6*2&#xff0c;keys也是2*6*2 attn_scores querys keys.transpose(1,2)attn_scores 最终维度是 266&#xff1b;数值上是两个矩阵的批量矩阵乘法结果&#xff0c;每个样本对应一个 66 的注意力分数矩阵 transpose(1,2) 交换 keys 的第 1 维和第 2 维&#xff1…

作者头像 李华
网站建设 2026/4/17 22:19:35

为什么标准化要用均值0和方差1?

为什么标准化要把均值设为0、方差设为1&#xff1f; 先说均值。均值就是平均数&#xff0c;所有观测值加起来除以个数。 μ是均值&#xff0c;n是数据点总数&#xff0c;xᵢ是每个数据点&#xff0c;所以均值就是数据的重心位置。比如均值是20&#xff0c;那20就是平衡点。这…

作者头像 李华
网站建设 2026/4/16 10:28:42

人群仿真软件:Vadere_(2).Vadere基本操作与界面

Vadere基本操作与界面 启动Vadere 启动过程 Vadere是一款基于Java的人群仿真软件&#xff0c;可以通过以下步骤启动&#xff1a; 安装Java环境&#xff1a;确保您的计算机上安装了Java运行环境&#xff08;JRE&#xff09;或Java开发工具包&#xff08;JDK&#xff09;。您可…

作者头像 李华
网站建设 2026/4/17 16:35:13

第三十四周 学习周报

摘要 今日通过排气岐管的模型学习了CFD前处理中几何结构定义的关键设置&#xff0c;通过回答模型类型、开口封堵及区域抽取等核心问题&#xff0c;将实体CAD模型转化为可用于流体仿真的计算域&#xff0c;为网格划分奠定基础。 Abstract Today, through the exhaust manifol…

作者头像 李华
网站建设 2026/4/17 23:15:49

大模型学习全攻略:零基础入门到进阶,附实战项目教程

文章提供大模型学习完整路径&#xff0c;从底层原理(Transformer架构、预训练vs微调)到微调技术(七阶段八方法)&#xff0c;推荐新手掌握Prompt TuningLoRA。包含"新闻分类大模型微调项目"实战案例&#xff0c;提供完整代码和部署流程。给出三条建议&#xff1a;先通…

作者头像 李华