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 ...
随机推荐
- hdu 5126 stars (四维偏序,离线,CDQ套CDQ套树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5126 思路:支持离线,那么我们可以用两次CDQ分治使四维降为二维,降成二维后排个序用树状数组维护下就好 ...
- MT【73】求函数表达式
评:由关系式求表达式最经典的莫过于已知$f(x+y)=f(x)f(y)$利用柯西法求得 $f(x)=[f(1)]^x$
- 【 Gym - 101124E 】Dance Party (数学)
BUPT2017 wintertraining(15) #4G Gym - 101124 E.Dance Party 题意 有c种颜色,每个颜色最多分配给两个人,有M个男士,F个女士,求至少一对男士同 ...
- Leetcode 1.两数之和 By Python
思路 很容易想到的方法是二重循环遍历一遍,但是会很慢 把加法变减法可以大大加速 代码 class Solution: def twoSum(self, nums, target): "&qu ...
- 【BZOJ1823】[JSOI2010]满汉全席(2-sat)
[BZOJ1823][JSOI2010]满汉全席(2-sat) 题面 BZOJ 洛谷 题解 很明显的\(2-sat\)模板题,还不需要输出方案. 对于任意两组限制之间,检查有无同一种石材要用两种不同的 ...
- 使用android快速开发框架afinal的FinalDb操作android数据库
http://my.oschina.net/yangfuhai/blog/87459 今天给大家介绍下#afinal#来操作android的数据库sqlite. #afinal#是一个android的 ...
- Ubuntu下安装tftp
用户可以在主机系统联网的情况下,在终端输入下面命令进行安装: vmuser@Linux-host: ~$ sudo apt-get install tftpd-hpa tftp-hpa 配置 TFTP ...
- 2018.9南京网络预选赛(J)
传送门:Problem J https://www.cnblogs.com/violet-acmer/p/9720603.html 变量解释: need[ i ] : 第 i 个房间含有的旧灯泡个数. ...
- 2018.9青岛网络预选赛(K)
传送门:Problem K https://www.cnblogs.com/violet-acmer/p/9664805.html 题意: 给你n个数,找出满足条件的最多的数的个数. 题解: 满足条件 ...
- Jenkins-Pipeline 流水线发布部署项目
node { sh 'mkdir -p cms' dir('cms') { git branch: 'prerelease', credentialsId: '5fb79ef0-4301-4b7c-a ...