@CachePut |学习笔记

简介: 快速学习 @CachePut

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

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


@CachePut

@CachePut:

既调用方法,又更新缓存数据;同步更新缓存

修改了数据库的某个数据,同时更新缓存;

运行时机:

先调用目标方法

将只标玄法的结果缓存起来

测试步骤:

查询1号员工;查到的结果会放到缓存中

以后查询还是之前的结果

更新1号员工;【lastName:zhangsan;gender:θ】

将方法的返回值也放进缓存了;

*key:传入的 employee 对象值:返回的 employee 对象;

*key="#employee. id":使用传入的参数的员工 id;key=“#result. id:使用返回后的 id Qeachacle 的 key 是不能用 #result

@CachePut(value="emp",key="#result.id")

public Employee updateEmp(Employee employee){

System. out. println("updateEmp:"+emplo/ee);

emp1oyeeMapper .updateEmp(employee);

return employee;

查询1号员工?  

应该是更新后的员工;

为什么是没更新前的?

因为第一步查询后放入了缓存中,1号员工在缓存中没有更新。

更新之后会给缓存中放 key

@Cach&Put(value="emp")

publicEmployeeupdateEmp(Employee employee){ employeeaper .updatemρ(emρleme); return employee;

}

}

package com. atguigu. cache.  controller ;

import...

@ RestController

public class  EmployeeController {

@Autowired

EmployeeService  em  ployeeService ;

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

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

return employee;

}

@ GetMapping ("/empj")

public Employee update(Employee employee){

Employee emp= emp1oyeeService .updateEmp(employee);

return emp;

}

相关文章
|
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 数据库
@ CacheEvict |学习笔记
快速学习 @ CacheEvict
102 0
|
缓存 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 Cache --- @Cacheable/@CachePut/@CacheEvict缓存注解相关基础类打点【享学Spring】(上)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict缓存注解相关基础类打点【享学Spring】(上)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict缓存注解相关基础类打点【享学Spring】(上)
|
消息中间件 设计模式 缓存
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict缓存注解相关基础类打点【享学Spring】(中)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict缓存注解相关基础类打点【享学Spring】(中)
玩转Spring Cache --- @Cacheable/@CachePut/@CacheEvict缓存注解相关基础类打点【享学Spring】(中)