现在的位置: 首页 > 综合 > 正文

LruCache

2014年01月13日 ⁄ 综合 ⁄ 共 4133字 ⁄ 字号 评论关闭
  1. package android.util;  
  2.   
  3. import java.util.LinkedHashMap;  
  4. import java.util.Map;  
  5.   
  6. /** 
  7.  * A cache that holds strong references to a limited number of values. Each time 
  8.  * a value is accessed, it is moved to the head of a queue. When a value is 
  9.  * added to a full cache, the value at the end of that queue is evicted and may 
  10.  * become eligible for garbage collection. 
  11.  * Cache保存一个强引用来限制内容数量,每当Item被访问的时候,此Item就会移动到队列的头部。
  12.  * 当cache已满的时候加入新的item时,在队列尾部的item会被回收。
  13.  * <p>If your cached values hold resources that need to be explicitly released, 
  14.  * override {@link #entryRemoved}. 
  15.  * 如果你cache的某个值需要明确释放,重写entryRemoved()
  16.  * <p>If a cache miss should be computed on demand for the corresponding keys, 
  17.  * override {@link #create}. This simplifies the calling code, allowing it to 
  18.  * assume a value will always be returned, even when there's a cache miss. 
  19.  * 如果key相对应的item丢掉啦,重写create().这简化了调用代码,即使丢失了也总会返回。
  20.  * <p>By default, the cache size is measured in the number of entries. Override 
  21.  * {@link #sizeOf} to size the cache in different units. For example, this cache 
  22.  * is limited to 4MiB of bitmaps: 默认cache大小是测量的item的数量,重写sizeof计算不同item的
  23.  *  大小。
  24.  * <pre>   {@code 
  25.  *   int cacheSize = 4 * 1024 * 1024; // 4MiB 
  26.  *   LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) { 
  27.  *       protected int sizeOf(String key, Bitmap value) { 
  28.  *           return value.getByteCount(); 
  29.  *       } 
  30.  *   }}</pre> 
  31.  * 
  32.  * <p>This class is thread-safe. Perform multiple cache operations atomically by 
  33.  * synchronizing on the cache: <pre>   {@code 
  34.  *   synchronized (cache) { 
  35.  *     if (cache.get(key) == null) { 
  36.  *         cache.put(key, value); 
  37.  *     } 
  38.  *   }}</pre> 
  39.  * 
  40.  * <p>This class does not allow null to be used as a key or value. A return 
  41.  * value of null from {@link #get}, {@link #put} or {@link #remove} is 
  42.  * unambiguous: the key was not in the cache.
  43.  * 不允许key或者value为null
  44.  *  当get(),put(),remove()返回值为null时,key相应的项不在cache中
  45.  */  
  46. public class LruCache<K, V> {  
  47.     private final LinkedHashMap<K, V> map;  
  48.   
  49.     /** Size of this cache in units. Not necessarily the number of elements. */  
  50.     private int size;
    //已经存储的大小
  51.     private int maxSize;
    //规定的最大存储空间
  52.   
  53.     private int putCount;
     //put的次数
  54.     private int createCount;
     //create的次数
  55.     private int evictionCount;
     //回收的次数
  56.     private int hitCount;
     //命中的次数
  57.     private int missCount;
     //丢失的次数
  58.   
  59.     /** 
  60.      * @param maxSize for caches that do not override {@link #sizeOf}, this is 
  61.      *     the maximum number of entries in the cache. For all other caches, 
  62.      *     this is the maximum sum of the sizes of the entries in this cache. 
  63.      */  
  64.     public LruCache(int maxSize) {  
  65.         if (maxSize <= 0) {  
  66.             throw new IllegalArgumentException("maxSize <= 0");  
  67.         }  
  68.         this.maxSize = maxSize;  
  69.         this.map = new LinkedHashMap<K, V>(00.75f, true);  
  70.     }  
  71.   
  72.     /** 
  73.      * Returns the value for {@code key} if it exists in the cache or can be 
  74.      * created by {@code #create}. If a value was returned, it is moved to the 
  75.      * head of the queue. This returns null if a value is not cached and cannot 
  76.      * be created. 通过key返回相应的item,或者创建返回相应的item。相应的item会移动到队列的头部,
  77.      * 如果item的value没有被cache或者不能被创建,则返回null。
  78.      */  
  79.     public final V get(K key) {  
  80.         if (key == null) {  
  81.             throw new NullPointerException("key == null");  
  82.         }  
  83.   
  84.         V mapValue;  
  85.         synchronized (this) {  
  86.             mapValue = map.get(key);  
  87.             if (mapValue != null) {  
  88.                 hitCount++;  //命中
  89.                 return mapValue;  
  90.             }  
  91.             missCount++;  //丢失
  92.         }  
  93.   
  94.         /* 
  95.          * Attempt to create a value. This may take a long time, and the map 
  96.          * may be different when create() returns. If a conflicting value was 
  97.          * added to the map while create() was working, we leave that value in 
  98.          * the map and release the created value. 
  99.          * 如果丢失了就试图创建一个item
  100.          */  
  101.   
  102.         V createdValue = create(key);  
  103.         if (createdValue == null) {  
  104.             return null;  
  105.         }  
  106.   
  107.         synchronized (this) {  
  108.             createCount++;//创建++  
  109.             mapValue = map.put(key, createdValue);  

抱歉!评论已关闭.