搭建Caffeine+Redis多级缓存机制

本地缓存的简单实现方案有HashMap,CucurrentHashMap,成熟的本地缓存方案有Guava 与 Caffeine ,企业级应用推荐下面说下两者的区别

1. 核心异同对比

特性Guava CacheCaffeine
诞生背景Google Guava 库的一部分(2011年)基于 Guava Cache 重构的现代缓存库(2015+)
性能中等(锁竞争较多)极高(优化并发设计,吞吐量提升5~10倍)
内存管理基于 LRU 算法结合 W-TinyLFU 算法(高命中率)
过期策略支持 expireAfterWrite/access支持 expireAfterWrite/access + refresh
缓存回收同步阻塞异步非阻塞(后台线程)
监控统计基础统计(命中率等)详细统计(命中率、加载时间等)
依赖需引入整个 Guava 库轻量(仅依赖 Caffeine)
社区维护维护模式(新功能少)活跃更新(Java 17+ 兼容)

从上面的比较可知, Caffeine 各方面是优于Guava的,因此在搭建多级缓存机制时,建议使用Caffeine+Redis的组合方案。

业务执行流程

  1. 请求优先读取 Caffeine 本地缓存(超快,减少网络IO)。

  2. 本地缓存未命中 → 读取 Redis 分布式缓存

  3. Redis 未命中 → 查询数据库,并回填到两级缓存。

下面介绍下实现方式

注意:下面的实现方式是基于Springboot 2.4+,版本不同,配置上会略有差异

1.maven中引入下面的依赖

<!-- Caffeine 本地缓存 -->
<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId>
</dependency><!-- 缓存抽象层 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency><!-- redis 缓存操作 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><exclusions><exclusion><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>
</dependency>

 2.application中进行配置

spring: cache:caffeine:spec: maximumSize=1000,expireAfterWrite=10m  # 本地缓存redis:time-to-live: 1h  # Redis缓存过期时间# redis 配置redis:# 地址host: 127.0.0.1# 端口,默认为6379port: 6379# 数据库索引database: 0# 密码password: abc123# 连接超时时间timeout: 6000ms  # 连接超时时长(毫秒)jedis:pool:max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)max-wait: -1ms      # 连接池最大阻塞等待时间(使用负值表示没有限制)max-idle: 10      # 连接池中的最大空闲连接min-idle: 5       # 连接池中的最小空闲连接

 3.自定义多级缓存管理器


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.support.CompositeCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.github.benmanes.caffeine.cache.Caffeine;import java.util.concurrent.TimeUnit;@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class MyCacheConfig {@Beanpublic RedisCacheConfiguration cacheConfiguration(CacheProperties cacheProperties) {RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));CacheProperties.Redis redisProperties = cacheProperties.getRedis();if (redisProperties.getTimeToLive() != null) {config = config.entryTtl(redisProperties.getTimeToLive());}if (redisProperties.getKeyPrefix() != null) {config = config.prefixKeysWith(redisProperties.getKeyPrefix());}if (!redisProperties.isCacheNullValues()) {config = config.disableCachingNullValues();}if (!redisProperties.isUseKeyPrefix()) {config = config.disableKeyPrefix();}return config;}@Beanpublic Caffeine<Object, Object> caffeineConfig() {return Caffeine.newBuilder().maximumSize(1000).expireAfterWrite(10, TimeUnit.MINUTES);}@Bean@Primary  // 添加 @Primary 注解指定 CaffeineCacheManager 作为默认的缓存管理器public CacheManager caffeineCacheManager(Caffeine<Object, Object> caffeine) {CaffeineCacheManager manager = new CaffeineCacheManager();manager.setCaffeine(caffeine);return manager;}@Beanpublic RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory,RedisCacheConfiguration cacheConfiguration) {return RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(cacheConfiguration).build();}@Beanpublic CacheManager compositeCacheManager(@Qualifier("caffeineCacheManager") CacheManager caffeineCacheManager,@Qualifier("redisCacheManager") CacheManager redisCacheManager) {return new CompositeCacheManager(caffeineCacheManager,redisCacheManager);}
}

4.业务逻辑层调用

使用示例:

@Service
public class ProductService {@Autowiredprivate ProductRepository repository;// 优先读本地缓存,其次Redis,最后数据库@Cacheable(cacheNames = "product", key = "#id")public Product getProductById(Long id) {return repository.findById(id).orElseThrow();}// 更新数据时清除两级缓存@CacheEvict(cacheNames = "product", key = "#product.id")public Product updateProduct(Product product) {return repository.save(product);}
}

手动控制多级缓存

