Spring三级缓存

Spring三级缓存

P42
1.单例池 singletonObjects
2.第二级缓存 earlySingletonObjects
3.第三季缓存 singletonFactories

    private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(256);
    private final Map<String, ObjectFactory<?>> singletonFactories = new HashMap(16);
    private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<String, Object>(16);

在这里插入图片描述
填充service属性对应步骤在DefaultSingletonBeanRegistry.java的getSingleton方法

 @Nullable
    protected Object getSingleton(String beanName, boolean allowEarlyReference) {
         //先去一级缓存单例池中找
        Object singletonObject = this.singletonObjects.get(beanName);
        //如果以及缓存单例池没找到,并且需要的bean正在创建中(即循环依赖)并且二级缓存单例池没找到,并且允许循环引用,进入if
        if (singletonObject == null && this.isSingletonCurrentlyInCreation(beanName) && (singletonObject = this.earlySingletonObjects.get(beanName)) == null && allowEarlyReference) {
            Map<String, Object> map = this.singletonObjects;
            //双重锁检测并保证下面的put和remove操作的原子性
            synchronized (map) {
                ObjectFactory<?> singletonFactory;
                singletonObject = this.singletonObjects.get(beanName);
                //第一级缓存空并且第二级缓存空并且第三季缓存不为空
                if (singletonObject == null && (singletonObject = this.earlySingletonObjects.get(beanName)) == null && (singletonFactory = this.singletonFactories.get(beanName)) != null) {
                    singletonObject = singletonFactory.getObject();
                    //把对象放入二级缓存并且清除三级缓存内的lambbda表达式
                    this.earlySingletonObjects.put(beanName, singletonObject);
                    this.singletonFactories.remove(beanName);
                }
            }
        }
        return singletonObject;
    }

第二步或者第五步什么时候执行aop关键看earlyProxyReferences 这个map是否有值
AbstractAutoProxyCreator.java

private final Map<Object, Object> earlyProxyReferences = new ConcurrentHashMap<Object, Object>(16);
//对应第五步aop
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
       Object cacheKey;
       if (bean != null && this.earlyProxyReferences.remove(cacheKey = this.getCacheKey(bean.getClass(), beanName)) != bean) {
           return this.wrapIfNecessary(bean, beanName, cacheKey);
       }
       return bean;
   }
//对应第二步aop
 public Object getEarlyBeanReference(Object bean, String beanName) {
     Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
     this.earlyProxyReferences.put(cacheKey, bean);
     return this.wrapIfNecessary(bean, beanName, cacheKey);
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值