使用Spring Cache设置缓存条件操作
作者:梁云亮 发布时间:2023-01-25 16:38:06
标签:Spring,Cache,缓存
Spring Cache设置缓存条件
原理
从Spring3.1开始,Spring框架提供了对Cache的支持,提供了一个对缓存使用的抽象,通过在既有代码中添加少量它定义的各种 annotation,即能够达到缓存方法的返回对象的作用。
提供的主要注解有@Cacheable、@CachePut、@CacheEvict和@Caching,具体见下表:
注解 | 说明 |
---|---|
@Cacheable | 可以标注在类或方法上:标注在方法上表示该方法支持数据缓存;标在类上表示该类的所有方法都支持数据缓存。 具体功能:在执行方法体之前,检查缓存中是否有相同key值的缓存存在,如果存在对应的缓存,直接返回缓存中的值;如果不存在对应的缓存,则执行相应的方法体获取数据,并将数据存储到缓存中。 |
@CachePut | 可以标注在类或方法上,表示支持数据缓存。 具体功能:在方法执行前不会检查缓存中是否存在相应的缓存,而是每次都会执行方法体,并将方法执行结果存储到缓存中,如果相应key值的缓存存在,则更新key对应的value值。 |
@CacheEvict | 可以标注在类或方法上,用于清除相应key值的缓存。 |
@Caching | 可以标注在类或方法上,它有三个属性cacheable、put、evict分别用于指定@Cacheable、@CachePut和@CacheEvict |
当需要在类上或方法上同时使用多个注解时,可以使用@Caching,如:
@Caching(cacheable=@Cacheable("User"), evict = {@CacheEvict("Member"), @CacheEvict(value = "Customer", allEntries = true)})
@Cacheable的常用属性及说明
如下表所示:
@Cacheable属性 | 说明 |
---|---|
key | 表示缓存的名称,必须指定且至少要有一个值,比如:@Cacheable(value=“Dept”)或@Cacheable(value={“Dept”,“Depts”}) |
condition | 表示是否需要缓存,默认为空,表示所有情况都会缓存。通过SpEL表达式来指定,若condition的值为true则会缓存,若为false则不会缓存,如@Cacheable(value=“Dept”,key="‘deptno_'+# deptno “,condition=”#deptno<=40") |
value | 表示缓存的key,支持SpEL表达式,如@Cacheable(value=“Dept”,key="‘deptno_' +#deptno"),可以不指定值,如果不指定,则缺省按照方法的所有参数进行组合。除了上述使用方法参数作为key之外,Spring还提供了一个root对象用来生成key,使用方法如下表所示,其中"#root"可以省略。 |
Root对象
Root对象 | 说明 |
---|---|
methodName | 当前方法名,比如#root.methodName |
method | 当前方法,比如#root.method.name |
target | 当前被调用的对象,比如#root.target |
targetClass | 当前被调用的对象的class,比如#root.targetClass |
args | 当前方法参数组成的数组,比如#root.args[0] |
caches | 当前被调用的方法使用的缓存,比如#root.caches[0].name |
@CachePut的常用属性同@Cacheable
@CacheEvict的常用属性如下表所示:
@CacheEvict属性 | 说明 |
---|---|
value | 表示要清除的缓存名 |
key | 表示需要清除的缓存key值, |
condition | 当condition的值为true时才清除缓存 |
allEntries | 表示是否需要清除缓存中的所有元素。默认为false,表示不需要,当指定了allEntries为true时,将忽略指定的key。 |
beforeInvocation | 清除操作默认是在方法成功执行之后触发的,即方法如果因为抛出异常而未能成功返回时不会触发清除操作。使用beforeInvocation可以改变触发清除操作的时间,当该属性值为true时,会在调用该方法之前清除缓存中的指定元素。 |
示例:设置当 dname 的长度大于3时才缓存
//条件缓存
@ResponseBody
@GetMapping("/getLocByDname")
@Cacheable(cacheNames = "dept", key = "#dname", condition = "#dname.length()>3")
public String getLocByDname(@RequestParam("dname") String dname) {//key动态参数
QueryWrapper<Dept> queryMapper = new QueryWrapper<>();
queryMapper.eq("dname", dname);
Dept dept = deptService.getOne(queryMapper);
return dept.getLoc();
}
示例:unless 即条件不成立时缓存
#result 代表返回值,意思是当返回码不等于 200 时不缓存,也就是等于 200 时才缓存。
@ResponseBody
@GetMapping("/getDeptByDname")
@Cacheable(cacheNames = "dept", key = "#dname", unless = "#result.code != 200")
public Result<Dept> getDeptByDname(@RequestParam("dname") String dname){//key动态参数
QueryWrapper<Dept> queryMapper = new QueryWrapper<>();
queryMapper.eq("dname", dname);
Dept dept = deptService.getOne(queryMapper);
if (dept == null)
return ResultUtil.error(120, "dept is null");
else
return ResultUtil.success(dept);
}
Cache缓存配置
1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 反射工具类用于手动扫描指定包下的注解,根据defaultCache模块增加ehcache缓存域(非Spring Cache必须)-->
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
2、Ehcache配置文件
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir" />
<!--
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
-->
<!-- 默认缓存 -->
<defaultCache
eternal="false"
maxElementsInMemory="200000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU" />
</ehcache>
3、配置类
@Configuration
@EnableCaching
public class CustomConfiguration {
/**
* @see org.springframework.cache.interceptor.SimpleKeyGenerator
* Generate a key based on the specified parameters.
*/
public static Object generateKey(Object... params) {
if (params.length == 0) {
return SimpleKey.EMPTY;
}
if (params.length == 1) {
Object param = params[0];
if (param != null && !param.getClass().isArray()) {
return param;
}
}
return new SimpleKey(params);
}
/**
* 若将target作为key的一部分时,CGLIB * 可能导致重复缓存
* 注意:返回的key一定要重写hashCode()和toString(),防止key对象不一致导致的缓存无法命中
* 例如:ehcache 底层存储net.sf.ehcache.store.chm.SelectableConcurrentHashMap#containsKey
*/
@Bean
public KeyGenerator customKeyGenerator(){
return (target, method, params) -> {
final Object key = generateKey(params);
StringBuffer buffer = new StringBuffer();
buffer.append(method.getName());
buffer.append("::");
buffer.append(key.toString());
// 注意一定要转为String,否则ehcache key对象可能不一样,导致缓存无法命中
return buffer.toString();
};
}
/**
* redis缓存管理器
*/
@Bean
@ConditionalOnBean(RedisConfiguration.class)
@ConditionalOnProperty(prefix = "spring.cache", name = "type", havingValue = "redis",
matchIfMissing = false)
public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
.entryTtl(Duration.ofMinutes(10));
return RedisCacheManager
.builder(RedisCacheWriter.lockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(config).build();
}
/**
* ehcache缓存管理器(默认)
* default XML files {@link net.sf.ehcache.config.ConfigurationFactory#parseConfiguration()}
*/
@Bean
@ConditionalOnProperty(prefix = "spring.cache", name = "type", havingValue = "ehcache",
matchIfMissing = true)
public CacheManager ehcacheCacheManager() {
net.sf.ehcache.CacheManager cacheManager = net.sf.ehcache.CacheManager.create();
/**
* 包扫描查找指定注解并将cacheNames添加到net.sf.ehcache.CacheManager(单例)
*/
Reflections reflections = new Reflections("com.example.demo.service", new TypeAnnotationsScanner()
, new SubTypesScanner(), new MethodAnnotationsScanner());
Set<Class<?>> classesList = reflections.getTypesAnnotatedWith(CacheConfig.class);
for (Class<?> aClass : classesList) {
final CacheConfig config = AnnotationUtils.findAnnotation(aClass, CacheConfig.class);
if (config.cacheNames() != null && config.cacheNames().length > 0) {
for (String cacheName : config.cacheNames()) {
cacheManager.addCacheIfAbsent(cacheName);
}
}
}
/**
* 方法级别的注解 @Caching、@CacheEvict、@Cacheable、@CachePut,结合实际业务场景仅扫描@Cacheable即可
*/
final Set<Method> methods = reflections.getMethodsAnnotatedWith(Cacheable.class);
for (Method method : methods) {
final Cacheable cacheable = AnnotationUtils.findAnnotation(method, Cacheable.class);
if (cacheable.cacheNames() != null && cacheable.cacheNames().length > 0) {
for (String cacheName : cacheable.cacheNames()) {
cacheManager.addCacheIfAbsent(cacheName);
}
}
}
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
ehCacheCacheManager.setCacheManager(cacheManager);
return ehCacheCacheManager;
}
}
4、示例
@Component
@CacheConfig(cacheNames = "XXXServiceImpl", keyGenerator = "customKeyGenerator")
public class XXXServiceImpl extends ServiceImpl<XXXMapper, XXXEntity> implements XXXService {
@CacheEvict(allEntries = true)
public void evictAllEntries() {}
@Override
@Cacheable
public List<XXXEntity> findById(Long id) {
return this.baseMapper.selectList(new QueryWrapper<XXXEntity>().lambda()
.eq(XXXEntity::getId, id));
}
}
来源:https://hcshow.blog.csdn.net/article/details/119271227
0
投稿
猜你喜欢
- SqlssionFactory1.SqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像
- 目录一、图示二、链表的概念及结构三、单链表的实现四、完整代码的展示一、图示二、链表的概念及结构 链表是一种物理存储结构上非连续存储结构,数据
- 一、简介JetCache是一个基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用。 JetCache提供了比SpringCa
- 本文实例讲述了java识别一篇文章中某单词出现个数的方法。分享给大家供大家参考。具体如下:1. java代码:import java.io.
- 一,项目简介经过调查研究进行开发设计的这款仓库管理系统,主要是为商家提供商品货物进销存的信息化管理,以便让商家在竞争如此激烈的今天占据一定的
- 输入方法第一种输入方法:scannerimport java.util.Scanner; // 导入java.util.Scannerpub
- 一、平衡二叉树的定义平衡二叉树是一种二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1 。它是一种高度平衡的二叉排序树。意思是说,
- 一、准备工作mybatis-plus作为mybatis的增强工具,它的出现极大的简化了开发中的数据库操作,但是长久以来,它的联表查询能力一直
- Selenium 是目前用的最广泛的Web UI 自动化测试框架。 本系列文章,将深入简出来讲解selenium 的用法阅读目录seleni
- JetBrains 系列产品(IDEA、Pycharm 等)使用本站破解教程 (opens new window),在输入激活码时,部分小伙
- 最近在搭建springmvc的框架,遇到的这样的问题:在地址栏访问登陆界面访问不了,http://localhost/XXXX/WEB-IN
- 本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js、css这些静态文件的加载配置,以及服务器推送的两种实现方式。当然我们在
- 目录 * 仓库的配置1、 下载sonatype Nexus来搭建 * 2 安装nexus服务3、创建 * 仓库4、配置 * 信息中央仓库的配置三个仓
- ApplicationContext简述ApplicationContext代表IOC容器,在SpringIOC容器中读取Bean配置创建B
- 根据中国的国情,宽带共享遭受dns污染和HTTP拦截非常严重,造成网络请求的不稳定.但是ip/tcp协议一般不受影响。因此可以把域名先解析成
- 写在前面 众所周知,kafka是现代流行的消息队列,它使用经典的消息订阅发布模式实现消息的流转,大部分代码结合kaf
- 这是一个可以从乱码文本中得到正确的原始文本的程序,其基于的原理在于错误的编码往往导致位补充,因此正确的文本使用的字节数应该是最少的(之一)。
- ES是一个基于Lucene的分布式全文搜索服务器,和SQL Server的全文索引(Fulltext Index)有点类似,都是基于分词和分
- final File imageFile = new File(getCacheDir().getPath() + "/img/&
- 基本流foreach 实现了外部迭代,它是建立于集合的基础上;stream 流实现的是内部迭代中间操作与终端操作对任意集合使用 stream