springboot 整合内存缓存Caffeine

1.引jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.8.6</version>
        </dependency>

2.写配置

app.cache.enabled:true
aa.cache.caffeine.pesc:expireAfterWrite=1m, recordStats
Caffeine配置说明:

initialCapacity=[integer]:初始的缓存空间大小

maximumSize=[long]:缓存的最大条数

maximumWeight=[long]:缓存的最大权重

expireAfterAccess=[duration]:最后一次写入或访问后经过固定时间过期

expireAfterWrite=[duration]:最后一次写入后经过固定时间过期

refreshAfterWrite=[duration]:创建缓存或者最近一次更新缓存后经过固定的时间间隔,刷新缓存

recordStats:开发统计功能

注意:

expireAfterWrite和expireAfterAccess同时存在时,以expireAfterWrite为准。

maximumSize和maximumWeight不可以同时使用

3.书写配置类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import com.haier.cz.lowcode.table.cache.LocalCaffeineCacheManager;

@Configuration("aa")
@ConditionalOnClass({ Caffeine.class, CaffeineCacheManager.class })
//@ConditionalOnMissingBean(CacheManager.class)
public class LocalCacheConfigCaffeine {
	/**
	 * 模型缓存,默认1分钟
	 */
	@Value("${table.cache.caffeine.form.model:expireAfterWrite=1m, recordStats}")
    private String formModelCacheManagerSpec;
  /**  
    @Bean("studentManager")
    public CacheManager caffeineCacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager(true);
        cacheManager.setCaffeine(Caffeine.newBuilder()
                // 设置最后一次写入或访问后经过固定时间过期
                .expireAfterWrite(10, TimeUnit.SECONDS)
                // 初始的缓存空间大小
                .initialCapacity(100)
                // 缓存的最大条数
                .maximumSize(1000));
        return cacheManager;
    }
*/

    @Bean("stu")
    @Primary
    public CacheManager cacheManager(){
        CaffeineSpec parse = CaffeineSpec.parse(pesc);
        Caffeine<Object,Object> caffeine = Caffeine.from(parse);
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(caffeine);
        return cacheManager;
    }
}

4.简单使用

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangfan.po.Student;
import com.wangfan.service.StudentService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wjj
 */
@Service
@Cacheable(cacheManager = "aa")
public class StudentServiceImpl extends ServiceImpl<BaseMapper<Student>,Student> implements StudentService {

    @Override
    @Cacheable(cacheNames = "student")
    public List<Student> findAll() {
        return this.list();
    }