Lrucache

Lrucacha,主要是get和set方法,存储的结构采用了LinkedHashMap,这种map内部实现了lru算法(Least Recently Used 近期最少使用算法)。
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. this.map = new LinkedHashMap<String, Bitmap>(00.75f, true);  

最后一个参数的解释:
true if the ordering should be done based on the last access (from least-recently accessed to most-recently accessed), and false if the ordering should be the order in which the entries were inserted.
因为可能会涉及多线程,所以在存取的时候都会加锁。而且每次set操作后都会判断当前缓存区是否已满,如果满了就清掉最少使用的图形。代码如下
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. private void trimToSize(int maxSize) {  
  2.         while (true) {  
  3.             String key;  
  4.             Bitmap value;  
  5.             synchronized (this) {  
  6.                 if (size < 0 || (map.isEmpty() && size != 0)) {  
  7.                     throw new IllegalStateException(getClass().getName()  
  8.                             + ".sizeOf() is reporting inconsistent results!");  
  9.                 }  
  10.   
  11.                 if (size <= maxSize || map.isEmpty()) {  
  12.                     break;  
  13.                 }  
  14.   
  15.                 Map.Entry<String, Bitmap> toEvict = map.entrySet().iterator()  
  16.                         .next();  
  17.                 key = toEvict.getKey();  
  18.                 value = toEvict.getValue();  
  19.                 map.remove(key);  
  20.                 size -= Utils.getBitmapBytes(value);  
  21.                 evictionCount++;  
  22.             }  
  23.         }  
  24. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值