android lru缓存 辅助类LruCache源码解析

来源:互联网 发布:淘宝全屏优惠券代码 编辑:程序博客网 时间:2024/05/17 08:02

android lru缓存 辅助类LruCache 使用和解析

有一段日子没有写博客了,放假的时候事情挺多的,最近又在啃android,以前写listview,或者其他图片显示过大总会发生oom的错误,找了点时间去看了android对于LruCache的支持,记录一下。

  • LruCache 缓存到底是什么,LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,LRU缓存就是使用这种原理实现,简单的说就是缓存一定量的数据,当超过设定的阈值时就把一些过期的数据删除掉,比如我们缓存100M的数据,当总数据小于100M时可以随意添加,当超过100M时就需要把新的数据添加进来,同时要把过期数据删除,以确保我们最大缓存100M。
  • 听了lrucache的作用,是不是很适合作为一种内存管理的方法呢

构造方法

 /**     * @param maxSize for caches that do not override {@link #sizeOf}, this is     *     the maximum number of entries in the cache. For all other caches,     *     this is the maximum sum of the sizes of the entries in this cache.     */    public LruCache(int maxSize) {        if (maxSize <= 0) {            throw new IllegalArgumentException("maxSize <= 0");        }        this.maxSize = maxSize;        this.map = new LinkedHashMap<K, V>(0, 0.75f, true);    }

可以看到构造方法中设置了最大的size同时创建了一个LinkedHashMap。

put方法

/**     * Caches {@code value} for {@code key}. The value is moved to the head of     * the queue.     *     * @return the previous value mapped by {@code key}.     */    public final V put(K key, V value) {        if (key == null || value == null) {            throw new NullPointerException("key == null || value == null");        }        V previous;        synchronized (this) {            putCount++;            size += safeSizeOf(key, value);   //加上长度            previous = map.put(key, value);   //如果已经存在返回v            if (previous != null) {           //如果已经存在减掉已经加上的长度                size -= safeSizeOf(key, previous);            }        }        if (previous != null) {             // 方法通知旧数据被更新为新的值            entryRemoved(false, key, previous, value);        }        trimToSize(maxSize);                       return previous;    }

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.         */        V createdValue = create(key);        if (createdValue == null) {            return null;        }        synchronized (this) {            createCount++;            mapValue = map.put(key, createdValue);            if (mapValue != null) {                // There was a conflict so undo that last put                map.put(key, mapValue);            } else {                size += safeSizeOf(key, createdValue);            }        }        if (mapValue != null) {            entryRemoved(false, key, createdValue, mapValue);            return mapValue;        } else {            trimToSize(maxSize);            return createdValue;        }    }

可以看到get方法传入了key进行查找,如果找到就返回,同时默认返回空的creat方法,当creat被重写后,creat可能会耗时,同时createdValue可能会改变,同时可能会有put方法将新的同样的key的方法添加进去,所以后面会看到和put方法一样的操作。

trimToSize方法

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!");                }                if (size <= maxSize || map.isEmpty()) {                    break;                }                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);        }    }

同时trimToSize方法将会对溢出情况进行判断,移除最少使用的部分。

原创粉丝点击