You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中不使用Spring缓存实现Guava/Caffeine无过期永久缓存方案问询

Spring Boot 集成Caffeine/Guava Cache实现永久驻留缓存方案

优先推荐使用Caffeine,它是Guava Cache的性能优化升级版,吞吐量、内存效率都更优,两者API基本兼容,切换成本极低。


1. 引入依赖

Caffeine依赖(推荐)

<dependency>
    <groupId>com.github.benmanes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
    <version>3.1.8</version> <!-- 可替换为最新稳定版 -->
</dependency>

Guava依赖(备选)

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>32.1.3-jre</version> <!-- 可替换为最新稳定版 -->
</dependency>

2. 构建无自动回收的缓存实例

不配置任何过期时间、最大容量限制,即可实现缓存数据永久驻留,直到应用重启。

Caffeine配置示例

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfig {
    // 泛型可根据实际业务调整,示例为key=主键ID(Long),value=业务数据对象
    @Bean
    public Cache<Long, YourBusinessData> permanentCache() {
        return Caffeine.newBuilder()
                // 不配置任何过期、容量限制参数,缓存不会自动回收
                .build();
    }
}

Guava配置示例

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfig {
    @Bean
    public Cache<Long, YourBusinessData> permanentCache() {
        return CacheBuilder.newBuilder()
                // 不配置任何回收参数
                .build();
    }
}

3. 应用启动时自动加载DB数据到缓存

通过ApplicationRunner在Spring Boot初始化完成后自动执行DB查询,写入缓存:

import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;

@Component
public class CacheInitRunner implements ApplicationRunner {
    @Resource
    private Cache<Long, YourBusinessData> permanentCache;
    @Resource
    private YourBusinessRepository yourBusinessRepository; // 替换为你的DAO层接口

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 从DB查询全量需要缓存的业务数据
        List<YourBusinessData> allData = yourBusinessRepository.findAll();
        // 批量写入缓存
        allData.forEach(data -> permanentCache.put(data.getId(), data));
    }
}

4. 手动增改缓存操作

封装缓存服务类,对外提供操作入口,业务逻辑需要更新缓存时直接调用对应方法即可:

import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Map;
import java.util.List;

@Service
public class PermanentCacheService {
    @Resource
    private Cache<Long, YourBusinessData> permanentCache;

    // 新增/更新单个缓存项
    public void saveOrUpdate(Long key, YourBusinessData value) {
        permanentCache.put(key, value);
    }

    // 批量新增/更新缓存项
    public void batchSaveOrUpdate(Map<Long, YourBusinessData> dataMap) {
        permanentCache.putAll(dataMap);
    }

    // 查询缓存
    public YourBusinessData get(Long key) {
        return permanentCache.getIfPresent(key);
    }

    // 全量刷新缓存(需要全量同步DB时调用)
    public void refreshAll(List<YourBusinessData> allData) {
        permanentCache.invalidateAll();
        allData.forEach(data -> permanentCache.put(data.getId(), data));
    }
}

注意:如果缓存数据量极大,需要提前评估JVM堆内存容量,避免出现OOM问题。

内容的提问来源于stack exchange,提问作者ppb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 10:36:05