缓存工作原理 &@Cacheable 运行流程|学习笔记

简介: 快速学习缓存工作原理 &@Cacheable 运行流程

开发者学堂课程【SpringBoot 快速掌握 - 高级应用:缓存工作原理&@Cacheable 运行流程】学习笔记,与课程紧密联系,让用户快速学习知识

课程地址https://developer.aliyun.com/learning/course/613/detail/9290


缓存工作原理 &@Cacheable 运行流程

原理:

1、自动配置类; CacheAutoConfiguration

2、缓存的配置类

org,springframework , boot,  autoconfigure , cache, Generic Cache  Configuration

org,springframework,boot,autoconfigure , cache]JCache[ acheConfiguration

org,springframework,boot, autoconfigure , cache. EhCacheCacheConfiguration

org,springframework,boot, autoconfigure ,cache. HazelcastCacheConfiguratio

org,springframework,boot.autoconfigure , cache. InfinispanCacheConfiguration

org,springframework,boot,autoconfigure , cache. CouchbaseCacheConfiguration

org,springframework.boot,autoconfigure , caqhe, RedisCacheConfiguration

org,springframework . boot.  autoconfigure ,cahe. CaffeineCacheConfiguration

org,springframework,boot.autoconfigure , cache. GuavaCacheConfiguration

org,springframework.boot.autoconfigure,cache.S imple  CacheConfiguration

org,springframework,boot,autoconfigure , cache, No0pCacheConfiguration |

这些缓存配置类都是有顺序的

哪个配置类默认生效: SimpleCacheConfiguration ;

直接在缓存控制台测试就可以得到

给容器中注册了一个 CacheManager :  

ConcurpentMapCacheManager

可以获取和创建 ConcurrentMapCache 类型的援存组件;他的作用将数据保存在 ConcurrentMap 中;

运行流程:(断点都打在行内)

以 @Cacheable:为例

方法运行之前,先去查询 Cache (缓存组件),按照 cacheNames 指定的名字获取;

( CacheManager 先获取相应的缓存),第一次获取缓存如果没有 Cache 组件会自动创建。

@Override

public  Collection <String>get  CacheNames (){return  Collections . unmodifiableset (this.cach=Wa。

@Override

public Cache getCache(String name){name;"emp"

Cache cache=this cacheMap. get(name);cache:null

if(cache==null&this. dynamic){

synchronized (this.cacheMap){

cache=this cacheMap. get(name);name:"emp"

if(cache==null){cache: null

cache= createConcurrentMapCache (name);

this:cacheMap. put(name, cache);

}

}

}

return cache;

去 Cache 中查找缓存的内容,使用一个 key,默认就是方法的参数;key 是按照某种策略生成的。默认是使用 KeyGenerator 生成的,默认使用 SimpleKeyGenerator 生成 key

SimpLeKeyGenerator 生成 key 的默认策略;

如果没有参数;key=new SimpleKey();

如果有一个参数:key-参数的值

如果有多个参数:key=new SimpleKey(params )

没有查到缓存就调用目标方法;

将目标方法返回的结果,放进缓存中。调用的是put

Cacheable 标注的方法执行之前先来检查缓存中有没有这个数据,默认按照参数的值作为key去查询缓存,如果没有就运行方法并将结果放入缓存中;以后再来调用就可以直接使用缓存中的数据;

第一次运行放入缓存,要从缓存中查询

核心:

使用【 CacheMandgerKConcurrentMapCacheMandger 】按照名字得到 【ConcurrentMapCache】组件

key 使用 keyGenerator 生成的,默认是 SimpleKeyGenerator

@Override

protected0bjectlookup (0bjectkey){key:2

return this. store. get(key); key:2

}

@ SuppressWarnings ("unchecked")

@Override

public<T>T get(0bjectkey, Callable<T> valueloader ){

//Try efficient lookup on the  ConcurrentHashMap  first...

ValueWrapperstoreValue =get(key);

if( storeValue !=null){

return(T) storeValue . get();

}

@param  id

@return

@Cacheable( cacheNames ={"emp"})

public Employee getEmp(Integer id){

System. out. println("查询"+id+"号员工");

Employee emp= emp1oyeeMapper . getEmpById (id);

return emp;

}

}

相关文章
|
1月前
|
存储 缓存 关系型数据库
InnoDB 引擎底层存储和缓存原理
InnoDB 引擎底层存储和缓存原理
|
1月前
|
存储 缓存 前端开发
浏览器缓存工作原理是什么?
浏览器缓存工作原理是什么?
|
1月前
|
存储 缓存 Java
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
|
1月前
|
XML 存储 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache管理器的实战开发指南(修正篇)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache管理器的实战开发指南(修正篇)
44 0
|
1月前
|
存储 XML 缓存
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南(一)
【深入浅出Spring原理及实战】「缓存Cache开发系列」带你深入分析Spring所提供的缓存Cache功能的开发实战指南
198 0
|
22天前
|
缓存 NoSQL Java
Spring Cache之本地缓存注解@Cacheable,@CachePut,@CacheEvict使用
SpringCache不支持灵活的缓存时间和集群,适合数据量小的单机服务或对一致性要求不高的场景。`@EnableCaching`启用缓存。`@Cacheable`用于缓存方法返回值,`value`指定缓存名称,`key`定义缓存键,可按SpEL编写,`unless`决定是否不缓存空值。当在类上使用时,类内所有方法都支持缓存。`@CachePut`每次执行方法后都会更新缓存,而`@CacheEvict`用于清除缓存,支持按键清除或全部清除。Spring Cache结合Redis可支持集群环境。
84 6
|
29天前
|
存储 缓存 中间件
中间件Read-Through Cache(直读缓存)策略工作原理
【5月更文挑战第11天】中间件Read-Through Cache(直读缓存)策略工作原理
21 3
|
29天前
|
缓存 数据安全/隐私保护 UED
深入了解304缓存原理:提升网站性能与加载速度
深入了解304缓存原理:提升网站性能与加载速度
|
1月前
|
存储 缓存 移动开发
html实现离线缓存(工作原理+怎么使用+应用场景)
html实现离线缓存(工作原理+怎么使用+应用场景)
20 0
|
1月前
|
存储 缓存 Java
探秘MyBatis缓存原理:Cache接口与实现类源码分析
探秘MyBatis缓存原理:Cache接口与实现类源码分析
42 2
探秘MyBatis缓存原理:Cache接口与实现类源码分析