当前位置: 首页 > news >正文

高性能 Java 本地缓存 Caffeine 框架介绍及在 SpringBoot 中的使用

在现代应用程序中,缓存是一种重要的性能优化技术,它可以显著减少数据访问延迟,降低服务器负载,提高系统的响应速度。特别是在高并发的场景下,合理地使用缓存能够有效提升系统的稳定性和效率。

Caffeine 是一个高性能的 Java 本地缓存库,以其近乎最佳的命中率和灵活的配置选项在众多缓存解决方案中脱颖而出。它的设计目标是提供一个简单但功能强大的缓存实现,支持多种缓存策略,包括基于大小的淘汰、基于时间的过期、弱引用和软引用等。


文章目录

      • 1、Caffeine Cache 介绍
      • 2、Caffeine Cache 使用示例
        • 2.1、依赖引入
        • 2.2、示例代码
      • 3、Caffeine Cache 在 SpringBoot 中的使用
        • 3.1、依赖引入
        • 3.2、配置文件
        • 3.3、启用缓存
        • 3.4、配置缓存管理器
        • 3.5、使用缓存


1、Caffeine Cache 介绍

Caffeine 是一个用于 Java 的缓存库,其设计目标是高性能,和近乎最佳的命中率。缓存库的主要功能是存储和快速检索数据,以减少直接访问数据源(例如数据库或远程服务)的次数,从而提高应用程序的性能。Caffeine 的 “高性能” 表示它在处理缓存操作时速度很快,并且占用的资源较少。“近乎最佳的命中率” 指的是 Caffeine 在缓存命中(即请求的数据已经存在于缓存中)方面表现非常出色。高命中率意味着大多数数据请求都可以直接从缓存中获得,而无需访问原始数据源,从而大大提高了效率和响应速度。

image-20240726183556025

Caffeine 是受 Google Guava 提供内存缓存 API 的启发。借着 Guava 的思想优化了算法发展而来。

Caffeine 缓存与并发映射(ConcurrentMap)类似,但不完全相同。最根本的区别在于,并发映射会保存所有添加到其中的元素,直到它们被显式地移除。而缓存通常配置为自动驱逐条目,以限制其内存占用。在某些情况下,即使缓存不驱逐条目,LoadingCache 或 AsyncLoadingCache 也可能非常有用,因为它们可以自动加载缓存。

Caffeine 提供灵活的构建方式来创建具有以下可选功能组合的缓存:

  • 自动加载缓存条目,可以选择异步方式
  • 基于大小的驱逐,当超过最大值时根据使用频率和最近使用时间进行驱逐
  • 基于时间的条目过期,依据最后访问时间或最后写入时间进行测量
  • 当首次请求陈旧条目时异步刷新
  • 键自动包装成弱引用
  • 值自动包装成弱引用或软引用
  • 驱逐(或其他方式移除)条目的通知
  • 将写操作传播到外部资源
  • 累积缓存访问统计信息

为了改进集成,扩展模块中提供了 JSR-107 JCache 和 Guava 适配器。JSR-107 标准化了基于 Java 6 的 API,以最小化特定供应商代码的代价来实现功能和性能。Guava 的 Cache 是 Caffeine 的前身库,这些适配器提供了一个简单的迁移策略。


2、Caffeine Cache 使用示例

2.1、依赖引入

Caffeine 可以通过 Maven 或 Gradle 依赖引入,对于 Java 11 或更高版本,使用 3.x 其他版本则使用 2.x

Maven 依赖:

<!-- https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/caffeine -->
<dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId><version>2.9.3</version>
</dependency>
2.2、示例代码

