Spring Boot整合本地缓存:Guava与Caffeine实战对比

开发 前端
本地缓存是提升应用性能的重要手段之一。Spring Boot 整合 Guava 和 Caffeine 两种主流本地缓存方案,究竟该如何选择?本文将给出完整实现示例,并通过性能对比和场景分析帮你做出决策。

本地缓存是提升应用性能的重要手段之一。Spring Boot 整合 Guava 和 Caffeine 两种主流本地缓存方案,究竟该如何选择?本文将给出完整实现示例,并通过性能对比和场景分析帮你做出决策。

1.为什么需要本地缓存?

在高并发场景下,频繁访问数据库或外部接口会导致性能瓶颈。本地缓存将热点数据存储在应用内存中,具备以下优势

  • 微秒级响应速度(比Redis快10倍以上)
  • 减轻外部存储压力
  • 网络开销为零
  • 应对突发流量

2.Guava Cache整合实战

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

配置缓存

@Configuration
@EnableCaching
public class GuavaCacheConfig {
    @Bean
    public CacheManager cacheManager() {
        GuavaCacheManager cacheManager = new GuavaCacheManager();
        cacheManager.setCacheBuilder(CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(10, TimeUnit.MINUTES)
                .recordStats());
        return cacheManager;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

使用示例

@Service
public class ProductService {
    @Cacheable(value = "products", key = "#id")
    public Product getProductById(Long id) {
        // 模拟数据库查询
        return findFromDB(id);
    }
    @CacheEvict(value = "products", key = "#id")
    public void refreshProduct(Long id) {
        // 触发缓存清除
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

特性说明

  • 基于LRU淘汰策略
  • 支持权重控制
  • 提供缓存统计功能
  • 最大容量限制

2.Guava Cache整合实战

添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

高级配置

@Configuration
@EnableCaching
public class CaffeineCacheConfig {
    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder()
                .initialCapacity(200)
                .maximumSize(2000)
                .expireAfterAccess(5, TimeUnit.MINUTES)
                .weakKeys()
                .recordStats());
        return cacheManager;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

异步操作示例

@Cacheable(value = "userProfiles", key = "#userId", sync = true)
public CompletableFuture<UserProfile> getUserProfileAsync(Long userId) {
    return CompletableFuture.supplyAsync(() -> queryUserProfile(userId));
}
  • 1.
  • 2.
  • 3.
  • 4.

特性说明

  • 更高的并发性能(相比Guava提升30%+)
  • 异步加载机制
  • 支持Entry自动刷新
  • 更精细的内存控制

3.关键指标对比

特性

Guava Cache

Caffeine

并发性能

中等(基于分段锁)

高(WRite优化)

命中率

85%-92%

93%-98%

内存占用

较高

优化压缩

淘汰策略

LRU

Window-TinyLFU

统计功能

基础指标

详细监控

维护状态

停止更新

持续迭代

GC友好度

一般

弱引用支持

4.选型建议

选择Guava

  • 项目已使用Guava工具库
  • 需要保持依赖最小化
  • 缓存需求较简单
  • 系统并发量 < 5000 QPS

选择Caffeine

  • 需要处理高并发(>1万QPS)
  • 追求更高缓存命中率
  • 需要异步加载能力
  • 系统内存资源紧张
  • 需要更细粒度的控制

5.最佳实践建议

容量规划

根据业务量设置合理的初始容量(initialCapacity)

过期策略组合

同时设置访问过期和写入过期

监控接入

// 获取统计信息
CacheStats stats = cacheManager.getCache("cacheName").getNativeCache().stats();
logger.info("命中率:{}", stats.hitRate());
  • 1.
  • 2.
  • 3.

防御性编程

CacheBuilder.newBuilder()
    .maximumWeight(100000)
    .weigher((key, value) -> calculateObjectSize(value))
    .build();
  • 1.
  • 2.
  • 3.
  • 4.

6.性能测试数据

模拟10万次读取操作(单位:ms)

线程数

Guava

Caffeine

50

1200

850

200

3200

1800

500

6500

3100

7.小结

通过合理使用本地缓存,可使应用性能获得数量级提升。对于新项目推荐直接采用Caffeine,而历史项目迁移到Caffeine通常也能获得显著收益。建议根据实际压测结果进行最终决策,并持续监控缓存效果。

  • 在Spring Boot 2.4+版本中,Caffeine已成为默认缓存实现,可通过spring.cache.type=caffeine快速启用。
责任编辑:武晓燕 来源: JAVA充电
相关推荐

2017-04-17 10:35:40

Spring BooRedis 操作

2025-02-17 09:32:18

2025-02-21 12:00:00

SpringBoot防重复提交缓存机制

2024-12-06 10:02:46

2024-11-11 10:02:37

Spring搜索数据

2020-08-19 08:55:47

Redis缓存数据库

2022-03-31 13:58:37

分布式SpringRedis

2024-07-25 14:04:16

2021-11-04 08:04:49

缓存CaffeineSpringBoot

2022-12-23 08:28:42

策略模式算法

2020-06-29 07:43:12

缓存RedisSpringBoot

2015-12-28 10:48:44

RedisSpring缓存实例

2024-12-13 08:12:02

2017-10-17 15:14:33

Spring BooThymeleafWeb

2022-07-21 11:04:53

Swagger3Spring

2023-09-11 07:46:03

Cache2k缓存

2023-12-14 13:28:00

Spring流程Web

2021-12-27 09:59:57

SpringCanal 中间件

2009-07-21 16:49:41

整合iBatis和SpSqlMapClien

2009-07-14 14:41:33

Webwork与Spr
点赞
收藏

51CTO技术栈公众号