使用 Caffeine 缓存并在业务方法上通过注解实现每3到5秒更新缓存

要使用 Caffeine 缓存并在业务方法上通过注解实现每3到5秒更新缓存,可以借助 Spring 框架的缓存管理功能。Spring Cache 提供了 @Cacheable@CachePut 注解,可以方便地管理缓存。

以下是一个示例,展示了如何在 Spring Boot 项目中使用 Caffeine 缓存,并通过注解实现每3到5秒更新缓存。

1. 添加依赖

首先,在 pom.xml 中添加 Spring Boot 和 Caffeine 的依赖:

xml 复制代码
<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>

2. 配置缓存管理器

在 Spring Boot 配置类中,配置 Caffeine 缓存管理器,并设置缓存项的过期时间为3到5秒。

java 复制代码
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
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 CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(3, TimeUnit.SECONDS));
        return cacheManager;
    }
}

3. 使用注解进行缓存管理

在业务方法上使用 @Cacheable 注解来缓存方法的结果,并在需要更新缓存的地方使用 @CachePut 注解。

java 复制代码
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class DataService {

    @Cacheable(value = "dataCache", key = "#key")
    public String fetchData(String key) {
        // 模拟数据加载过程
        System.out.println("Fetching data for key: " + key);
        return "Data for " + key;
    }

    @CachePut(value = "dataCache", key = "#key")
    public String updateData(String key, String newData) {
        // 更新数据
        System.out.println("Updating data for key: " + key);
        return newData;
    }
}

4. 测试缓存

创建一个简单的控制器来测试缓存功能。

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DataController {

    @Autowired
    private DataService dataService;

    @GetMapping("/data")
    public String getData(@RequestParam String key) {
        return dataService.fetchData(key);
    }

    @GetMapping("/update")
    public String updateData(@RequestParam String key, @RequestParam String newData) {
        return dataService.updateData(key, newData);
    }
}

解释

  1. 配置缓存管理器

    • CacheConfig 类中配置了 Caffeine 缓存管理器,并设置了缓存项在写入后3秒过期。
  2. 使用注解进行缓存管理

    • @Cacheable 注解用于缓存方法的结果。当方法参数相同且缓存中存在结果时,不会执行方法体,直接返回缓存中的结果。
    • @CachePut 注解用于更新缓存中的数据。每次调用该方法时,都会更新缓存中的数据。
  3. 测试缓存

    • DataController 类提供了两个端点,一个用于获取数据,另一个用于更新数据。

通过这种方式,你可以轻松地在业务方法上使用注解来管理缓存,并实现每3到5秒更新缓存的效果。

相关推荐
带刺的坐椅4 分钟前
(对标 Spring IA 和 LangChain4j)Solon AI & MCP v3.7.0, v3.6.4, v3.5.8 发布(支持 LTS)
java·spring·ai·solon·mcp·langchain4j
7澄15 分钟前
深入解析 LeetCode 1572:矩阵对角线元素的和 —— 从问题本质到高效实现
java·算法·leetcode·矩阵·intellij-idea
诗9趁年华9 分钟前
缓存三大问题深度解析:穿透、击穿与雪崩
java·spring·缓存
阳光明媚sunny10 分钟前
分糖果算法题
java·算法
whltaoin10 分钟前
【JAVA全栈项目】弧图图-智能图床SpringBoot+MySQL API接口结合Redis+Caffeine多级缓存实践解析
java·redis·spring·缓存·caffeine·多级缓存
一 乐24 分钟前
医疗管理|医院医疗管理系统|基于springboot+vue医疗管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·医疗管理系统
华仔啊39 分钟前
SpringBoot 2.x 和 3.x 的核心区别,这些变化你必须知道
java·spring boot·后端
laocooon5238578861 小时前
大数的阶乘 C语言
java·数据结构·算法
不见长安在1 小时前
分布式ID
java·分布式·分布式id
熊小猿1 小时前
Redis 缓存怎么更新?—— 四种模型与一次“迟到的删除”
java·后端·spring