一、引入相关依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

SpringBoot检查到有redis的启动依赖,就会将默认的ConcurrentMapCacheManage换为 RedisCacheManager;默认创建的 RedisCacheManager 操作redis的时候使用的是 RedisTemplate<Object, Object>,并且默认使用jdk的序列化机制

二、使用相关@Cachexxxx:

注意:该注解特性和@Transactional一致,基于AOP,需要外界调用

SpringCache是对缓存使用的抽象,通过在已有代码中打上几个预定义的注释,就可以实现我们希望达到的缓存效果

@EnableCaching
作用:标注在Configuration类上,用于启用Cache注解

@CachePut 
作用:标注到写数据的方法上,如新增/修改方法,调用方法时会自动把符合条件的数据存入缓存

public @interface CachePut {
String[] value(); //缓存的名字,可以把数据写到多个缓存 // value -》 cacheName 二者选一,必须指定 //最终 value::key--obj
String key() default ""; //缓存key,如果不指定将使用默认的KeyGenerator生成(SimpleKey:使用方法参数)
String condition() default ""; //满足缓存条件的数据才会放入缓存,CachePut的condition只在调用方法之后判断,可以操作result
String unless() default ""; //用于否决缓存更新的,在方法执行之后,用result进行判断
}

@CacheEvict
作用:标注到移除数据的方法上,如删除方法,调用方法时会从缓存中移除符合条件的数据

public @interface CacheEvict {
String[] value(); //缓存的名字,可以从多个缓存中移除数据
String key() default "";
String condition() default ""; //满足缓存条件的数据才会从缓存中移除,condition在调用方法之前和之后都会判断
boolean allEntries() default false; //是否移除所有数据
boolean beforeInvocation() default false;//是调用方法之前移除/还是调用之后移除 ,默认是调用之后移除
}

@Cacheable
作用:标注到读取数据的方法上。如查找方法:先从缓存中读取,如果没有再调用方法获取数据,否则不执行方法体,然后把数据添加到缓存中。

@Caching
作用:定义若干组的Cache注释,用于实现多个缓存逻辑。例如用户新增成功后,添加id-->user;username--->user;email--->user到缓存,代码如下:

@Caching(
put = {
@CachePut(value = "mycache", key = "#user.id"),
@CachePut(value = "mycache2", key = "#user.username.concat(#user.email)")
},
evict = {
@CacheEvict(value = "tempcache", key = "#user.id")
}
)
public User save(User user) {
.............
}

三、 缓存条件condition和unless的执行时机:

@Cacheable中的condition是在执行方法之前用于被判断是否符合从缓存中读取,因此它无法使用返回值#result;而其unless是在执行方法之后做判断,因此它可以使用返回值#result。
@Cacheput中的condition和unless都是在执行方法之后用于被判断是否符合将结果保存到缓存中,因此它们都可以使用返回值#result。
@CacheEvict的condition由beforeInvocation的值来确定是在方法调用前还是在方法调用后执行。若beforeInvocation为true则condition在方法调用前执行;否则condition在方法调用后执行。

四、SpEL语法及可使用的上下文数据:

https://blog.csdn.net/m0_37962779/article/details/78747619

SpringBoot集成redis,使用@Cachexxxx的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  3. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  4. Windows环境下springboot集成redis的安装与使用

    一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...

  5. springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败

    提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...

  6. SpringBoot | 集成Redis

    Windows下安装: https://github.com/MicrosoftArchive/redis/releases zip下就解包到自定义目录下,msi就跟着步骤安装 进入安装目录下运行命令 ...

  7. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices

    springboot 集成redis使用redis作为缓存,会报错的问题. 错误信息: java.lang.IllegalStateException: Error processing condit ...

  8. Springboot集成Redis步骤

    Spring boot 集成Redis的步骤如下: 1.在pom.xml中配置相关的jar依赖: <!--加载spring boot redis包 --> <dependency&g ...

  9. SpringBoot集成Redis

    1.引入 spring-boot-starter-redis <dependency> <groupId>redis.clients</groupId> <a ...

  10. SpringBoot集成Redis分布式锁以及Redis缓存

    https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...

随机推荐

  1. bresenham 算法生成直线

    struct Point{ Point() { posx = 0; posy = 0; } Point(int x, int y) { posx = x; posy = y; } int posx; ...

  2. 195. Spring Boot 2.0数据库迁移:Flyway

    [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...

  3. ios7自定义返回按钮后,右滑返回功能失效解决方法

    -(void)viewWillAppear:(BOOL)animated{     [super viewWillAppear:animated];     //开启ios右滑返回     if ([ ...

  4. QTP 学习 - 参数化

  5. Idea中运行项目时出现:未结束的字符串解决方案

    一般出现这种情况是编码不一致导致 解决办法: settings>file Encodings 编码设置成一致

  6. 如何使用 Visual C# .NET 处理 Excel 事件

    事件处理概述 Visual C# .NET 使用委派处理来自组件对象模型 (COM) 服务器的事件.委派是 Microsoft Visual Studio .NET 中的一个新概念.对于 COM 事件 ...

  7. leetcode32

    class Solution { public: int longestValidParentheses(string s) { ; stack<int> st; ; i < n; ...

  8. IOS搜索框输入中文解决方案(防抖)

    class Header extends React.Component { constructor(props) { super(props); this.time = 0; // 重点在于这个th ...

  9. [原]vue中各模块的实际引用

    检查发现: 1.vue实际引用文件配置位置 alias: { 'vue$': 'vue/dist/vue.esm.js', 此位置替换了vue包内的package.json中定义的位置 } 2.vue ...

  10. tkinter窗口居中方法

    tkinter窗口居中 from tkinter import * class MyFrm(Frame): def __init__(self, master): self.root=master s ...