首页 > 编程语言 > 详细

SpringCache

时间:2020-09-24 16:03:10      阅读:41      评论:0      收藏:0      [点我收藏+]

Spring 3.1 开始  官方文档:https://docs.spring.io/spring-framework/docs/5.1.18.RELEASE/spring-framework-reference/integration.html#cache

Cache & CacheManager

SpringCache整合,简化缓存开发

  1. 引入依赖  spring-boot-starter-cache、spring-boot-starter-data-redis
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-cache -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

  2.自动配置

   CacheAutoConfiguration会导入 RedisCacheConfiguration

   自动配好了缓存管理器RedismanagerCache

  3.手动配置

spring.cache.type=redis

  4.测试使用

  @Cacheable  : 触发将数据保存到缓存的操作

  @CacheEvict : 触发将数据从缓存中删除的操作

  @CachePut :  不影响方法执行,更新缓存

  @Caching :  组合以上多个操作

  @CacheConfig :在类级别共享缓存的相同配置

  1. 开启缓存功能@Enablecaching
  2. 方法上添加@Cacheable    如果缓存中有,方法不用调用。如果缓存中没有,会调用方法,最后将方法的结果放入缓存

    【缓存的分区(按照业务类型分)】 @Cacheable("category")

   3.默认行为:

  • 如果缓存中有,方法不会调用
  • key默认自动生成,缓存的名字::Simplekey[]{自动生成key值}
  • 缓存的value值,默认使用jdk序列化机制,将序列化后的数据存到redis
  • 默认ttl时间:-1

  4.自定义行为

  • 指定生成的缓存使用的key,key属性制定,接收一个Spel
  • 指定缓存的存活时间,配置文件中修改
  • 将数据保存为json模式

  

SpringCache

原文:https://www.cnblogs.com/sgrslimJ/p/13723571.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!