@Service
public class CacheService {@Autowiredprivate CacheManager cacheManager;@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public Product getProductWithManualControl(Long id) {// 1. 先查本地缓存Cache caffeineCache = cacheManager.getCache("product");Product product = caffeineCache.get(id, Product.class);if (product != null) {return product;}// 2. 查Redis缓存product = (Product) redisTemplate.opsForValue().get("product:" + id);if (product != null) {// 回填本地缓存caffeineCache.put(id, product);return product;}// 3. 查数据库product = repository.findById(id).orElseThrow();// 回填两级缓存redisTemplate.opsForValue().set("product:" + id, product, Duration.ofHours(1));caffeineCache.put(id, product);return product;}
}
  1. 缓存一致性

    • 使用 @CacheEvict 或 Redis Pub/Sub 同步失效两级缓存。

    • 示例:通过 Redis 消息通知其他节点清理本地缓存。

  2. 防止缓存击穿

    • Caffeine 配置 refreshAfterWrite

Caffeine.newBuilder().refreshAfterWrite(5, TimeUnit.MINUTES).build(key -> loadFromRedisOrDb(key));

3.监控统计:

Caffeine 统计:cache.getNativeCache().stats()

Redis 统计:INFO commandstats

4. 验证多级缓存
本地缓存生效:连续调用同一接口,观察第二次响应时间骤降。
Redis 缓存生效:重启应用后,首次请求仍快速返回(数据来自Redis)。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/79874.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Linux系统】第四节—详解yum+vim

hello 我是云边有个稻草人 Linux—本节课所属专栏—欢迎订阅—持续更新中~ 目录 画板—本节课知识点详解 一、软件包管理器 1.1 什么是软件包 1.2 Linux软件⽣态 1.3 yum具体操作 【查看软件包】 【安装软件】 【卸载软件】 【注意事项】 1.4 安装源 二、vim 2.1 …

EasyRTC嵌入式音视频通信SDK打造带屏IPC全场景实时通信解决方案

一、方案概述​ 在智能安防与物联网快速发展的背景下&#xff0c;带屏IPC&#xff08;网络摄像机&#xff09;不仅承担着视频采集与监控的基础功能&#xff0c;还逐渐向多样化交互与智能化方向演进。EasyRTC作为一款强大的实时通信框架&#xff0c;具备低延迟、高稳定性、跨平…

Linux下的c/c++开发之操作Redis数据库

C/C 操作 Redis 的常用库 在 C/C 开发中操作 Redis 有多种方式&#xff0c;最主流的选择是使用第三方客户端库。由于 Redis 官方本身是使用 C 编写的&#xff0c;提供的 API 非常适合 C/C 调用。常见的 Redis C/C 客户端库包括&#xff1a; hiredis&#xff1a;官方推荐的轻量…

go 通过汇编学习atomic原子操作原理

文章目录 概要一、原理1.1、案例1.2、关键汇编 二、LOCK汇编指令2.1、 LOCK2.2、 原理2.2.1、 缓存行2.2.2、 缓存一致性之MESI协议2.2.3、lock原理 三、x86缓存发展四、x86 DMA发展参考 概要 在并发操作下&#xff0c;对一个简单的aa2的操作都会出错&#xff0c;这是因为这样…

mapreduce打包运行

maven打包 MapReduce是一个分布式运算程序的编程框架&#xff0c;是用户开发“基于Hadoop的数据分析应用”的核心框架。 MapReduce核心功能是将用户编写的业务逻辑代码和自带默认组件整合成一个完整的分布式运算程序&#xff08;例如&#xff1a;jar包&#xff09;&#xff0…

小白成长之路-LInux系统文件与目录管理(二)

提示&#xff1a;第二部分对第一部分收尾 文章目录 常见的命令如下一、文件查看命令1. more命令2.less命令3.head命令4.tail命令5.nl命令&#xff08;了解&#xff09;6.创建目录命令7.创建文件命令>: 覆盖重定向>>: 追加重定向 8.touch命令9.echo命令10.文件或目录复…

JVM之虚拟机运行

虚拟机运行快速复习 try-catch&#xff1a;catch-异常表栈展开&#xff0c;finally-代码复制异常表兜底 类的生命周期&#xff1a;加载&#xff0c;连接&#xff08;验证&#xff0c;准备&#xff0c;解析&#xff09;&#xff0c;初始化&#xff0c;使用&#xff0c;卸载 类…

AI数字人实现原理

随着人工智能与数字技术的快速发展&#xff0c;AI数字人&#xff08;Digital Human&#xff09;作为新一代人机交互媒介&#xff0c;正在多个行业中快速落地。无论是在虚拟主播、在线客服、教育培训&#xff0c;还是在数字代言、元宇宙中&#xff0c;AI数字人都扮演着越来越重要…

Android开发-数据库SQLite

在Android应用开发中&#xff0c;当需要存储结构化数据时&#xff0c;SQLite是一个非常强大的工具。SQLite是一款轻量级的关系型数据库管理系统&#xff0c;它内嵌于Android系统中&#xff0c;支持SQL语法&#xff0c;并且不需要单独的服务器进程或系统配置。本文将介绍如何在A…

android实现USB通讯

在 Android 上枚举 USB 设备除了使用 UsbManager.getDeviceList() 方法外&#xff0c;还有以下几种常见的方式&#xff1a; 1. 使用 USB 设备过滤器&#xff08;XML 配置&#xff09; 通过在 AndroidManifest.xml 中配置 USB 设备过滤器&#xff0c;可以让系统自动检测并通知…

FFmpeg视频编码的完整操作指南

步骤如下&#xff1a; 安装和准备FFmpeg&#xff1a;确保包含所需编码器&#xff08;如libx264&#xff09;。基本命令行编码&#xff1a;使用ffmpeg命令进行转码&#xff0c;设置视频编码器、CRF、预设等。API编码流程&#xff08;针对开发者&#xff09;&#xff1a; a. 注册…

鸿蒙 UIAbility组件与UI的数据同步和窗口关闭

使用 EventHub 进行数据通信 Stage模型概念图 根据 Stage 模型概念图 UIAbility 先于 ArkUI Page 创建 所以&#xff0c;事件要先 .on 订阅 再 emit 发布 假如现在有页面 Page1 和他的 UIAbility // src/main/ets/page1ability/Page1Ability.ets onCreate(want: Want, laun…

全栈工程师实战手册:LuatOS日志系统开发指南!

本文聚焦LuatOS-log库的实战应用场景&#xff0c;通过完整案例演示日志模块集成、格式定制及远程同步方案&#xff0c;帮助全栈开发者构建灵活可靠的日志管理框架。下面&#xff0c;我们一起来认识LuatOS的log库&#xff01; 一、 log.info() log info()主要打印一些正常的…

STM32-USART串口通信(9)

一、通信接口介绍 通信的目的&#xff1a;将一个设备的数据传送到另一个设备&#xff0c;扩展硬件系统。 当STM32想要实现一些功能&#xff0c;但是需要外挂一些其他模块才能实现&#xff0c;这就需要在两个设备之间连接上一根或多跟通信线&#xff0c;通过通信线路发送或者接…

【MoveIt 2】使用 MoveIt 任务构造器(MoveIt Task Constructor)进行拾取和放置

本教程将引导您创建一个使用 MoveIt 任务构造器规划抓取和放置操作的包。MoveIt 任务构造器&#xff08;https://github.com/moveit/moveit_task_constructor/tree/ros2/&#xff09;提供了一种为包含多个不同子任务&#xff08;称为阶段&#xff09;的任务进行规划的方法。如果…

破解商业综合体清洁管理困局:商业空间AI智能保洁管理系统全场景解决方案

方案整体概述 随着商业综合体日益向智能化、精细化管理转型&#xff0c;传统保洁工作面临人员监管难、清洁效果评估难、应急响应滞后等诸多挑战。为解决这些痛点&#xff0c;本系统依托计算机视觉、行为识别、图像分割与深度学习等AI技术&#xff0c;构建一套集人员管理、工作…

spring响应式编程系列:异步消费数据

目录 示例 大致流程 parallel cache PARALLEL_SUPPLIER newParallel init publishOn new MonoSubscribeOnValue ​​​​​​​subscribe ​​​​​​​new LambdaMonoSubscriber ​​​​​​​MonoSubscribeOnValue.subscribe ​​​​​​​onSubscribe ​​…

视频编解码学习十二之Android疑点

一、android.view.SurfaceControl.setDisplaySurface的作用 android.view.SurfaceControl.setDisplaySurface 是 Android 系统中一个 native 层级别的 API&#xff0c;主要用于 设置某个物理显示屏&#xff08;Display&#xff09;的输出 Surface&#xff0c;属于 SurfaceFlin…

家用或办公 Windows 电脑玩人工智能开源项目配备核显的必要性(含 NPU 及显卡类型补充)

一、GPU 与显卡的概念澄清 首先需要明确一个容易误解的概念&#xff1a;GPU 不等同于显卡。 显卡和GPU是两个不同的概念。 【概念区分】 在讨论图形计算领域时&#xff0c;需首先澄清一个常见误区&#xff1a;GPU&#xff08;图形处理单元&#xff09;与显卡&#xff08;视…

Python----神经网络(《Deep Residual Learning for Image Recognition》论文和ResNet网络结构)

一、论文 1.1、论文基本信息 标题&#xff1a;Deep Residual Learning for Image Recognition 作者&#xff1a;Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun 单位&#xff1a;Microsoft Research 会议&#xff1a;CVPR 2016 主要贡献&#xff1a;提出了一种深度残…