可以参照以下示例代码来使用 CaffeineCache:

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;import java.util.concurrent.TimeUnit;public class CaffeineCacheExample {public static void main(String[] args) {// 初始化缓存Cache<String, String> cache = Caffeine.newBuilder()// 设置最大缓存条目数.maximumSize(100)// 设置条目在最后一次访问后10分钟过期.expireAfterAccess(10, TimeUnit.MINUTES)// 设置条目在写入后10分钟过期.expireAfterWrite(10, TimeUnit.MINUTES)// 设置键使用弱引用.weakKeys()// 设置值使用软引用.softValues()// 构建缓存.build();// 添加一些条目到缓存中cache.put("key1", "value1");cache.put("key2", "value2");// 获取并打印缓存条目System.out.println("key1: " + cache.getIfPresent("key1"));System.out.println("key2: " + cache.getIfPresent("key2"));// 模拟访问 "key1"cache.getIfPresent("key1");// 等待一段时间后再获取缓存条目try {// 等待6秒Thread.sleep(6000); } catch (InterruptedException e) {Thread.currentThread().interrupt();}// 获取并打印缓存条目System.out.println("key1 (after 6 seconds): " + cache.getIfPresent("key1"));System.out.println("key2 (after 6 seconds): " + cache.getIfPresent("key2"));// 模拟再等待时间超过10分钟,检查过期try {// 等待10分钟Thread.sleep(600000); } catch (InterruptedException e) {Thread.currentThread().interrupt();}// 获取并打印缓存条目,预期缓存条目已过期System.out.println("key1 (after 10 minutes): " + cache.getIfPresent("key1"));System.out.println("key2 (after 10 minutes): " + cache.getIfPresent("key2"));}
}

3、Caffeine Cache 在 SpringBoot 中的使用

在 Spring Boot 应用中,可以通过配置文件和注解来简化对 Caffeine 缓存的集成和管理。

3.1、依赖引入

pom.xml 文件中添加 Caffeine 和 Spring Boot 缓存依赖:

<dependencies><!-- Spring Boot Starter Cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- Caffeine Cache --><dependency><groupId>com.github.ben-manes.caffeine</groupId><artifactId>caffeine</artifactId></dependency>
</dependencies>
3.2、配置文件

application.yml 中配置 Caffeine 缓存属性:

spring:cache:type: caffeinecache-names: myCachecaffeine:spec: maximumSize=100,expireAfterWrite=10m,expireAfterAccess=10m

这里 spec 属性用于设置缓存策略,包括最大缓存大小 (maximumSize)、写入后的过期时间 (expireAfterWrite),以及访问后的过期时间 (expireAfterAccess)。

3.3、启用缓存

在 Spring Boot 应用主类上添加 @EnableCaching 注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class ExampleApplication {public static void main(String[] args) {SpringApplication.run(ExampleApplication.class, args);}
}
3.4、配置缓存管理器

创建一个配置类来自定义缓存管理器:

import com.github.benmanes.caffeine.cache.Caffeine;
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 {@Beanpublic CaffeineCacheManager cacheManager() {CaffeineCacheManager cacheManager = new CaffeineCacheManager("myCache");cacheManager.setCaffeine(Caffeine.newBuilder().maximumSize(100).expireAfterWrite(10, TimeUnit.MINUTES).expireAfterAccess(10, TimeUnit.MINUTES));return cacheManager;}
}
3.5、使用缓存

在需要缓存的方法上添加 @Cacheable 注解:

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class MyService {@Cacheable("myCache")public String getData(String key) {// 模拟获取数据的过程return "Data for " + key;}
}
  • @Cacheable("myCache"):注解告诉 Spring 该方法的返回值应该被缓存。缓存的名字是 myCache
  • @RequestParam String key:注解从请求参数中获取 key 的值并传递给 getData 方法;
  • 缓存命中:如果缓存中存在对应 key 的值,则直接返回缓存值,否则执行方法体并将结果存入缓存。
http://www.lryc.cn/news/407256.html

相关文章:

  • Http 和 Https 的区别(图文详解)
  • DP学习——外观模式
  • Vue3 + Vite 打包引入图片错误
  • 搭建NFS、web、dns服务器
  • C++的UI框架和开源项目介绍
  • SpringBoot连接PostgreSQL+MybatisPlus入门案例
  • vue3里将table表格中的数据导出为excel
  • 【算法】分布式共识Paxos
  • 软考:软件设计师 — 5.计算机网络
  • C++ //练习 15.28 定义一个存放Quote对象的vector,将Bulk_quote对象传入其中。计算vector中所有元素总的net_price。
  • Midjourney绘画提示词精选
  • Kylin中的RBAC:为大数据安全加把锁
  • DDoS 攻击下的教育网站防护策略
  • Android13以太网静态IP不保存的问题
  • Redis 7.x 系列【31】LUA 脚本
  • mysql中You can’t specify target table for update in FROM clause错误
  • Linux Vim最全面的教程
  • setsockopt选项对tcp速度
  • HarmonyOS应用开发者高级认证,Next版本发布后最新题库 - 多选题序号3
  • bool数组的理解和应用[C++]
  • JavaScript模拟滑动手势
  • Text Control 控件教程:使用 .NET C# 中的二维码和条形码增强文档
  • 最新爆火的开源AI项目 | LivePortrait 本地安装教程
  • 揭秘Django与Neo4j:构建智能知识图谱的终极指南
  • 项目一缓存商品
  • SEO与数据中心代理IP的结合能带来哪些便利?
  • 《昇思25天学习打卡营第6天|ResNet50图像分类》
  • Activiti 6 兼容openGauss数据库bytes类型不匹配
  • 缓存技术:提升性能与效率的利器
  • LeetCode 637, 67, 399