栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java资源缓存 之 LruCache

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java资源缓存 之 LruCache

例如对 网络加载图片进行缓存 :

  // 得到 应用程序 被分配的最大的内存
    int maxMemory=(int) Runtime.getRuntime().maxMemory();
    // 取处内存的 1/5 用来当 缓存 大小
    int cachSize=maxMemory/5;
    // 实例化 LruCache
    lruCache=new lruCache(cachSize){
      //内部方法sizeOf设置每一张图片的缓存大小
      protected int sizeOf(String key, Bitmap value) {
 //在每次存入缓存时调用,告诉系统这张缓存图片有多大
 // 相当于 为每次 要缓存的 资源 分配 大小空间
 return value.getByteCount();
      }
    };
     

上面的 代码 一般 放在初始化的 方法 里面

其实 可以将 LurCache 类 理解 为 Map 类 map 有 put和 get 方法

接下去就调用put 和 get 方法 进行需要缓存资源的存取

LurCache 的 add :

public void putBitmapToCache(String url,Bitmap bitmap){
    if (getBitmapfromCache(url)==null) {//判断当前缓存是否存在
      lruCache.put(url, bitmap);
    }
  }
LurCache 的 get:

public Bitmap getBitmapfromCache(String url){
    return lruCache.get(url);//可将lruCache看成map
  }

调用上面的 add 和 get 方法 就可以对资源进行缓存的 ,还是挺简单的,

但要注意一点 LruCache lruCache=new LruCache(cachSize) 只能被new 一次 ,不然不同对象就不同的缓存了

附上Android的Lrucache类

package android.util; 
 
import java.util.linkedHashMap; 
import java.util.Map; 
 
 
public class LruCache { 
  private final linkedHashMap map; 
 
   
  private int size; //已经存储的大小
  private int maxSize; //规定的最大存储空间
 
  private int putCount; //put的次数
  private int createCount; //create的次数
  private int evictionCount; //回收的次数
  private int hitCount; //命中的次数
  private int missCount; //丢失的次数
 
   
  public LruCache(int maxSize) { 
    if (maxSize <= 0) { 
      throw new IllegalArgumentException("maxSize <= 0"); 
    } 
    this.maxSize = maxSize; 
    this.map = new linkedHashMap(0, 0.75f, true); 
  } 
 
   
  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++; //丢失
    } 
 
     
 
    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 
 //如果前面存在oldValue,那么撤销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; 
    } 
  } 
 
   
  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); 
      if (previous != null) { //返回的先前的value值
 size -= safeSizeOf(key, previous); 
      } 
    } 
 
    if (previous != null) { 
      entryRemoved(false, key, previous, value); 
    } 
 
    trimToSize(maxSize); 
    return previous; 
  } 
 
   
  private 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) { 
   break; 
 } 
 
 Map.Entry toEvict = map.eldest(); 
 if (toEvict == null) { 
   break; 
 } 
 
 key = toEvict.getKey(); 
 value = toEvict.getValue(); 
 map.remove(key); 
 size -= safeSizeOf(key, value); 
 evictionCount++; 
      } 
 
      entryRemoved(true, key, value, null); 
    } 
  } 
 
   
  public final V remove(K key) { 
    if (key == null) { 
      throw new NullPointerException("key == null"); 
    } 
 
    V previous; 
    synchronized (this) { 
      previous = map.remove(key); 
      if (previous != null) { 
 size -= safeSizeOf(key, previous); 
      } 
    } 
 
    if (previous != null) { 
      entryRemoved(false, key, previous, null); 
    } 
 
    return previous; 
  } 
 
   
  protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {} 
 
   
  protected V create(K key) { 
    return null; 
  } 
 
  private int safeSizeOf(K key, V value) { 
    int result = sizeOf(key, value); 
    if (result < 0) { 
      throw new IllegalStateException("Negative size: " + key + "=" + value); 
    } 
    return result; 
  } 
 
   
  protected int sizeOf(K key, V value) { 
    return 1; 
  } 
 
   
  public final void evictAll() { 
    trimToSize(-1); // -1 will evict 0-sized elements 
  } 
 
   
  public synchronized final int size() { 
    return size; 
  } 
 
   
  public synchronized final int maxSize() { 
    return maxSize; 
  } 
 
   
  public synchronized final int hitCount() { 
    return hitCount; 
  } 
 
   
  public synchronized final int missCount() { 
    return missCount; 
  } 
 
   
  public synchronized final int createCount() { 
    return createCount; 
  } 
 
   
  public synchronized final int putCount() { 
    return putCount; 
  } 
 
   
  public synchronized final int evictionCount() { 
    return evictionCount; 
  } 
 
   
  public synchronized final Map snapshot() { 
    return new linkedHashMap(map); 
  } 
 
  @Override public synchronized final String toString() { 
    int accesses = hitCount + missCount; 
    int hitPercent = accesses != 0 ? (100 * hitCount / accesses) : 0; 
    return String.format("LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]", 
 maxSize, hitCount, missCount, hitPercent); 
  } 
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149463.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号