SpringCache的简单入门(RedisCacheManager)(@Cacheable、@CachePut、@CacheEvict)
1.介绍
Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能,大大简化我们在业务中操作缓存的代码。
Spring Cache只是提供了一层抽象,底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口。
CacheManager | 描述 |
EhCacheCacheManager | 使用EhCache作为缓存技术 |
GuavaCacheManager | 使用Google的GuavaCache作为缓存技术 |
RedisCacheManager | 使用Redis作为缓存技术 |
2.注解
注解 | 说明 |
---|---|
@EnableCaching | 开启缓存注解功能,用在启动类上 |
@Cacheable | 放在查询方法上 在方法执行前Spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据; 若没有数据,调用方法并将方法返回值放到缓存中 |
@CachePut | 将方法的返回值放到缓存中 |
@CacheEvict | 将一条或多条数据从缓存中删除 |
3.案例
3.1.导入依赖
<!-- spring cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><!-- redis --><!--1.没有导入redis和spring cache之前,实现类:ConcurrentMapCacheManager2.导入以后使用的实现类:RedisCacheManager--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
这里要注意的是,当我们没有导入redis和spring cache之前
实现类:ConcurrentMapCacheManager
导入以后使用的实现类:RedisCacheManager
3.2.配置配置文件application.yml
spring:cache:redis:time-to-live: 1800000 #单位是毫秒,设置缓存过期时间,不是必须的,默认是不过期
3.3.配置存入redis时,将key和value序列化
package com.wenbo.config;import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@EnableCaching //开启缓存注解功能(也可以写在启动类中)
public class RedisConfig {@Beanpublic RedisCacheConfiguration redisCacheConfiguration() {return RedisCacheConfiguration.defaultCacheConfig().serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) // key的序列化.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer())); // value的序列化}}
配置到这,我们就可以开始使用@CachePut注解、@CacheEvict注解、@Cacheable注解
3.4.@CachePut注解的使用
作用 | 将方法返回值,放入缓存 |
value | 缓存的名称, 同义词(cacheNames) 每个缓存名称下面可以有很多key |
key | 缓存的key,支持Spring的表达式语言SPEL语法 |
3.4.1.使用样例
//CachePut:在方法执行完毕以后,将返回值放入缓存// value缓存的名称,每个缓存名称下面可以有多个key// 缓存的key, 当调用参数或者返回值当key时,可以#+变量名,(若方法为findUsers(User user), user为一个对象,可以取值如:#user.id)// result.id : #result代表方法返回值,该表达式 代表以返回对象的id属性作为key;// result.name : #result代表方法返回值,该表达式 代表以返回对象的name属性作为key;@CachePut(value = "userCache", key = "#id")public List<User> findUsers(Long id) {List<User> list = userDao.findUsers(id);return list;}
3.5.@CacheEvict注解的使用
作用 | 清理指定缓存 |
value | 缓存的名称,每个缓存名称下面可以有多个key |
key | 缓存的key,支持Spring的表达式语言SPEL语法 |
3.5.1使用样例
一般当我们进行对数据库的修改时,将缓存删除(增、删、改)
/*** CacheEvict:清理指定缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key,赋值同CachePut* @CacheEvict(value = "userCache",key = "#id") //#id 代表变量名为id的参数值* @CacheEvict(value = "userCache",key = "#p0") //#p0 代表第一个参数* @CacheEvict(value = "userCache", allEntries = true) //删除这个名字下所有的缓存,不能与key同时使用*/@CacheEvict(value = "userCache",key = "#id")public void deleteUsers(Long id) {List<User> list = userDao.deleteUsers(id);}
当我们需要删除该缓存下所有的key时,可以增添一个参数allEntries = true
3.6.@Cacheable注解的使用
作用 | 在方法执行前,先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中 |
value | 缓存的名称,每个缓存名称下面可以有多个key |
key | 缓存的key, 支持Spring的表达式语言SPEL语法 |
3.6.1.使用样例
/*** value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CachePut(value = "userCache", key = "#id")public List<User> findUsers(Long id) {List<User> list = userDao.findUsers(id);return list;}
注:@Cacheable和@CachePut的区别在于,@CachePut只是将数据存入缓存,未进行读取。
而@Cacheable在方法执行前,先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
4.总结
方法 | 注解 | 作用 |
---|---|---|
添加 | @CachePut | 将方法返回值,放入缓存 |
删除或更新 | @CacheEvict | 清理指定缓存 |
查询 | @Cacheable | 在方法执行前,先查看缓存中是否有数据, 如果有数据,则直接返回缓存数据;若没有数据, 调用方法并将方法返回值放到缓存中 |
第一次写文章,若有不足之处望大家多多指教。
SpringCache的简单入门(RedisCacheManager)(@Cacheable、@CachePut、@CacheEvict)相关推荐
- 详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
注释介绍 @Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法 参数 解释 example value 缓 ...
- cacheable更新_详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
注释介绍 @Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法 参数 解释 example value 缓 ...
- Spring缓存注解@CachePut , @CacheEvict,@CacheConfig使用
转载自https://www.cnblogs.com/OnlyCT/p/7845660.html#t1 Cacheable CachePut CacheEvict CacheConfig 开启缓存注解 ...
- 2021-10-12Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- cacheable 表达式_Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut
SpringBoot Redis缓存 @Cacheable.@CacheEvict.@CachePut
- cacheable 表达式_Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...
- 三十七、缓存注解@Cacheable、@CacheEvict、@CachePut详解
# 缓存注解@Cacheable.@CacheEvict.@CachePut详解 ##一.@Cacheable用法详解 ###1.用在哪里? 用在方法或者类上. ###2.这两种 ...
最新文章
- python实现单例_Python 实现单例模式
- 强烈安利!这个私藏已久的神器!
- 15-数据结构探险系列-图篇
- Element-ui学习笔记3--Form表单(二)
- ISA2006之工作组环境下的阵列部署
- 业务基础【概念说明】-userAgent
- php代码怎么复制_PHP_PHP网站备份程序代码分享,效果图:PHP代码 复制代码 代码 - phpStudy...
- Chinese savior crepe
- 使用Oracle LogMiner分析archived log
- 《vSphere性能设计:性能密集场景下CPU、内存、存储及网络的最佳设计实践》一1.1.1 确定参数...
- python和rpa_什么是RPA
- 手机软件项目管理(1)—软件供应商评判项
- STM32工作笔记004---了解高速版PCB设计Cadence
- 16位汇编第一讲简介
- 计算机网络防护,计算机网络防护方法与流程
- 【问题解决】D:\Users10476\AppData\Local\Programs\Microsoft vsCode\unins000.exe 尝试在目标目录创建文件时发生一个错误:拒绝访问。.
- 计算机组装维护理论知识大全,计算机组装与维护知识汇总.docx
- java 常用的时间相关转化
- 小马哥讲Spring核心编程思想 - 第二章 重新认识IoC
- windows_删除多余网络适配器(删除/卸载多余网卡)/删除TAP虚拟网卡NIC