springboot的几个缓存相关注解
@Cacheable:查询
几个属性:
cacheNames/value:指定缓存组件的名字;
key:缓存数据使用的key,可以用来指定。默认即使用方法参数的值
keyGenerator:key的生成器,可以自己指定key的生成器的组件id
//自定义配置类配置keyGenerator
@Configuration
public class MyCacheConfig {
@Bean("myKeyGenerator")
public KeyGenerator keyGenerator(){
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
return method.getName()+"["+ Arrays.asList(params).toString() +"]";
}
};
}
}
cacheManager:指定缓存管理器;或者cacheResolver获取指定解析器
condition:指定符合条件的情况下才缓存;如condition="#id>0"
unless:否定缓存,当unless指定的条件为true,方法的返回值不会被缓存,可以获取到结果进行判断;如unless="#result==null";
sync:是否使用异步模式
@Cacheable(cacheNames = "user",keyGenerator = "myKeyGenerator")
public User getUser(Integer id) {
System.out.println("查询" + id + "号用户");
User user = userMapper.getUserId(id);
return user;
}
@CachePut:更新
既调用方法,又更新缓存数据,可达到同步更新缓存;
修改了数据库的某个数据,同时更新缓存
运行时机:
1、先调用运行方法;2、将目标方法的结果缓存起来
value:缓存名 key:缓存的key其中#result表示方法返回的结果(确保更新的key和查询一致即可做到同时更新数据库数据和缓存中的数据)
@CachePut(value="user",key = "#result.id")
public User updateUser(User user){
System.out.println("updateUser:"+user);
userMapper.updateUser(user);
return user;
}
@CacheEvict:删除
缓存清除:目的是为了删除一个数据并删掉缓存
key:指定要清除的数据(对应上key可实现目的即同时做到删除数据库和缓存中的数据)
allEntries =true:指定清楚这个缓存中所有的数据
beforeInvocation = false:缓存的清楚是否在方法之前执行,默认代表是在方法之后执行
@CacheEvict(value = "user",key = "#id")
public void deleteUser(Integer id){
System.out.println("deleteUser:"+id);
userMapper.deleteUserById(id);
}
@Caching:定义复杂的缓存规则
@Caching(
cacheable = {
@Cacheable()
},
put = {
@CachePut(),
@CachePut()
},
evict = {
@CacheEvict()
}
)
public 返回值 方法名(参数类型 参数){
return 返回结果;
}
@CacheConfig:类公共配置
加在类上,为当前类统一配置,具体进入注解中查看可设置属性,如value="?"统一类下所有的缓存名
springboot的几个缓存相关注解的更多相关文章
- 【玩转SpringBoot】用好条件相关注解,开启自动配置之门
自动配置隐含两层含义,要搞清楚 上帝让程序员的发量减少,是为了让他变得更聪明,如果有一天聪明到了极点,那就是绝顶聪明. 据说在大脑高速运转下,这样更有利于散热,不至于核心温度过高而产生告警. 聪明的大 ...
- 【SpringBoot实战】核心配置和注解
前言 SpringBoot核心配置在springboot中有非常重要的作用,我们可是使用核心配置文件进行一些基础功能的定义,属性值的注入等.springboot支持两种格式的核心配置文件,一种是pro ...
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...
- springboot 2 集成 redis 缓存 序列化
springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...
- SpringBoot集成Redis实现缓存处理(Spring AOP实现)
第一章 需求分析 计划在Team的开源项目里加入Redis实现缓存处理,因为业务功能已经实现了一部分,通过写Redis工具类,然后引用,改动量较大,而且不可以实现解耦合,所以想到了Spring框架的A ...
- SpringBoot 使用 EhCache2.x 缓存(三十一)
SpringBoot 使用 EhCache2.x 缓存入门很简单,废话少说上干货: 1.在POM.xml中增加jar包 <!--开启 cache 缓存--> <dependency& ...
- springboot redis-cache 自动刷新缓存
这篇文章是对上一篇 spring-data-redis-cache 的使用 的一个补充,上文说到 spring-data-redis-cache 虽然比较强悍,但还是有些不足的,它是一个通用的解决方案 ...
- 在springboot中使用redis缓存,将缓存序列化为json格式的数据
背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...
- springboot系列:使用缓存
前言:springboot已经为我们实现了抽象的api接口,因此当我们使用不同的缓存时,只是配置有可能有点区别(比如ehcache和Redis),但是在程序中使用缓存的方法是一样的. 1.spring ...
随机推荐
- Python for Tkinter
# tkinter常用组件- 按钮 - button(按钮组件) - RadioButton(单选框组件) - CheckButton(选择按钮组件) - Listbox(列表框组件) - 文本输入组 ...
- zTree 无子节点 单击事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 转-----------------------js window.open() 操作
<% if request("infoid")<>"" then set rs=conn.execute("select * fro ...
- 基于mybatis的CRUD
u 基于Mybatis的CRUD u 掌握MyBatis的结果类型-resultMap和resultType u 掌握MyBatis的参数类型 u 掌握#和$两种语法 1 基于myb ...
- Linux下 利用find命令删除所有.svn目录
====================实例============== 删除所有.svn目录 这也是我当初查找 Linux find 命令的目的. 1) find . -type d -name ...
- BA-siemens-desigo_cc安装
1.首先安装NT3.5和NT4.0软件 2.按照以下网址的教程配置好IIS和WEBDAV环境:http://www.cnblogs.com/xiongzai/p/4126493.html 文章写的真不 ...
- 数据库连接池dataesoruce pool深入理解
8.数据库连接池的connection都是长连接的,以方便多次调用,多人连续使用.dataSourcePool9.数据库连接池中的连接,是在你用完之后,返回给数据库连接池的,并不是close()掉,而 ...
- HDU 1431
可以先找出回文数,再用素数测试来判是否为素数即可. 打回文数时,因为左右对称,可以只枚举后半部,然后通过逆转得到前半部分. #include <iostream> #include < ...
- FOJ题目Problem 2082 过路费 (link cut tree边权更新)
Problem 2082 过路费 Accept: 382 Submit: 1279 Time Limit: 1000 mSec Memory Limit : 32768 KB Proble ...
- springmvc+spring+jpa(hibernate)+redis+maven配置
废话不多少 项目结构 pom.xml配置例如以下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...