Caffeine缓存和@Cacheable注解

本文介绍了Caffeine作为高性能的Java缓存库,其基础使用包括自动加载策略(同步、异步)、其他API(如统计信息、CacheWriter、移除监听)和缓存淘汰机制(LRU、LFU)。此外,还简述了Spring中的@Cacheable注解在缓存管理中的应用。
摘要由CSDN通过智能技术生成

1. Caffeine的基础使用

借鉴了https://www.jianshu.com/p/9ee291147617

1.1 简介

Caffeine是基于Java 8的高性能,接近最佳的缓存工具库。Caffeine使用Google Guava启发的API提供内存缓存。所以它的使用成本较低,跟Guava的API大致一致。

它主要有以下几个功能:

  • 自动将条目自动加载到缓存中,可以选择同步或异步加载

  • 基于频率和新近度超过最大值时基于大小的逐出

  • 自上次访问或上次写入以来测得的基于时间的条目到期

  • 发生第一个陈旧的条目请求时,异步刷新

  • 键自动包装在弱引用中

  • 值自动包装在弱引用或软引用中

  • 逐出(或以其他方式删除)条目的通知

  • 写入通知

  • 缓存访问统计信息的

1.2 常见API

Cache分为LoadingCache(同步缓存),AsyncLoadingCache(异步缓存)

  • pom依赖
      <dependency>
            <groupId>com.github.benmanes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.8.5</version>
        </dependency>
1.2.1 人工加载策略
Cache<Object, Object> cache = Caffeine.newBuilder()
        .expireAfterWrite(1, TimeUnit.SECONDS)
            .expireAfterAccess(1, TimeUnit.SECONDS)
        .maximumSize(10)//最大条数
        .build();//定义cache
User user1=(User) cache.get(id, v-> userDao.getOne(id
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Caffeine 和 `@Cacheable` 注解实现定时刷新缓存,可以按照以下步骤进行操作: 1. 添加 Caffeine 依赖:在项目的 `pom.xml` 文件中添加 Caffeine 依赖。 ```xml <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.9.0</version> </dependency> ``` 2. 创建缓存配置:在 Spring Boot 的配置类中创建一个缓存配置类,配置 Caffeine 缓存。 ```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("myCache"); cacheManager.setCaffeine(caffeineCacheBuilder()); return cacheManager; } Caffeine<Object, Object> caffeineCacheBuilder() { return Caffeine.newBuilder() .expireAfterWrite(1, TimeUnit.HOURS) // 缓存项写入后过期时间 .refreshAfterWrite(30, TimeUnit.MINUTES) // 缓存项刷新时间 .maximumSize(100) // 缓存最大大小 .weakKeys() // 弱引用键 .recordStats(); // 记录统计信息 } } ``` 3. 使用 `@Cacheable` 注解:在需要进行缓存的方法上使用 `@Cacheable` 注解,并指定缓存的名称。 ```java import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class MyService { @Cacheable(cacheNames = "myCache") public String getData(String key) { // 从数据库或其他数据源获取数据 return "data"; } } ``` 4. 定时刷新缓存:使用 Spring 的定时任务功能,定时调用需要刷新缓存的方法。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class CacheScheduler { private final CacheManager cacheManager; private final MyService myService; @Autowired public CacheScheduler(CacheManager cacheManager, MyService myService) { this.cacheManager = cacheManager; this.myService = myService; } @Scheduled(fixedRate = 30 * 60 * 1000) // 每30分钟执行一次 public void refreshCache() { cacheManager.getCache("myCache").clear(); myService.getData("key"); // 触发缓存的重新加载 } } ``` 以上步骤将使用 Caffeine 缓存和 `@Cacheable` 注解实现缓存的定时刷新。在每次定时任务执行时,会清空缓存并重新加载数据。请根据自己的需求进行适当调整和配置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值