Spring Boot 使用Caffeine缓存

本小妞迷上赌 提交于 2019-12-24 19:08:13

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

Spring Boot 使用Caffeine缓存

Caffeine官方的介绍
demo
Caffeine配置参数
Caffeine是Java8重写Guava缓存,取代Guava缓存。
Spring Cache相关注解基础请查看这篇文章

Caffeine官方的介绍

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

demo

pom.xml
引入spring-context-support

        <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>
1
2
3
4
5
6
7
8
9
或者spring-boot-starter-cache

        <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>
1
2
3
4
5
6
7
8
9
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

spring:
  cache:
    cache-names:
      - cache1
      - cache2
    type: caffeine
    caffeine:
      spec:
        maximumSize=500,expireAfterAccess=600s


1
2
3
4
5
6
7
8
9
10
11
yml文件中的配置也可以通过bean来配置

CacheConfig.java

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;
    }

}


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
CacheService.java

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";
    }
————————————————
版权声明:本文为CSDN博主「宋大王」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/JsongNeu/article/details/103671650

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!