我是靠谱客的博主 正直书本,这篇文章主要介绍Spring Boot 使用Caffeine缓存,现在分享给大家,希望可以做个参考。

Spring Boot 使用Caffeine缓存

    • Caffeine官方的介绍
    • demo
      • Caffeine配置参数

Caffeine是Java8重写Guava缓存,取代Guava缓存。
Spring Cache相关注解基础请查看这篇文章

Caffeine官方的介绍

caffeine官网
Caffeine是基于Java8的高性能,接近完美的缓存库。

demo

pom.xml
引入spring-context-support

复制代码
1
2
3
4
5
6
7
8
9
10
<dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>

或者spring-boot-starter-cache

复制代码
1
2
3
4
5
6
7
8
9
10
<dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>

Caffeine配置参数

属性说明
initalCapacity初始空间大小
maximumSize缓存最大条数
maximumWeight缓存的最大权重
expireAfterAccess最后一次写入或访问后经过固定时间过期
expireAfterWrite最后一次写入后经过固定时间过期
refreshAfterWrite创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存
weakKeys打开key的弱引用
weakValues打开value的弱引用
softValues打开value的软引用
recordStats开发统计功能
  • 注意
    refreshAfterWrite 要实例 LoadingCache 否则报错
    java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache

application.yml

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
spring: cache: cache-names: - cache1 - cache2 type: caffeine caffeine: spec: maximumSize=500,expireAfterAccess=600s

yml文件中的配置也可以通过bean来配置

CacheConfig.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.jsong.wiki.blog.config; import com.github.benmanes.caffeine.cache.CacheLoader; import com.github.benmanes.caffeine.cache.Caffeine; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.springframework.cache.CacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.concurrent.TimeUnit; @Configuration public class CacheConfig { @Bean("caffeineCacheManager") public CacheManager caffeineCacheManager() { CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCaffeine(Caffeine.newBuilder() .maximumSize(10_1000) .expireAfterAccess(5, TimeUnit.SECONDS)); return cacheManager; } /* 如果配置refreshAfterWrite要有这个bean,否则报错 java.lang.IllegalStateException: refreshAfterWrite requires a LoadingCache*/ @Bean public CacheLoader<Object, Object> cacheLoader() { CacheLoader<Object, Object> cacheLoader = new CacheLoader<Object, Object>() { @Nullable @Override public Object load(@NonNull Object key) throws Exception { return null; } }; return cacheLoader; } }

CacheService.java

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.jsong.wiki.blog.service; import org.springframework.cache.annotation.*; import org.springframework.stereotype.Component; @EnableCaching @CacheConfig(cacheNames = "caffeineCacheManager") @Component public class CacheService { @Cacheable(value = "cache1", key = "#root.target") public String getName() { System.out.println("cache1"); return "cache1"; } }

最后

以上就是正直书本最近收集整理的关于Spring Boot 使用Caffeine缓存的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部