源码: /frameworks/base/core/java/android/util/LruCache.java

文件开篇注释如下:

A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.

含义是:

一个只对有限个value保持强引用的Cache.

每次访问一个value, 它都会被移到队头.

当一个Cache满了之后, 如果再向它添加元素, 队尾的元素将会被回收.

<p>If your cached values hold resources that need to be explicitly released, override {@link #entryRemoved}.

如果有一个元素需要被显式的释放, 重写entryRemoved方法

protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}

<p>If a cache miss should be computed on demand for the corresponding keys,override {@link #create}.
This simplifies the calling code, allowing it to assume a value will always be returned, even when there's a cache miss.

对这段理解的一直不是很好, 读完源码以后再说

    protected V create(K key) {
return null;
}
<p>This class does not allow null to be used as a key or value.
A return value of null from {@link #get}, {@link #put} or {@link #remove} is unambiguous: the key was not in the cache.

这个类不允许Key 和 Value 是 null. 如果 get, put, remove中出现了null, 那么将会返回 该Cache中不存在这个key

关键代码:

public class LruCache<K, V> {
private final LinkedHashMap<K, V> map; /** Size of this cache in units. Not necessarily the number of elements. */
private int size;
private int maxSize; private int putCount;
private int createCount;
private int evictionCount;
private int hitCount;
private int missCount; /**
* @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);
}
}

使用 LinkedHashMap来管理整个Cache

使用 LruCache 必须重写 sizeOf 方法, sizeOf方法用来计算 value的大小

    protected int sizeOf(K key, V value) {
return 1;
}

使用方法

        // 继承LruCache时,必须要复写sizeof方法,用于计算每个条目的大小
// cacheMemory 表示可用缓存的大小, value 也不能超出 cacheMemory
mLruCache = new LruCache<String, Bitmap>(cacheMemory) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};

Android LruCache究竟是什么的更多相关文章

  1. Android LRUCache简介

    LRU Cache数据结构的介绍可以参考前面的http://www.cnblogs.com/XP-Lee/p/3441555.html. 本文以Android LRUCache来做一个简单的介绍.我们 ...

  2. Android LruCache(Picasso内存缓存)

    Cache保存一个强引用来限制内容数量,每当Item被访问的时候,此Item就会移动到队列的头部,当cache已满的时候加入新的item时,在队列尾部的item会被回收. 如果你cache的某个值需要 ...

  3. 【转】Android LruCache源码介绍

    本文来源:转载自: http://blog.csdn.net/linghu_java/article/details/8574102 package android.util; import java ...

  4. Android LruCache技术原理

    概述 记得在很早之前,我有写过一篇文章Android高效加载大图.多图解决方案,有效避免程序OOM,这篇文章是翻译自Android Doc的,其中防止多图OOM的核心解决思路就是使用LruCache技 ...

  5. Android LRUCache

    package android.util; import java.util.LinkedHashMap; import java.util.Map; /** * A cache that holds ...

  6. Android LruCache 压缩图片 有效避免程序OOM

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9316683 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工, ...

  7. Android——LruCache源码解析

    以下针对 Android API 26 版本的源码进行分析. 在了解LruCache之前,最好对LinkedHashMap有初步的了解,LruCache的实现主要借助LinkedHashMap.Lin ...

  8. Android LruCache源码简介

    package android.util; import java.util.LinkedHashMap; import java.util.Map; /** * A cache that holds ...

  9. Android开发究竟用什么工具,Eclipse||AS

    所谓公欲善其事必先利器,那就让我们来看一下android的开发工具吧,安卓的开发工具有Eclipse和Android Studio,另外还有IntelliJ IDEA,可能很多人并不知道. 首先看一下 ...

随机推荐

  1. oracle之单行函数

     单行函数 ①.字符函数 LOWER(x):将x中的每一个单词都转换成小写 UPPER(x):将x中的每一个单词都转换成大写 INITCAP(x): 将x中的每一个单词的首字母转换成大写 CONC ...

  2. jQuery中要注意的一些函数

    has()方法 或 :has选择器 :是过滤子类含有的,并不是过滤当前选择器选择的元素或对象含有的

  3. Models

    Models Models control the data source, they are used for collecting and issuing data, this could be ...

  4. iOS 常见知识点(三):Lock

    iOS 常见知识点(一):Runtime iOS 常见知识点(二):RunLoop 锁是最常用的同步工具.一段代码段在同一个时间只能允许被有限个线程访问,比如一个线程 A 进入需要保护代码之前添加简单 ...

  5. 通过scrollerview自定义collectionview(常用于推荐关注界面)

    最近项目上要实现一个效果,先把效果图摆上来吧: 刚看到效果图的时候  我觉得很简单  用UICollectionview就可以了 但是后来发现collectionview只有两种布局方式  比较单一 ...

  6. 深入理解计算机系统第二版习题解答CSAPP 2.12

    对于下面的值,写出变量x的C语言表达式.代码应该对任何字长w≥8都能工作.我们给出当x=0x87654321以及w=32时表达式的结果,仅供参考. A.x的最低有效字节,其他位均为0.[0x00000 ...

  7. Windows2012中安装Nginx并创建为Windows服务

    安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...

  8. Tomcat服务器启动常见问题

    参考:http://www.cnblogs.com/xdp-gacl/p/3729033.html 学习web开发,需要先安装一台web服务器,然后再在web服务器中开发相应的web资源,供用户使用浏 ...

  9. centos7下载安装谷歌浏览器

    centos7安装完成结束后,发现自带的火狐浏览器不太习惯,自己还是习惯谷歌浏览器,因为是新手嘛,所以自己就各种找教程看如何下载安装谷歌浏览器,一个一个按照教程试验,终于最后试验成功了一个,亲测可用. ...

  10. Hive over HBase和Hive over HDFS性能比较分析

    http://superlxw1234.iteye.com/blog/2008274 环境配置: hadoop-2.0.0-cdh4.3.0 (4 nodes, 24G mem/node) hbase ...