Springboot 集成 Caffeine

本文介绍了在Spring Boot中集成Caffeine缓存的方法。Caffeine是高性能Java内存缓存库,在Spring Boot集成简单。文中说明了Caffeine配置参数及注意事项,还介绍了通过注解和手动配置实现不同缓存策略的过程,最后验证了手动配置缓存策略的成功。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot 集成 Caffeine

Caffeine 是个高性能的开源 Java 内存缓存库,具有较高的命中率和出色的并发能力。在 Spring Boot 中集成也非常简单,提供了各种开箱既用的工具。

Caffeine 并不是分布式缓存.

Springboot 中引用的依赖

		<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>
    </dependencies>

其中有两个依赖需要注意

				<!-- springboot 缓存-->
				<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <!-- caffeine 依赖-->
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>

配置文件

spring.cache.cache-names=IZUUL
spring.cache.caffeine.spec=initialCapacity=50,maximumSize=500,expireAfterWrite=5s
spring.cache.type=caffeine

Caffeine配置说明:

  • initialCapacity=[integer]: 初始的缓存空间大小
  • maximumSize=[long]: 缓存的最大条数
  • maximumWeight=[long]: 缓存的最大权重
  • expireAfterAccess=[duration]: 最后一次写入或访问后经过固定时间过期
  • expireAfterWrite=[duration]: 最后一次写入后经过固定时间过期
  • refreshAfterWrite=[duration]: 创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存
  • weakKeys: 打开key的弱引用
  • weakValues:打开value的弱引用
  • softValues:打开value的软引用
  • recordStats:开发统计功能

注意:

  • expireAfterWrite和expireAfterAccess同事存在时,以expireAfterWrite为准。
  • maximumSize和maximumWeight不可以同时使用
  • weakValues和softValues不可以同时使用

启动类中加入 @EnableCaching 注解 开启缓存

package com.izuul.caffeinedemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
//启用缓存
@EnableCaching
public class CaffeineDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(CaffeineDemoApplication.class, args);
    }

}

创建 service, 其中有三个方法:

查询方法 cacheIZUUL(), 获取需要缓存的数据, 调用 getCache() 方法

修改方法 cachePutIZUUL(), 更新缓存数据

getCache() 方法模拟生产项目中的逻辑代码, 线程睡眠 3s 模拟项目运行耗时

关于@CacheAble @CachePut 等注解上以前的 <<Springboot 集成 Redis>>中有介绍

package com.izuul.caffeinedemo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CaffeineService {

    @Cacheable(value = "IZUUL", key = "#key")
    public String cacheIZUUL(String key) {
        log.info("cacheIZUUL()方法执行");
        return getCache(key);
    }

    @CachePut(value = "IZUUL", key = "#key")
    public String cachePutIZUUL(String key) {
        log.info("cachePutIZUUL()方法执行");
        return "cachePutIZUUL--" + key;
    }

    private String getCache(String key) {
        try {
            log.info("getCache()方法执行");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return key;
    }
}

创建 controller 调用 service

package com.izuul.caffeinedemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CaffeineController {

    @Autowired
    private CaffeineService caffeineService;

    @GetMapping("/cache-izuul/{key}")
    public String cacheIZUUL(@PathVariable String key) {

        return caffeineService.cacheIZUUL(key);
    }

    @GetMapping("/cache-put-izuul/{key}")
    public String cachePutIZUUL(@PathVariable String key) {

        return caffeineService.cachePutIZUUL(key);
    }

}

启动项目 访问 http://localhost:8080/cache-izuul/izuul

延迟 3 秒后展示数据 izuul, 5s 后缓存过期

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NDvCDyd6-1609982441988)(https://ws1.sinaimg.cn/large/006tNc79ly1g2txnkqlixj30bs03l3ym.jpg)]

访问 http://localhost:8080/cache-put-izuul/izuul

展示数据 cachePutIZUUL—izuul, 同样是 5s 后缓存过期

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jVj2sou2-1609982441993)(https://ws3.sinaimg.cn/large/006tNc79ly1g2txva8oalj30d004qdg1.jpg)]

以上配置过程比较死板, 没法实现多种缓存策略, 所以可以在项目中手动进行配置

删除 properties 文件中的配置

创建配置类 CaffeineConfig

package com.izuul.caffeinedemo;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class CaffeineConfig {

    @Bean
    public CacheManager caffeineCacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<CaffeineCache> caffeineCaches = new ArrayList<>();

        for (CacheType cacheType : CacheType.values()) {
            caffeineCaches.add(new CaffeineCache(cacheType.name(),
                    Caffeine.newBuilder()
                            .expireAfterWrite(cacheType.getExpires(), TimeUnit.SECONDS)
                            .build()));
        }

        cacheManager.setCaches(caffeineCaches);

        return cacheManager;
    }
}

