做电商网站企业,wordpress主题proland,佛山家具网站建设公司,云服务器怎么建立网站在现代应用程序中#xff0c;缓存是一种重要的性能优化技术#xff0c;它可以显著减少数据访问延迟#xff0c;降低服务器负载#xff0c;提高系统的响应速度。特别是在高并发的场景下#xff0c;合理地使用缓存能够有效提升系统的稳定性和效率。 Caffeine 是一个高性能的… 在现代应用程序中缓存是一种重要的性能优化技术它可以显著减少数据访问延迟降低服务器负载提高系统的响应速度。特别是在高并发的场景下合理地使用缓存能够有效提升系统的稳定性和效率。 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 在缓存命中即请求的数据已经存在于缓存中方面表现非常出色。高命中率意味着大多数数据请求都可以直接从缓存中获得而无需访问原始数据源从而大大提高了效率和响应速度。 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 --
dependencygroupIdcom.github.ben-manes.caffeine/groupIdartifactIdcaffeine/artifactIdversion2.9.3/version
/dependency2.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) {// 初始化缓存CacheString, 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));// 模拟访问 key1cache.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 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency!-- Caffeine Cache --dependencygroupIdcom.github.ben-manes.caffeine/groupIdartifactIdcaffeine/artifactId/dependency
/dependencies3.2、配置文件
在 application.yml 中配置 Caffeine 缓存属性
spring:cache:type: caffeinecache-names: myCachecaffeine:spec: maximumSize100,expireAfterWrite10m,expireAfterAccess10m这里 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 该方法的返回值应该被缓存。缓存的名字是 myCacheRequestParam String key注解从请求参数中获取 key 的值并传递给 getData 方法缓存命中如果缓存中存在对应 key 的值则直接返回缓存值否则执行方法体并将结果存入缓存。