LruCache

记录一下LruCache put 和 get过程。

  public final V put(K key, V value) {
         if (key == null || value == null) {
            throw new NullPointerException("key == null || value == null");
        }
//用来指向map.put()后替换出来的对象。
        V previous;
        synchronized (this) {
            putCount++;
            //在原来大小上加上新增加对象的大小
            size += safeSizeOf(key, value);
            previous = map.put(key, value);
            //如果有替换出来的对象,缓存大小减去替换出对象的大小
            if (previous != null) {
                size -= safeSizeOf(key, previous);
            }
        }
        //空实现,可以按照自己的要求去实现想要的功能
        if (previous != null) {
            entryRemoved(false, key, previous, value);
        }
        //重新整理缓存对象大小
        trimToSize(maxSize);
        return previous;
    }
    public void trimToSize(int maxSize) {
    //无限循环
        while (true) {
            K key;
            V value;
            synchronized (this) {
                if (size < 0 || (map.isEmpty() && size != 0)) {
                    throw new IllegalStateException(getClass().getName()
                            + ".sizeOf() is reporting inconsistent results!");
                }
                //循环跳出条件 map是空的或者 目前缓存大小<=规定的最大数。
                if (size <= maxSize || map.isEmpty()) {
                    break;
                }
            //如果不满足条件,执行移除操作,起始位置遍历map,不断变化size大小,知道满足跳出循环
                Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
                key = toEvict.getKey();
                value = toEvict.getValue();
                map.remove(key);
                size -= safeSizeOf(key, value);
                evictionCount++;
            }

            entryRemoved(true, key, value, null);
        }
    }

get 过程

 public final V get(K key) {
        if (key == null) {
            throw new NullPointerException("key == null");
        }

        V mapValue;
        synchronized (this) {
            mapValue = map.get(key);
            if (mapValue != null) {
                hitCount++;
                //返回查找到的对象
                return mapValue;
            }
            missCount++;
        }

        /*
         * Attempt to create a value. This may take a long time, and the map
         * may be different when create() returns. If a conflicting value was
         * added to the map while create() was working, we leave that value in
         * the map and release the created value.
         */

        //默认返回null。正常情况下到此就结束了。没有查找到值 就返回null.
        V createdValue = create(key);
        if (createdValue == null) {
            return null;
        }
        //如果重写了creatValue。返回值不是null,会进入到下面方法
        synchronized (this) {
            createCount++;
            //保存自己的创建的value
            mapValue = map.put(key, createdValue);

            if (mapValue != null) {
                // There was a conflict so undo that last put
                // 又把替换出来的mapValue put进去。
                map.put(key, mapValue);
            } else {
            //之前没有存储过,改变缓存的大小
                size += safeSizeOf(key, createdValue);
            }
        }

        if (mapValue != null) {
        //把之前存进去的默认值取出来返回。
            entryRemoved(false, key, createdValue, mapValue);
            return mapValue;
        } else {
            //如果mapValue 是null,说明之前没有put过creatValue,
            //现在put进去后,要重新整理缓存大小。
            trimToSize(maxSize);
            return createdValue;
        }
    }
//默认返回null
 protected V create(K key) {
        return null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值