LruCache

来源:互联网 发布:玩客云抢购软件3.0 编辑:程序博客网 时间:2024/06/11 14:01

记录一下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;    }
原创粉丝点击