自学内容网 自学内容网

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

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

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



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: caffeine
    cache-names: myCache
    caffeine:
      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 {

    @Bean
    public 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 的值,则直接返回缓存值,否则执行方法体并将结果存入缓存。

原文地址:https://blog.csdn.net/weixin_45187434/article/details/140722527

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!