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. spring mvc @Configuration addConverterFactory 无效问题

    spring 版本: 4.3.7 addFormatters(FormatterRegistry registry) 不生效 <!-- 此处与 @EnableWebmvc 冲突, 配置此处后 E ...

  2. linux kernel 常用函数记录

    offsetof是用来判断结构体中成员的偏移位置 container_of宏用来根据成员的地址来获取结构体的地址 bitwise 是用来确保不同位方式类型不会被弄混 (小端模式,大端模式,cpu 尾模 ...

  3. nginx日志按日期存储

    http {     include       mime.types;     default_type  application/octet-stream;     map $time_iso86 ...

  4. 3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?

    3DMAX安装失败怎么办?安装3DMAX失败提示错误怎么解决?有很多同学想把3DMAX卸载后重新安装,但是发现3DMAX安装到一半就失败了或者显示3DMAX已安装或者安装未完成,大多数情况下其实是3D ...

  5. WPF BackSpace 回退到上一个页面

    在Wpf程序中,有时候点击到某些控件后,再按下[BackSpace]键,画面会回到上一个 TextBox可能自己处理了,所以没有这一个现象. 解决方案是: 在App.xaml.cs 的 Initial ...

  6. tcpdump: error while loading shared libraries: libpcap.so.1: cannot open shared object file: No such file or directory

    [root@inner ~]# tcpdump -i any -s 0 -w trunkm.pcaptcpdump: error while loading shared libraries: lib ...

  7. lua-携程

    function SayHey(mag) for i = 1 , 3 doprint(mag)coroutine.yield()end end --创建携程(协同) coFunc= coroutine ...

  8. Freertos stack-overflow 检测

    (出现的地址信息没有特定意义,仅供参考) vApplicationStackOverflowHook() at rtos.c:371 0x55aa vTaskSwitchContext() at ta ...

  9. ConvTranspose的output_padding问题

    当stride>=2时,反向传播,由dy, w得到dx的时候,dx的形状不唯一. 例如input_shape (7,7)或者(8,8)在kernel(3,3)上,以stride=2进行卷积, 最 ...

  10. PaddlePaddleOCR安装教程

    1.首先下载并安装python3.7.8 链接地址: 注:安装时Add path勾选 2.进入命令提示符界面 3.升级pip版本 python -m pip install --upgrade pip ...