spring boot 整合 spring cache 简单使用
spring boot 整合 spring cache 简单使用
- spring cache简介
- 使用spring cache
spring cache简介
Spring 3.1起,提供了基于注解的对Cache的支持。Spring提供了各种xxxCache的实现。使用Spring Cache的好处:
- 基于注解,代码清爽简洁;
- 基于注解也可以实现复杂的逻辑;
- 可以对缓存进行回滚;
- Spring Cache不是具体的缓存技术,而是基于具体的缓存产品(如Guava、EhCache、Redis等)的共性进行了一层封装,但是可以通过简单的配置切换底层使用的缓存。具体的底层缓存技术究竟采用了Guava、EhCache还是Redis,只需要简单的配置就可以实现方便的切换。
使用spring cache
pom.xml中添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
application.properties 配置文件中添加 spring cache 配置:
1) spring.cache.type=redis #指定缓存类型,redis
2) spring.cache.redis.time-to-live=36000 #指定过期时间,毫秒
3) spring.cache.redis.key-prefix=CACHE_ #指定缓存前缀
4) spring.cache.redis.use-key-prefix=true #指定缓存前缀是否生效
5) spring.cache.redis.cache-null-values=true #开启缓存空值。防止缓存穿透创建config配置类,MyCacheConfig
添加 @EnableConfigurationProperties(CacheProperties.class) 注解
添加 @Configuration 注解
添加 @EnableCaching 注解
@EnableConfigurationProperties(CacheProperties.class)
@Configuration
@EnableCaching
public class MyCacheConfig {@BeanRedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties){RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));CacheProperties.Redis redisProperties = cacheProperties.getRedis();if (redisProperties.getTimeToLive() != null) {config = config.entryTtl(redisProperties.getTimeToLive());}if (redisProperties.getKeyPrefix() != null) {config = config.prefixKeysWith(redisProperties.getKeyPrefix());}if (!redisProperties.isCacheNullValues()) {config = config.disableCachingNullValues();}if (!redisProperties.isUseKeyPrefix()) {config = config.disableKeyPrefix();}return config;}
}
- 缓存注解的使用
1) @Cacheable(value = {“category”}, key = “#root.method.name”) //添加缓存
需要缓存的方法上添加注解,并申明此缓存名字、指定key
//value代表缓存名字,key代表缓存中的键,key为SpEL表达式:将方法名设为key
2) @CacheEvict(value = {“category”}, key = “getLevel1categorys”) //清除缓存
需要清除缓存的方法上添加注解,并申明此缓存名字、指定key
参数:allEntries = true //批量清除, key = SpEL表达式 //主键, value = {“category”} //组名
//value代表缓存名字,key代表缓存中的键,key为要清楚的数据key
3) @Caching(evict = {
@CacheEvict(value = {“category”}, key = “‘getLevel1categorys’”),
@CacheEvict(value = {“category”}, key = “‘getCatalogJson’”)
}) //批量操作缓存
需要操作缓存的方法上添加注解
//evict 代表批量操作,{} 中 添加要操作的注解
暂时就这些,后期有用到新的,会再来更新!
spring boot 整合 spring cache 简单使用相关推荐
- Spring Boot 整合——Spring batch重试和回滚
关于版本 依赖 版本 springboot 2.4.0 spring batch 2.4.0 代码地址 因为每个例子涉及代码较多,且包含测试用例,如果都贴到文章中内容过多,所以只贴出了部分代码.全部的 ...
- spring boot整合spring security笔记
最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...
- springboot整合hibernate_峰哥说技术系列-17 .Spring Boot 整合 Spring Data JPA
今日份主题 Spring Boot 整合 Spring Data JPA JPA(Java Persistence API)是用于对象持久化的 API,是Java EE 5.0 平台标准的 ORM 规 ...
- Spring boot 整合Spring Security Jwt
记录学习Spring boot 整合Spring Security Jwt 学习参考 – 慢慢的干货 https://shimo.im/docs/OnZDwoxFFL8bnP1c/read 首先创建S ...
- 认证与授权流程与spring boot整合 spring security(1)
一 spring security 1.1 spring security的作用 Spring Security所解决的问题就是安全访问控制,而安全访问控制功能其实就是对所有进入系统的请求进行拦截 ...
- 二、何为Spring Boot整合Spring Cloud?
题语:学习方法之多思考:正向.逆向.跳跃 作者:A哥(YourBatman) wx号:fsx641385712(备注"Java群"字样) 公众号:BAT的乌托邦(ID:BAT-ut ...
- Spring Boot 整合——Spring Boot整合kafka整合
Spring Boot 整合之前的内容 项目名称 描述 地址 base-data-mybatis 整合mybatis-plus(实际上官方教程已经很多,只做了自定义插件) 未完成 base-jpa J ...
- Spring Boot整合Spring Data JPA操作数据
一. Sping Data JPA 简介 Spring Data JPA 是 Spring 基于 ORM 框架.JPA 规范的基础上封装的一套 JPA 应用框架,底层使用了 Hibernate 的 J ...
- Spring Boot整合Spring Data Redis-整合步骤
如何通过SpringBoot去整合我们的Redis,这里我们先对SpringBoot Redis做一个简单的介绍,其实SpringBoot Redis,和我们之前讲的Spring JPA都是Sprin ...
最新文章
- 把自己朝九晚五的工作自动化了,有错吗?
- win7 X64 编译ffmpeg
- 从程序员到项目经理(二十九):怎样写文档
- python初中必背语法_初中必背英语语法知识汇总
- 当猪飞起来:也谈创业初期商业模式问题
- python怎么全选_有没有一种方法可以在Python网页上模拟“全选复制粘贴”?
- airflow 進行後端大數據中ETL處理(草稿)
- scala:Scala class的构造方法与继承
- ❤️力扣线性枚举算法第二题数组中连续为一的最大个数
- HDU1829【种类并查集】
- Vue学习笔记之05-条件判断
- iOS 使用CocoaPods
- B站上这些Python和数据分析视频真香!
- 敬伟PS教程:掌握篇B07高级抠图
- java web 使用 Freemarker 导出word,zip包导出多个word
- 中国现代书画家——鞠宗霖
- 【漫步计算机系统】:发展概览Ⅲ
- Error evaluating expression ‘xxxxx != null and xxxxxx!= ’
- 【系统分析师之路】第二十章 数学与经济管理(章节重点)
- 淘宝海量数据产品的技术架构
热门文章
- DHCP接口信任(DHCP Snooping)
- 大数据面试题V2.0,641页,39w字
- 我院足球健儿在重庆市第一届“健康校园杯”大学生运动会足球比赛中载誉而归...
- 同步升压3节锂电池充电芯片带NTC-ZCC6983
- 假如时间还可以重来那你还愿意做过去一年的事吗?——年终总结
- Enriched Long-term Recurrent Convolutional Network for Facial Micro-Expression Recognition阅读笔记
- oraclevm 安装mac os x 10.8
- 中国一次性塑料食品包装容器行业发展趋势及需求前景展望报告2022-2028年版
- 操作系统 内存的分配与回收
- 易度文档管理系统-源文件存储功能