1. 缓存、两级缓存
1.1 内容说明
spring cache:主要包含spring cache定义的接口方法说明和注解中的属性说明
springboot+spring cache:rediscache实现中的缺陷
caffeine简介
spring boot+spring cache实现两级缓存
使用缓存时的流程图

1.2 sping cache
spring cache是spring-context包中提供的基于注解方式使用的缓存组件,定义了一些标准接口,通过实现这些接口,就可以通过在方法上增加注解来实现缓存。这样就能够避免缓存代码与业务处理耦合在一起的问题。spring cache的实现是使用spring aop中对方法切面(methodinterceptor)封装的扩展,当然spring aop也是基于aspect来实现的。
spring cache核心的接口就两个:cache和cachemanager
1.2.1 cache接口
提供缓存的具体操作,比如缓存的放入,读取,清理,spring框架中默认提供的实现有

1.2.2 cachemanager接口
主要提供cache实现bean的创建,每个应用里可以通过cachename来对cache进行隔离,每个cahename对应一个cache实现,spring框架中默认提供的实现与cache的实现都是成对出现的
1.2.3 常用的注解说明
- @cacheable:主要应用到查询数据的方法上
- @cacheevict:清除缓存,主要应用到删除数据的方法上
- @cacheput:放入缓存,主要用到对数据有更新的方法上
- @caching:用于在一个方法上配置多种注解
- @enablecaching:启用spring cache缓存,作为总的开关,在spring boot的启动类或配置类上需要加入次注解才会生效
2.实战多级缓存的用法
package com.xfgg.demo.config;
import lombok.allargsconstructor;
import com.github.benmanes.caffeine.cache.caffeine;
import org.springframework.cache.cachemanager;
import org.springframework.cache.caffeine.caffeinecachemanager;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import java.util.concurrent.timeunit;
@configuration
@allargsconstructor
//把定义的缓存加入到caffeine中
public class cacheconfig {
@bean
public cachemanager cachemanager(){
caffeinecachemanager cachemanager = new caffeinecachemanager();
cachemanager.setcaffeine(caffeine.newbuilder()
//使用refreshafterwrite必须要设置cacheloader
//在5分钟内没有创建/覆盖时,会移除该key,下次取的时候从loading中取【重点:失效、移除key、失效后需要获取新值】
.expireafterwrite(5, timeunit.minutes)
//初始容量
.initialcapacity(10)
//用来控制cache的最大缓存数量
.maximumsize(150)
);
return cachemanager;
}
}
package com.xfgg.demo.config;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.bean;
import org.springframework.data.redis.connection.redisconnectionfactory;
import org.springframework.data.redis.connection.redispassword;
import org.springframework.data.redis.connection.redisstandaloneconfiguration;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.serializer.stringredisserializer;
//生成的redis连接
public class redisconfig<genericobjectpoolconfig> {
@value("${spring.redis1.host}")
private string host;
@value("${spring.redis1.port}")
private integer port;
@value("${spring.redis1.password}")
private string password;
@value("${spring.redis1.database}")
private integer database;
@value("${spring.redis1.lettuce.pool.max-active}")
private integer maxactive;
@value("${spring.redis1.lettuce.pool.max-idle}")
private integer maxidle;
@value("${spring.redis1.lettuce.pool.max-wait}")
private long maxwait;
@value("${spring.redis1.lettuce.pool.min-idle}")
private integer minidle;
@bean
public redisstandaloneconfiguration redis1redisconfig() {
redisstandaloneconfiguration config = new redisstandaloneconfiguration();
config.sethostname(host);
config.setpassword(redispassword.of(password));
config.setport(port);
config.setdatabase(database);
return config;
}
//配置序列化器
@bean
public redistemplate<string,object> redistemplate(redisconnectionfactory factory){
redistemplate<string,object>template=new redistemplate<>();
//关联
template.setconnectionfactory(factory);
//设置key的序列化器
template.setkeyserializer(new stringredisserializer());
//设置value的序列化器
template.setvalueserializer(new stringredisserializer());
return template;
}
}
一个使用cacheable注解,一个使用redistemplate进行缓存
因为公司项目中用到的是jedis和jediscluster所以这里只是做个了解,没有写的很细
到此这篇关于springboot+springcache实现两级缓存(redis+caffeine)的文章就介绍到这了,更多相关springboot springcache两级缓存内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
小明________________