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. Adobe Acrobat PDF Reader DC软件下载

    安装包下载 https://get.adobe.com/en/reader/enterprise/ ftp下载,按日期排序 ftp://ftp.adobe.com/pub/adobe/reader/w ...

  2. SpringBoot整合MyBatis-Plus详细使用方法

    SpringBoot整合mp 一.添加依赖pom.xml <dependency> <groupId>mysql</groupId> <artifactId& ...

  3. 5-MIGO收货-请维护容差码VP(公司代码1000)的容差限制-消息号 M8215

    请维护容差码VP(公司代码1000)的容差限制 消息号 M8215 诊断 系统试图检查输入输入是否在特定的限度范围之内.由于尚未维护显示的容差码限制,所以无法进行此操作. 步骤 系统管理员必须在公司代 ...

  4. Rust智能指针

    Rust智能指针 https://course.rs/advance/smart-pointer/intro.html Box 堆对象分配 Box指针拥有内存对象的独占使用权 (一)使用场景 1. 使 ...

  5. c++游戏编程(1)开发环境与工具函数

    c++游戏编程(1)开发环境与工具函数 文章目录 c++游戏编程(1)开发环境与工具函数 前言 1.开发工具 2.工具函数 2.1 windows.h 2.1.1 _mkdir() 2.1.2 _rm ...

  6. Windows11 微软提供三种路径 安装程序应用

    Windows11 微软提供三种路径 安装程序应用

  7. TCC事务解决方案

    适用场景: 适用于具有强隔离性,严格一致性要求,也适用执行时间比较短的业务. 方案优缺点 优点: 1)在应用层实现具体逻辑,锁定资源的粒度小,不会锁定所有资源,性能比较高 2)Confirm阶段和Ca ...

  8. 解决uniapp 使用自带 switch 双向绑定视图不更新的问题

    使用  this.$set( a, b, c) a:需要更新视图属性对象 b:具体的属性值(就是你要更新视图的属性值) c:传递的参数 this.$set(this.gwjSelet,this.gwj ...

  9. Chtholly Tree 学习笔记

    前言 珂朵莉树 (Chtholly Tree) 是一种简单优美的数据结构,就像 Chtholly 一样可爱.暴力即优美. 适用于一些有区间赋值操作的序列操作题. Chtholly Tree 的本质是把 ...

  10. php基础教程(三)

    PHP语法概述 A. 基本语法 即使初次接触PHP的用户也会发现自己对PHP的语法风格并不陌生. 例如:<?php echo "Hello!": ?> 显示结果为&qu ...