创建枚举类, 加入过期时间expires, 分别设置IZUUl消亡时间是 10s, MUMU 消亡时间 5s

package com.izuul.caffeinedemo;

public enum CacheType {

    IZUUL(10),

    MUMU(5);

    private int expires;

    CacheType(int expires) {
        this.expires = expires;
    }

    public int getExpires() {
        return expires;
    }
}

以上配置就完成了 在 service 中新增两个方法来进行验证

package com.izuul.caffeinedemo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class CaffeineService {

    @Cacheable(value = "IZUUL", key = "#key")
    public String cacheIZUUL(String key) {
        log.info("cacheIZUUL()方法执行");
        return getCache(key);
    }

    @CachePut(value = "IZUUL", key = "#key")
    public String cachePutIZUUL(String key) {
        log.info("cachePutIZUUL()方法执行");
        return "cachePutIZUUL--" + key;
    }

    @Cacheable(value = "MUMU", key = "#key")
    public String cacheMUMU(String key) {
        log.info("cacheMUMU()方法执行");
        return getCache(key);
    }

    @CachePut(value = "MUMU", key = "#key")
    public String cachePutMUMU(String key) {
        log.info("cachePutMUMU()方法执行");
        return "cachePutMUMU--" + key;
    }

    private String getCache(String key) {
        try {
            log.info("getCache()方法执行");
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return key;
    }
}

controller 中调用新增的方法

package com.izuul.caffeinedemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CaffeineController {

    @Autowired
    private CaffeineService caffeineService;

    @GetMapping("/cache-izuul/{key}")
    public String cacheIZUUL(@PathVariable String key) {

        return caffeineService.cacheIZUUL(key);
    }

    @GetMapping("/cache-put-izuul/{key}")
    public String cachePutIZUUL(@PathVariable String key) {

        return caffeineService.cachePutIZUUL(key);
    }

    @GetMapping("/cache-mumu/{key}")
    public String cacheMUMU(@PathVariable String key) {

        return caffeineService.cacheMUMU(key);
    }

    @GetMapping("/cache-put-mumu/{key}")
    public String cachePutMUMU(@PathVariable String key) {

        return caffeineService.cachePutMUMU(key);
    }
}

添加完成后 启动项目, 分别访问 http://localhost:8080/cache-izuul/izuulhttp://localhost:8080/cache-mumu/mumu

你会发现 cahce-izuul 经过 10s 后会再一次执行 getCache() 方法, 说明它的缓存有 10s

而 cahce-mumu 是 5s

以上证明我们手动配置的 Caffeine 缓存策略成功了

在 java 项目中可以使用此方法进行本地缓存

### 如何在 Spring Boot 中集成 Caffeine 缓存 #### 添加 Maven 依赖项 为了在 Spring Boot 应用程序中使用 Caffeine 实现缓存功能,需引入相应的 Maven 依赖项: ```xml <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.9.0</version> </dependency> ``` 此操作确保应用程序能够访问到 Caffeine 的 API 和类库[^4]。 #### 启用缓存支持并配置 Caffeine CacheManager 通过创建 `@Configuration` 类来启用基于注解的缓存管理和自定义 Caffeine 的设置。这通常涉及到重写默认的 `CacheManager` bean 或者提供特定于应用需求的配置选项。 ```java import com.github.benmanes.caffeine.cache.Caffeine; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { CaffeineSpec spec = "maximumSize=100,expireAfterWrite=5m"; return new CaffeineCacheManager(spec); } } ``` 上述代码片段展示了如何利用 Java Config 方式来进行基本配置,并设置了最大条目数以及过期时间策略[^3]。 #### 使用缓存注解简化业务逻辑中的缓存处理 Spring 提供了一系列用于控制方法级别缓存行为的注解,比如 `@Cacheable`, `@CachePut`, 和 `@CacheEvict`. 这些注解可以被方便地应用于服务层的方法之上,从而减少重复计算开销的同时提高性能效率。 ```java @Service public class UserService { private final UserRepository userRepository; @Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } /** * 当调用该方法时会尝试从名为"user"的缓存里获取数据; * 如果不存在,则执行查询数据库的操作并将结果放入缓存。 */ @Cacheable(value="users", key="#id") public User getUserById(Long id){ return userRepository.findById(id).orElse(null); } // Other service methods... } ``` 这段例子说明了怎样借助 `@Cacheable` 注解轻松实现读取型接口的数据缓存机制[^1]。 #### 测试验证 完成以上步骤之后就可以编写单元测试或者启动项目查看效果了。对于简单的 CRUD 操作来说,可以通过模拟请求或直接调用相关 Service 方法的方式检验是否达到了预期的效果——即首次加载真实资源后再次请求相同参数应返回已存储的结果而不是重新发起 I/O 访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值