@ CacheEvict |学习笔记

简介: 快速学习 @ CacheEvict

开发者学堂课程【SpringBoot 快速掌握 - 高级应用@ CacheEvict】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址https://developer.aliyun.com/learning/course/613/detail/9293


@ CacheEvict


@CacheEvict :缓存清除

key:指定要清除的数据

alLEntries =true:指定清除这个缓存中所有的数据

@ CacheEvict 与其他不同的是多了 boolean  beforeInvocation () default false;翻译就是是否在方法之前执行。

默认代表缓存清除操作是在方法之后执行,如果出现异常缓存就不会清除

beforeInvocation =true:代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除。

如果删掉2号员工,方法出现异常,缓存就不会被清除。

@CacheEv(value="emp",key="#id")

public void deleteEmp(Integer id){

System. out. println("de1eteEmp:"+id)}  

// employeeMapper . deleteEmpById (id);

}

测试过程如下:

package com. atguigu. cache.  controller ;

import,.

@ RestController

public class  EmployeeController {

@Autowired

EmployeeService  employee Service;

@ GetMapping ("/emp/{id}")

publicEmployeegetEmployee (@ PathVariable ("id")Integerid){Employee employee= emp1oyeeService .getEmp(id);

return employee;

}

@ GetMapping ("/emp")

public Employee update(Employee employee){

Employee emp= employeeService .updateEmp(employee);

return emp;

)

@ GetMapping ("de1emp")

public String deleteEmp(Integer id){

return"success"

}

}

控制台打印出查询1号员工,说明用了数据库。

查2号员工,控制台没有发语句。

将所以数据删除之后,发现查询1号员工会查数据库,第二次查询不会查数据库。

查询2号员工,控制台还会被打印。

相关文章
|
4月前
|
缓存 NoSQL Java
Spring Cache之本地缓存注解@Cacheable,@CachePut,@CacheEvict使用
SpringCache不支持灵活的缓存时间和集群,适合数据量小的单机服务或对一致性要求不高的场景。`@EnableCaching`启用缓存。`@Cacheable`用于缓存方法返回值,`value`指定缓存名称,`key`定义缓存键,可按SpEL编写,`unless`决定是否不缓存空值。当在类上使用时,类内所有方法都支持缓存。`@CachePut`每次执行方法后都会更新缓存,而`@CacheEvict`用于清除缓存,支持按键清除或全部清除。Spring Cache结合Redis可支持集群环境。
239 6
|
9月前
|
缓存 Java Spring
Cacheable CacheEvict CachePut
Cacheable CacheEvict CachePut
31 0
|
缓存 Java 数据库
@CachePut |学习笔记
快速学习 @CachePut
|
缓存 Java 开发者
@Cacheable 其他属性|学习笔记
快速学习 @Cacheable 其他属性
|
SQL 缓存 JSON
自定义 CacheManager |学习笔记
快速学习自定义 CacheManager
360 0
|
缓存 Java Spring
SpringBoot - @Cacheable、@CacheEvict、@CachePut(二)
SpringBoot - @Cacheable、@CacheEvict、@CachePut(二)
219 0
SpringBoot - @Cacheable、@CacheEvict、@CachePut(二)
|
XML 缓存 Java
SpringBoot - @Cacheable、@CacheEvict、@CachePut(一)
SpringBoot - @Cacheable、@CacheEvict、@CachePut(一)
228 0
SpringBoot - @Cacheable、@CacheEvict、@CachePut(一)
|
缓存 NoSQL Java
Spring系列缓存注解@Cacheable @CacheEvit @CachePut 使用姿势介绍
Spring在3.1版本,就提供了一条基于注解的缓存策略,实际使用起来还是很丝滑的,本文将针对几个常用的注解进行简单的介绍说明,有需要的小伙伴可以尝试一下
242 0
Spring系列缓存注解@Cacheable @CacheEvit @CachePut 使用姿势介绍
|
缓存 Java Spring
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用【享学Spring】(下)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用【享学Spring】(下)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用【享学Spring】(下)
|
缓存 Java Spring
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用【享学Spring】(上)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用【享学Spring】(上)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用【享学Spring】(上)