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. pj_0002_wbs_manager

    #!/usr/bin/python # -*- coding: UTF-8 -*- import env_config from class_task import Task  from lib.li ...

  2. pyinstaller根据虚拟环境virtualenv进行打包,降低exe文件大小

    问题:使用pyinstaller打包后,发现打的exe特别大,有近200M,又没有用几个库,代码也很少,怎么会打出这么大的包呢? 分析:在pyinstaller打包的过程中,可以看到窗口中出现了很多本 ...

  3. [canvas]ncaught TypeError: Cannot read properties of null (reading 'getContext')

    相信你和我一样是直接复制大佬的js代码(笑) ------------ 主要问题在于:js先加载完了,html才加载,导致js获取不了html的对象 解决办法: 把  <head />  ...

  4. android root app 无法umount

    app已经有root权限了. 在执行umount /sbin时候总是不成功,但是在adb 的shell 里是可以的. 研究半天后发现,原来app的mount 空间被修改了. 用以下方法解决问. ech ...

  5. UI基础 - UIAppearance协议

    前言 1 - 在一些 app 中会涉及到更改外观设置的功能,最普遍的就是夜间模式和白天模式的切换,而对于外观的更改必定是一个全局的东西.这在 iOS5 以前想要实现这样的效果是比较困难的,但是 iOS ...

  6. 推测执行 Speculative execution

    如果我们只是靠随便网络上查找一个Speculative这个词的含义,是很难去理解的.但是我们通过查看英文原文去理解,可能就比较清楚地理解了: speculative (adjective) 1. ba ...

  7. 时间处理及interval函数运用

    MySql时间操作1.interval的说明1.1.当函数使用时,即interval(),为比较函数,如:interval(10,1,3,5,7); 结果4:原理:10为被比较数,后面1,3,5,7为 ...

  8. taro+vue3+TS+Pinia+tailwindcss+NutUI项目创建与开发

    一.初始化 1.Taro 基本安装并初始化项目 npx @tarojs/cli init WechatAppDemo -选择框架:vue3 -是否使用ts?y -请选择css预编译器:无 -请选择we ...

  9. laravel 邮件发送

    1.首先你要在qq悠闲中开启你的   SMPT(设置->账户)   获取到你的授权码 2.配置laravel MAIL_DRIVER=smtpMAIL_HOST=smtp.qq.comMAIL_ ...

  10. 移动端及pc端适配

    1.rem搭配CSS预处理器使用 这里我就用vue+less来简单操作一下,具体可以封装到底层,这里暂且演示一下原理. 这里推荐一下使用我的自制脚手架 (songyao-cli) 来快速生成一个vue ...