Spring从3.1开始定义了org.springframework.cache.Cacheorg.springframework.cache.CacheManager接口来统一不同的缓存技术;并支持使用JCache( JSR-107)注解简化开发;

  • Cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
  • Cache接口下Spring提供了各种xxxCache的实现;如RedisCache,EhCacheCache , ConcurrentMapCache等, Cache接口如下图;

    

  • 每次调用需要缓存功能的方法时,Spring会检查检查指定参数的指定的目标方法是否已经被调用过;如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法并缓存结果后返回给用户;下次调用直接从缓存中获取;

    • 使用Spring缓存抽象时需要关注以下两点,如下图

      1.确定方法需要被缓存以及他们的缓存策略

      2.从缓存中读取之前缓存存储的数据

      

  • 常用的缓存注解及参数

    • 常用的缓存注解  

    

    • 主要参数

    

    • Cache SpEL available metadata

      

    Spring 缓存抽象有缓存自动的配置类: CacheAutoConfiguration 

    下面SpringBoot的版本为 2.0.6.RELEASE

    

    这里有个@Import的注解,用于导入ImportSelector的实现类,如下图

    

    selectImports方法用于返回导入类的名称;

    

    列出所有的CacheConfiguration

    

    yml 配置中添加如下,打印匹配的自动配置类

debug: true

  

    默认情况下只有SimpleCacheConfiguration匹配

 SimpleCacheConfiguration matched:
- Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration automatic cache type (CacheCondition)
- @ConditionalOnMissingBean (types: org.springframework.cache.CacheManager; SearchStrategy: all) did not find any beans (OnBeanCondition)

  

    SimpleCacheConfigurationg给Spring容器注册一个ConcurrentMapCacheManager的Bean

    

    ConcurrentMapCacheManager的getCache方法

private final ConcurrentMap<String, Cache> cacheMap = new ConcurrentHashMap<>(16);

  

    该方法用于获取和创建ConcurrentMapCache类型的缓存

    

    

   缓存运行流程:

     1.先进getCache方法进行缓存查询,按照cacheNames指定的名字获取

      CacheManager先获取对应的缓存,没有则创建一个对应的缓存

     2.之后进入lookup方法,使用一个key查找缓存的内容,默认key为方法的参数

   先进入lookup方法查询缓存

  

    按照某种策略生成key

    

   generateKey方法

    

  使用keyGenerator生成key

private final KeyGenerator keyGenerator;

  

  默认使用SimpleKeyGenerator

  

  方法执行完成后,会将结果缓存到ConcurrentMap

  

  

  

最新文章

  1. js(jQuery)获取时间的方法及常用时间类
  2. NGUI缓动函数
  3. 12个JQuery小贴士
  4. Oracle数据库初级学习
  5. LeetCode &quot;Minimum Height Tree&quot; !!
  6. Linux DNS 设置失败
  7. #Leet Code# Unique Path(todo)
  8. ios专题 - APP设计流程
  9. Json 的日期格式转换成DateTime
  10. vb.net转换为C#方法
  11. Redis14--jedis实现主从模式。
  12. SpringMVC详解(四)------SSM三大框架整合之登录功能实现
  13. WPF DataGridHyperlinkColumn
  14. Maven 核心原理
  15. 使用 phpstudy 搭建本地测试环境
  16. windows电脑连接蓝牙耳机的正确步骤
  17. gcc的使用简介与命令行参数说明
  18. Spark记录-SparkSQL相关学习
  19. Python操作redis系列之 列表(list) (五)
  20. ceph rgw multisite基本用法

热门文章

  1. 学习 | css3基本动画之demo篇
  2. CEO的行为风格会影响公司业绩吗?
  3. matlab数据插值
  4. C++ (C#)实现获取NX PART预览图
  5. 2020 CiGA Game Jam活动总结
  6. 基础篇:java基本数据类型
  7. IDEA文本编辑区的护眼绿豆沙色配置
  8. 【漏洞复现】S2-052 (CVE-2017-9805)
  9. 【奇淫巧技】sqlmap绕过过滤的tamper脚本分类汇总
  10. git冲突的表现