Spring Cache + Caffeine实现本地缓存
Caffeine简介
Caffeine是一个高性能,高命中率,低内存占用,near optimal 的本地缓存,简单来说它是 Guava Cache 的优化加强版
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
开启缓存
@EnableCaching注解开启使用缓存管理功能
@SpringBootApplication
@EnableCaching
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注入
方式一
新建一个枚举类
public enum Caches {
CACHE_ACCESS_TOKEN(10, 7200);
/** 最大数量 */
private Integer maxSize;
/** 过期时间 秒 */
private Integer ttl;
Caches() {
}
Caches(Integer maxSize, Integer ttl) {
this.maxSize = maxSize;
this.ttl = ttl;
}
public Integer getMaxSize() {
return maxSize;
}
public Integer getTtl() {
return ttl;
}
}
注入到IOC容器
/**
* 本地缓存
* @return
*/
@Bean
@Primary
public CacheManager cacheManager() {
SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
ArrayList<CaffeineCache> caffeineCaches = new ArrayList<>();
for (Caches c : Caches.values()) {
caffeineCaches.add(new CaffeineCache(c.name(),
Caffeine.newBuilder()
.recordStats()
.expireAfterWrite(c.getTtl(), TimeUnit.SECONDS)
.maximumSize(c.getMaxSize())
.build()
)
);
}
simpleCacheManager.setCaches(caffeineCaches);
return simpleCacheManager;
}
方式二
@Bean
@Primary
public CacheManager cacheManager() {
CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().expireAfterWrite(60, TimeUnit.MINUTES);
caffeineCacheManager.setCaffeine(caffeine);
return caffeineCacheManager;
}
使用
可以使用spring提供的@Cacheable、@CachePut、@CacheEvict等注解来方便的使用caffeine缓存
@Cacheable(cacheNames = "CACHE_ACCESS_TOKEN", key = "#root.methodName")
public String getAccessToken(String corpid, String corpsecret) {
//todo something...
return "";
}
问题
使用@Cacheable缓存不起作用
失效场景
在私有方法上加缓存
类内部方法调用加缓存
失效原因
Spring cache的实现原理是基于AOP的动态代理实现的:即都在方法调用前后去获取方法的名称、参数、返回值,然后根据方法名称、参数生成缓存的key(自定义的key例外),进行缓存。
AOP不支持对private私有方法的拦截,所以也就不支持私有方法上的Spring Cache注解。
this调用不是代理对象的调用, 所以AOP失效,注解失效。
解决办法
方法用
public限定符修饰;类内部方法调用加缓存时可以用
SpringContextUtil获取当前Bean,由它来调用
工具类
SpringContextUtil
@Component
public class SpringContextUtil implements ApplicationContextAware {
public static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
SpringContextUtil.applicationContext = applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
public static Boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static Boolean isSingleton(String name) {
return applicationContext.isSingleton(name);
}
public static Class<? extends Object> getType(String name) {
return applicationContext.getType(name);
}
}
Spring Cache + Caffeine实现本地缓存的更多相关文章
- spring boot:使用spring cache+caffeine做进程内缓存(本地缓存)(spring boot 2.3.1)
一,为什么要使用caffeine做本地缓存? 1,spring boot默认集成的进程内缓存在1.x时代是guava cache 在2.x时代更新成了caffeine, 功能上差别不大,但后者在性能上 ...
- Spring集成GuavaCache实现本地缓存
Spring集成GuavaCache实现本地缓存: 一.SimpleCacheManager集成GuavaCache 1 package com.bwdz.sp.comm.util.test; 2 3 ...
- 使用Spring Cache + Redis + Jackson Serializer缓存数据库查询结果中序列化问题的解决
应用场景 我们希望通过缓存来减少对关系型数据库的查询次数,减轻数据库压力.在执行DAO类的select***(), query***()方法时,先从Redis中查询有没有缓存数据,如果有则直接从Red ...
- Caffeine Cache-高性能Java本地缓存组件
前面刚说到Guava Cache,他的优点是封装了get,put操作:提供线程安全的缓存操作:提供过期策略:提供回收策略:缓存监控.当缓存的数据超过最大值时,使用LRU算法替换.这一篇我们将要谈到一个 ...
- springboot之本地缓存(guava与caffeine)
1. 场景描述 因项目要使用本地缓存,具体为啥不用redis等,就不讨论,记录下过程,希望能帮到需要的朋友. 2.解决方案 2.1 使用google的guava作为本地缓存 初步的想法是使用googl ...
- JAVA缓存规范 —— 虽迟但到的JCache API与天生不俗的Spring Cache
大家好,又见面了. 本文是笔者作为掘金技术社区签约作者的身份输出的缓存专栏系列内容,将会通过系列专题,讲清楚缓存的方方面面.如果感兴趣,欢迎关注以获取后续更新. 有诗云"纸上得来终觉浅,绝知 ...
- Spring Cache缓存技术的介绍
缓存用于提升系统的性能,特别适用于一些对资源需求比较高的操作.本文介绍如何基于spring boot cache技术,使用caffeine作为具体的缓存实现,对操作的结果进行缓存. demo场景 本d ...
- Spring Cache缓存框架
一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...
- Spring Cache扩展:注解失效时间+主动刷新缓存
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 使用guava cache在本地缓存热点数据
某些热点数据在短时间内可能会被成千上万次访问,所以除了放在redis之外,还可以放在本地内存,也就是JVM的内存中. 我们可以使用google的guava cache组件实现本地缓存,之所以选择gua ...
随机推荐
- hugp-MemE关键美化
配置front matter 使用vscode snippet快捷生成front matter 参考博客:vs-code-workflows-for-hugo. markdown-snippets-n ...
- 关于Unity3D第一视角下镜头穿墙的问题解决方法
昨天做室内模型的时候,遇到一个非常棘手的问题,那就是第一视角在室内运行的时候,会出现穿墙的效果.类似下图效果,在靠近墙壁的时候,出现了镜头看见了墙壁外的情况,很显然这是不符合逻辑的.我们要做的就是避免 ...
- 使用 Sa-Token 实现 [记住我] 模式登录、七天免登录
一.需求分析 如图所示,一般网站的登录界面都会有一个 [记住我] 按钮,当你勾选它登录后,即使你关闭浏览器再次打开网站,也依然会处于登录状态,无须重复验证密码: 本文将详细介绍在 Sa-Token中, ...
- navicate的安装使用
1 navicat概述 Navicat for MySQL 是管理和开发 MySQL 或 MariaDB 的理想解决方案. 这套全面的前端工具为数据库管理.开发和维护提供了一款直观而强大的图形界面. ...
- 4.1 探索LyScript漏洞挖掘插件
在第一章中我们介绍了x64dbg这款强大的调试软件,通过该软件逆向工程师们可以手动完成对特定进程的漏洞挖掘及脱壳等操作,虽然x64dbg支持内置Script脚本执行模块,但脚本引擎通常来说是不够强大的 ...
- 介绍Vue router的history模式以及如何配置history模式
引言 Vue router给我们提供了两种路由模式,分别是hash模式和history模式.其中默认是使用hash模式,即URL中带有一个#符号,但是处于业务或个人喜爱的差别,Vue router也提 ...
- Lens Shading成因及相关
一个监控摄像头光学处理包含以下几个部分:镜头(Lens)(定变焦镜头).红外截止滤波片(IR-cut filter)(红外截止滤光片和蓝玻璃滤光片为主).图像传感器(Image Sensor)和印制电 ...
- eclipse在主题商城下载安装黑色主题
Eclipse配置黑色主题方法: 1. 借用国外一个Elipse主题网站分享的主题配置文件来配置一个黑色的主题. 主题网址 2. 在这个网站下载自己喜欢的主题,单击主题进入下载页面,建议大家选择EPF ...
- 封装一个可以左右滑动的Blazor组件
为什么要封装组件 最近写MAUI Blazor的时候,总是苦于对移动端没有什么好的支持,没有一个能左右滑动的tab切换组件. 既然没有,那就自己封装一个. 简单了解轮播图.tab切换的库之后,决定使用 ...
- 数据库是要拿来用的,不是用来PK先进性的
周五参加了WAIC后又和一家上海本地的数据库厂商交流了一下午.等我要买高铁票回南京的时候已经买不到票了.好不容易刷到一张到苏州北的高铁票,我就上了车.上车后突然想起还不如就回苏州老家住一晚算了.到家后 ...