@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的几个缓存相关注解的更多相关文章

  1. 【玩转SpringBoot】用好条件相关注解,开启自动配置之门

    自动配置隐含两层含义,要搞清楚 上帝让程序员的发量减少,是为了让他变得更聪明,如果有一天聪明到了极点,那就是绝顶聪明. 据说在大脑高速运转下,这样更有利于散热,不至于核心温度过高而产生告警. 聪明的大 ...

  2. 【SpringBoot实战】核心配置和注解

    前言 SpringBoot核心配置在springboot中有非常重要的作用,我们可是使用核心配置文件进行一些基础功能的定义,属性值的注入等.springboot支持两种格式的核心配置文件,一种是pro ...

  3. springBoot注解大全JPA注解springMVC相关注解全局异常处理

    https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...

  4. springboot 2 集成 redis 缓存 序列化

    springboot 缓存 为了实现是在数据中查询数据还是在缓存中查询数据,在application.yml 中将mybatis 对应的mapper 包日志设置为debug . spring: dat ...

  5. SpringBoot集成Redis实现缓存处理(Spring AOP实现)

    第一章 需求分析 计划在Team的开源项目里加入Redis实现缓存处理,因为业务功能已经实现了一部分,通过写Redis工具类,然后引用,改动量较大,而且不可以实现解耦合,所以想到了Spring框架的A ...

  6. SpringBoot 使用 EhCache2.x 缓存(三十一)

    SpringBoot 使用 EhCache2.x 缓存入门很简单,废话少说上干货: 1.在POM.xml中增加jar包 <!--开启 cache 缓存--> <dependency& ...

  7. springboot redis-cache 自动刷新缓存

    这篇文章是对上一篇 spring-data-redis-cache 的使用 的一个补充,上文说到 spring-data-redis-cache 虽然比较强悍,但还是有些不足的,它是一个通用的解决方案 ...

  8. 在springboot中使用redis缓存,将缓存序列化为json格式的数据

    背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...

  9. springboot系列:使用缓存

    前言:springboot已经为我们实现了抽象的api接口,因此当我们使用不同的缓存时,只是配置有可能有点区别(比如ehcache和Redis),但是在程序中使用缓存的方法是一样的. 1.spring ...

随机推荐

  1. VirtualBox安装kali linux过程及安装后无法全屏问题解决方法(2)

    ? 1   安装说完了,现在来看看怎么全屏吧,虚拟机无法全屏跟咸鱼有什么区别... 首先打开vbox,选择设备(Device)选项里面最下面安装增强工具那个选项(insert guest additi ...

  2. Guava工具类

    原文链接:http://blog.csdn.net/mnmlist/article/details/53425865 Objects类 Objects类有几个比较不错的方法,toString.hash ...

  3. 使用postman 测试restful接口

    前提: 在chrome网上应用商店安装postman 和 Postman Interceptor. 如下图: 使用postman的时候,最后开启postman Interceptor,如下图: 然后启 ...

  4. HDU 5078 Revenge of LIS II(dp LIS)

    Problem Description In computer science, the longest increasing subsequence problem is to find a sub ...

  5. java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlException

    http://blog.csdn.net/you23hai45/article/details/70197502

  6. Codeforces Round #272 (Div. 2) 题解

    Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs time limit per test 1 second memory limit per ...

  7. 【POJ 2182】Lost Cows

    [题目链接] http://poj.org/problem?id=2182 [算法] 树状数组 + 二分 [代码] #include <algorithm> #include <bi ...

  8. ROS-参数

    前言:参数的用法. 一.参数常用命令 命令 功能 rosparam list 参数列表 rosparam get 获取参数 rosparam set  设置参数 rosparam load 加载参数 ...

  9. 案例分析:大数据平台技术方案及案例(ppt)

    大数据平台是为了计算,现今社会所产生的越来越大的数据量,以存储.运算.展现作为目的的平台.大数据技术是指从各种各样类型的数据中,快速获得有价值信息的能力.适用于大数据的技术,包括大规模并行处理(MPP ...

  10. c# CacheHelper缓存帮助类

    一.开篇 主要功能:改善程序性能.服务器的响应速度,尤其是当数据的处理过程变得复杂以及访问量变大时,变得比较明显.有些数据并非时刻在发生变化,如果我们可以将一些变化不频繁的数据的最终计算结果(包括页面 ...