LruCache源码解析

前言

最近项目要用到Picasso,所以就看了一下Picasso里面的源码,发现里面的内存缓存主要用的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);                   
}                                                                                                                                                                         

从这个构造方法,我们至少可以得到以下两点信息:

  • LruCache所占的内存大小是可以自定义的。
  • LruCache的底层是通过LinkedHashMap实现数据缓存的。

看到LinkedHashMap的这个构造方法,我感觉挺奇怪的,因为以前从来没有用过这个构造方法,于是我就进入LinkedHashMap类看了看这个构造有何奇特之处:

/**                                                                        
 * Constructs a new {@code LinkedHashMap} instance with the specified      
 * capacity, load factor and a flag specifying the ordering behavior.      
 *                                                                         
 * @param initialCapacity                                                  
 *            the initial capacity of this hash map.                       
 * @param loadFactor                                                       
 *            the initial load factor.                                     
 * @param accessOrder                                                      
 *            {@code true} if the ordering should be done based on the last
 *            access (from least-recently accessed to most-recently        
 *            accessed), and {@code false} if the ordering should be the   
 *            order in which the entries were inserted.                    
 * @throws IllegalArgumentException                                        
 *             when the capacity is less than zero or the load factor is   
 *             less or equal to zero.                                      
 */                                                                        
public LinkedHashMap(                                                      
        int initialCapacity, float loadFactor, boolean accessOrder) {      
    super(initialCapacity, loadFactor);                                    
    init();                                                                
    this.accessOrder = accessOrder;                                        
}                                                                          

第一个参数是LinkedHashMap的初始化大小,这个很正常,没问题。第二个参数是loadFactor,一个float值,而且LruCache传进来的是0.75f,3/4,这么怪异的数字。我的好奇心被提起来了。可以看到,initialCapacity和loadFactor这两个参数是传进了super方法,好的,我们继续进去父类看,点住super然后control+左键:

/**                                                                          
 * Constructs a new {@code HashMap} instance with the specified capacity and 
 * load factor.                                                              
 *                                                                           
 * @param capacity                                                           
 *            the initial capacity of this hash map.                         
 * @param loadFactor                                                         
 *            the initial load factor.                                       
 * @throws IllegalArgumentException                                          
 *                when the ca
  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值