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

springboot 整合内存缓存Caffeine的更多相关文章

  1. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  2. springBoot整合ecache缓存

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心 ...

  3. 转载-Springboot整合ehcache缓存

    转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...

  4. SpringBoot整合guava缓存

    1.pom文件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  5. springboot实现内存缓存

    题记:实现缓存大部分可以使用redis实现,简单.便捷,redis在针对应用部署多服务器是很好的,但如果针对单一服务器,内存缓存更好. 1.创建CacheLoader.java import java ...

  6. springboot整合redis缓存

    使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...

  7. springboot整合redis缓存一些知识点

    前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...

  8. springboot 整合ehcache缓存

    1.CacheManager Spring Boot默认集成CacheManager,如下包所示: 可以看出springboot自动配置了 JcacheCacheConfiguration. EhCa ...

  9. SpringBoot整合redis缓存(一)

    准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...

  10. Springboot整合Ehcache缓存

    Pom.xml导包 <!-- ehcache --> <dependency> <groupId>org.springframework.boot</grou ...

随机推荐

  1. centos7.8 安装 redis5.0.2

    1.安装gcc依赖 redis是由C语言开发,因此安装之前必须要确保服务器已经安装了gcc,可以通过如下命令查看机器是否安装: gcc -v 如果没有安装则通过以下命令安装: yum install ...

  2. ElasticSearch入门学习笔记

    ElasticSearch入门笔记 分页查询 from: 开始位置 size: 查多少条 GET /credit_enterprise_info/_search { "query" ...

  3. 更改DBGrid 颜色技巧

    1.根据条件更改某一单元格的颜色 [delphi] view plain copy procedure TMainFrm.First_DGDrawColumnCell(Sender: TObject; ...

  4. CV-部署芯片接续-CV全流程部署-TF版本

    CV-部署芯片接续-CV全流程部署-TF版本 1 单个CNN算子 import cv2 import numpy as np import tensorflow as tf import os fro ...

  5. iOS笔记 - Runtime 01:前期准备(isa结构 | Class结构 | 方法缓存)

    前言 1 - OC机制很多都是基于 Runtime实现的,比如指针的弱引用.OC的消息机制属于 Runtime的一部分 2 - OC是一门动态语言,在程序运行过程中就可以修改已经编译好的代码 3 -  ...

  6. Keil51单片机数码管鬼影显示问题

    Keil51单片机数码管鬼影显示问题 所为的鬼影就是程序逻辑正确,但电路逻辑有问题.就是按程序逻辑,前一个数字显示后,直接显示下一组数字,因为没清干净,导致瞬间残留: 处理的办法,就是有就显示,没有就 ...

  7. comment out one line in the file with sed

    sed -i "/test2/s/^/#/" test.log https://jaminzhang.github.io/linux/sed-command-usage-summa ...

  8. jmeter中监听器及测试结果分析

  9. git 代码提交到github 回滚到上一版本

    1.项目所在目录>git log(查看提交日志) commit idCodeAuthor: XXXXDate: Tue Mar 6 15:10:59 2018 +0800 commit idCo ...

  10. Uri转绝对路径工具类

    /** * 反射从 provider uri中获取 文件绝对路径 * @param context * @param uri * @return */ private static String ge ...