SpringBoot缓存
(1)、使用@EnableCaching注解开启基于注解的缓存
package cn.coreqi; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication
@EnableCaching //开启基于注解的缓存
public class SpringbootjdbcApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootjdbcApplication.class, args);
} }
(2)、对使用缓存的方法添加缓存注解
package cn.coreqi.service; import cn.coreqi.dao.UserRepository;
import cn.coreqi.entities.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service; import java.util.List;
import java.util.Optional; @Service
//@CacheConfig(cacheNames = "user") //抽取缓存的公共配置
//可以使用@Caching注解在方法上运用多个缓存注解
public class UserService {
@Autowired
private UserRepository userRepository; public User addUser(User user){
return userRepository.save(user);
} /**
* @CachePut:调用方法并同步更新缓存,修改了数据库的某个数据同时更新缓存
* 运行流程
* 1.先调用目标方法
* 2.将运行结果缓存起来并同步更新缓存
* @CachePut的Key可以使用#result拿到运行结果
* @param user
* @return
*/
@CachePut(cacheNames = "user",key = "#result.Id")
public User modifyUser(User user){
return userRepository.save(user);
}
public List<User> getList(){
return userRepository.findAll();
} /**
* @Cacheable标注的方法在执行之前先来检查缓存中有没有这个数据,如果没有就运行方法并将结果放入缓存
* 默认按照参数的值作为Key去查询缓存
* 几个重要属性:
* cacheNames/value:指定当前缓存所在Cache组件的名称
* key:缓存数据使用的Key,默认是方法的入参和返回值组合,Key支持SpEL表达式
* keyGenerator:Key的生成器,可以自己指定Key的生成策略(key和keyGenerator二选一)
* cacheManager:指定缓存管理器,或者cacheResolver指定缓存解析器
* condition:指定符合条件的情况下才缓存数据
* unless:否定缓存,当unless指定的条件为True的情况下方法的返回值就不会被缓存,可以利用获取到的结果进行判断
* sync:是否使用异步模式
* @param id
* @return
*/
@Cacheable(cacheNames = "user")
public User getById(Integer id){
System.out.println("查询数据Id:" + id);
Optional<User> user = userRepository.findById(id);
return user.get();
} /**
* @CacheEvict:清除缓存
* 几个重要属性:
* key:指定要清除数据的Key
* allEntries:是否清除这个缓存中所有缓存数据
* beforeInvocation:缓存的清除是否在方法运行之前执行,默认False
* @param id
*/
@CacheEvict(cacheNames = "user",key = "#id")
public void delById(Integer id){
userRepository.deleteById(id);
}
}
*缓存支持的SpEL表达式
| 描述 | 示例 |
| 当前被调用的方法名 |
#root.methodName |
| 当前被调用的方法 |
#root.method.name |
| 当前被调用的目标对象 |
#root.target |
| 当前被调用的目标对象类 |
#root.targetClass |
| 当前被调用的方法的参数 |
#root.args[0] |
|
当前方法调用使用的缓存列表 (如@cacheable(value={"cache1","cache2"}),则有两个cache) |
#root.caches[0].name |
| 方法参数的名字,可以直接#参数名称,也可以使用#p0的形式,0代表参数索引 |
#Id、#users0 |
| 方法执行后的返回值(仅当方法执行之后的判断有效,如“unless”,“cachePut”的表达式,“cacheEvict的表达式”,beforeInvocation=false) |
#result |
SpringBoot缓存的更多相关文章
- SpringBoot缓存之redis--最简单的使用方式
第一步:配置redis 这里使用的是yml类型的配置文件 mybatis: mapper-locations: classpath:mapping/*.xml spring: datasource: ...
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- SpringBoot缓存管理(二) 整合Redis缓存实现
SpringBoot支持的缓存组件 在SpringBoot中,数据的缓存管理存储依赖于Spring框架中cache相关的org.springframework.cache.Cache和org.spri ...
- springboot缓存开发
前言:缓存在开发中是一个必不可少的优化点,近期在公司的项目重构中,关于缓存优化了很多点,比如在加载一些数据比较多的场景中,会大量使用缓存机制提高接口响应速度,简介提升用户体验.关于缓存,很多人对它都是 ...
- springboot缓存的使用
spring针对各种缓存实现,抽象出了CacheManager接口,用户使用该接口处理缓存,而无需关心底层实现.并且也可以方便的更改缓存的具体实现,而不用修改业务代码.下面对于在springboot中 ...
- springboot缓存及连接池配置
参见https://coding.imooc.com/lesson/117.html#mid=6412 1.springboot的springweb自己默认以及配置好了缓存,只需要在主文件(XxxAp ...
- SpringBoot 缓存注解 与EhCache的使用
在SpringBoot工程中配置EhCache缓存 1.在src/main/resources下新建ehcache.xml文件 eternal=true //缓存永久有效,false相反 maxEle ...
- SpringBoot 缓存模块
默认的缓存配置 在诸多的缓存自动配置类中, SpringBoot默认装配的是SimpleCacheConfigguration, 他使用的CacheManager是 CurrentMapCacheMa ...
- 转载-springboot缓存开发
转载:https://www.cnblogs.com/wyq178/p/9840985.html 前言:缓存在开发中是一个必不可少的优化点,近期在公司的项目重构中,关于缓存优化了很多点,比如在加载 ...
- SpringBoot缓存技术
一.SpringBoot整合Ehhcache 添加maven依赖 <dependency> <groupId>org.springframework.boot</grou ...
随机推荐
- day9-13 linux基础
有道云笔记链接 http://note.youdao.com/noteshare?id=207be3d6bd79e9ff2e30b160bca1fd87
- 自学Python5.2-类和对象概念
自学Python之路 自学Python5.2-类和对象概念 面向对象编程的2个非常重要的概念:类和对象 对象是面向对象编程的核心: 在使用对象的过程中,为了将具有共同特征和行为的一组对象抽象定义,提出 ...
- tf 数据读取
tf.train.batch( tensors, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, dy ...
- A1054. The Dominant Color
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...
- 那些神奇的before和after使用方法
在英文单词里面:before是在什么之前.after是在什么之后.诚然,在我们的css里面, 通过使用before和after伪类元素,可以在我们想要的元素前面或者后面插入内容. 下面是使用befor ...
- Maven web 项目工程的建立
打开eclipse,mars版本的已经集成了maven. 1. new 一个 maven project,勾选Create a simple project(这样就省去了建立文件夹的过程) 2. 选择 ...
- (qsort)绝对值排序
绝对值排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- sublime 成对括号高亮显示设置
ctrl+shift+p/pcip(Package Control : Install Package)/ BracketHighlighter(括号高亮插件)
- UVALive 4850 Installations 贪心
题目链接 题意 工程师要安装n个服务,其中服务Ji需要si单位的安装时间,截止时间为di.超时会有惩罚值,若实际完成时间为ci,则惩罚值为max{0,ci-di}.从0时刻开始执行任务,问惩罚值最大 ...
- FZU - 1688 Binary land
题目链接 Problem 1688 Binary land Accept: 72 Submit: 171Time Limit: 1000 mSec Memory Limit : 3276 ...