`
龙哥IT
  • 浏览: 253852 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Android提供的LruCache类简介

 
阅读更多

Android提供的LruCache类简介

package android.util;  
  
import java.util.LinkedHashMap;  
import java.util.Map;  
  
/** 
 * 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. 
 * Cache保存一个强引用来限制内容数量,每当Item被访问的时候,此Item就会移动到队列的头部。
 * 当cache已满的时候加入新的item时,在队列尾部的item会被回收。
 * <p>If your cached values hold resources that need to be explicitly released, 
 * override {@link #entryRemoved}. 
 * 如果你cache的某个值需要明确释放,重写entryRemoved()
 * <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. 
 * 如果key相对应的item丢掉啦,重写create().这简化了调用代码,即使丢失了也总会返回。
 * <p>By default, the cache size is measured in the number of entries. Override 
 * {@link #sizeOf} to size the cache in different units. For example, this cache 
 * is limited to 4MiB of bitmaps: 默认cache大小是测量的item的数量,重写sizeof计算不同item的
 *  大小。
 * <pre>   {@code 
 *   int cacheSize = 4 * 1024 * 1024; // 4MiB 
 *   LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) { 
 *       protected int sizeOf(String key, Bitmap value) { 
 *           return value.getByteCount(); 
 *       } 
 *   }}</pre> 
 * 
 * <p>This class is thread-safe. Perform multiple cache operations atomically by 
 * synchronizing on the cache: <pre>   {@code 
 *   synchronized (cache) { 
 *     if (cache.get(key) == null) { 
 *         cache.put(key, value); 
 *     } 
 *   }}</pre> 
 * 
 * <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时,key相应的项不在cache中
 */  
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;  //put的次数
    private int createCount;  //create的次数
    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);  
    }  
  
    /** 
     * Returns the value for {@code key} if it exists in the cache or can be 
     * created by {@code #create}. If a value was returned, it is moved to the 
     * head of the queue. This returns null if a value is not cached and cannot 
     * be created. 通过key返回相应的item,或者创建返回相应的item。相应的item会移动到队列的头部,
     * 如果item的value没有被cache或者不能被创建,则返回null。
     */  
    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++;  //丢失
        }  
  
        /* 
         * Attempt to create a value. This may take a long time, and the map 
         * may be different when create() returns. If a conflicting value was 
         * added to the map while create() was working, we leave that value in 
         * the map and release the created value. 
         * 如果丢失了就试图创建一个item
         */  
  
        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;  
        }  
    }  
  
    /** 
     * Caches {@code value} for {@code key}. The value is moved to the head of 
     * the queue. 
     * 
     * @return the previous value mapped by {@code key}. 
     */  
    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;  
    }  
  
    /** 
     * @param maxSize the maximum size of the cache before returning. May be -1 
     *     to evict even 0-sized elements. 
     *  清空cache空间
     */  
    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<K, V> 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);  
        }  
    }  
  
    /** 
     * Removes the entry for {@code key} if it exists. 
     * 删除key相应的cache项,返回相应的value
     * @return the previous value mapped by {@code key}. 
     */  
    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;  
    }  
  
    /** 
     * Called for entries that have been evicted or removed. This method is 
     * invoked when a value is evicted to make space, removed by a call to 
     * {@link #remove}, or replaced by a call to {@link #put}. The default 
     * implementation does nothing. 
     * 当item被回收或者删掉时调用。改方法当value被回收释放存储空间时被remove调用,
     * 或者替换item值时put调用,默认实现什么都没做。
     * <p>The method is called without synchronization: other threads may 
     * access the cache while this method is executing. 
     * 
     * @param evicted true if the entry is being removed to make space, false 
     *     if the removal was caused by a {@link #put} or {@link #remove}. 
     * true---为释放空间被删除;false---put或remove导致
     * @param newValue the new value for {@code key}, if it exists. If non-null, 
     *     this removal was caused by a {@link #put}. Otherwise it was caused by 
     *     an eviction or a {@link #remove}. 
     */  
    protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue) {}  
  
    /** 
     * Called after a cache miss to compute a value for the corresponding key. 
     * Returns the computed value or null if no value can be computed. The 
     * default implementation returns null. 
     * 当某Item丢失时会调用到,返回计算的相应的value或者null
     * <p>The method is called without synchronization: other threads may 
     * access the cache while this method is executing. 
     * 
     * <p>If a value for {@code key} exists in the cache when this method 
     * returns, the created value will be released with {@link #entryRemoved} 
     * and discarded. This can occur when multiple threads request the same key 
     * at the same time (causing multiple values to be created), or when one 
     * thread calls {@link #put} while another is creating a value for the same 
     * key. 
     */  
    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;  
    }  
  
    /** 
     * Returns the size of the entry for {@code key} and {@code value} in 
     * user-defined units.  The default implementation returns 1 so that size 
     * is the number of entries and max size is the maximum number of entries. 
     * 返回用户定义的item的大小,默认返回1代表item的数量,最大size就是最大item值
     * <p>An entry's size must not change while it is in the cache. 
     */  
    protected int sizeOf(K key, V value) {  
        return 1;  
    }  
  
    /** 
     * Clear the cache, calling {@link #entryRemoved} on each removed entry. 
     * 清空cacke
     */  
    public final void evictAll() {  
        trimToSize(-1); // -1 will evict 0-sized elements  
    }  
  
    /** 
     * For caches that do not override {@link #sizeOf}, this returns the number 
     * of entries in the cache. For all other caches, this returns the sum of 
     * the sizes of the entries in this cache. 
     */  
    public synchronized final int size() {  
        return size;  
    }  
  
    /** 
     * For caches that do not override {@link #sizeOf}, this returns the maximum 
     * number of entries in the cache. For all other caches, this returns the 
     * maximum sum of the sizes of the entries in this cache. 
     */  
    public synchronized final int maxSize() {  
        return maxSize;  
    }  
  
    /** 
     * Returns the number of times {@link #get} returned a value that was 
     * already present in the cache. 
     */  
    public synchronized final int hitCount() {  
        return hitCount;  
    }  
  
    /** 
     * Returns the number of times {@link #get} returned null or required a new 
     * value to be created. 
     */  
    public synchronized final int missCount() {  
        return missCount;  
    }  
  
    /** 
     * Returns the number of times {@link #create(Object)} returned a value. 
     */  
    public synchronized final int createCount() {  
        return createCount;  
    }  
  
    /** 
     * Returns the number of times {@link #put} was called. 
     */  
    public synchronized final int putCount() {  
        return putCount;  
    }  
  
    /** 
     * Returns the number of values that have been evicted. 
     * 返回被回收的数量
     */  
    public synchronized final int evictionCount() {  
        return evictionCount;  
    }  
  
    /** 
     * Returns a copy of the current contents of the cache, ordered from least 
     * recently accessed to most recently accessed. 返回当前cache的副本,从最近最少访问到最多访问
     */  
    public synchronized final Map<K, V> snapshot() {  
        return new LinkedHashMap<K, V>(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);  
    }  
} 

 

分享到:
评论

相关推荐

    Android使用LruCache缓存图片

    `LruCache`是Android SDK提供的一种基于最近最少使用(Least Recently Used)算法的内存缓存机制,常用于图片、数据等对象的缓存,以减少磁盘读取和网络加载的次数。本文将详细介绍如何在Android应用中使用`LruCache...

    android图片墙lrucache oom

    一、Android OOM简介 当应用程序请求的内存超过系统分配的最大内存时,就会发生OOM。在Android中,每个应用都有自己的Dalvik虚拟机实例,其内存限制由Android系统决定。如果图片加载不当,尤其是在滚动列表时一次性...

    Android LRUCache机制 缓存机制

    #### 一、LRUCache简介 在Android开发过程中,缓存技术是一项重要的优化手段,可以显著提升应用性能并改善用户体验。LRUCache(Least Recently Used Cache,最近最少使用缓存)是一种常用的内存缓存策略,它通过维护...

    Lrucache的相关使用(Android缓存)

    `LruCache`是Android SDK提供的一种基于最近最少使用(Least Recently Used)算法的内存缓存机制。本篇文章将深入探讨`LruCache`的原理、使用方法以及在实际应用中的注意事项。 首先,我们需要理解`LruCache`的工作...

    安卓图片加载缓存相关-AsyncTask的使用及ListView的常见优化asyncTask异步加载数据使用了LruCache优化图片加载通过滑动监听提高ListView滑动流畅度.rar

    AsyncTask的使用及ListView的常见优化 asyncTask异步加载数据 使用了LruCache优化图片加载 通过滑动监听提高ListView滑动流畅度.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习...

    LruCache Demo

    LRUCache(Least Recently Used Cache)是Android系统提供的一个基于最近最少使用算法(LRU)的内存缓存机制。在Android开发中,特别是在处理大量图片或者数据时,LRUCache可以帮助我们有效地管理内存,避免因内存...

    Android照片结合LruCache和DiskLruCache Demo源码程序

    首先,`LruCache`(Least Recently Used Cache)是Android SDK提供的一种基于最近最少使用原则的内存缓存机制。它位于`android.util`包中,用于在内存有限的情况下存储对象。当内存达到预设限制时,`LruCache`会自动...

    浅谈Android LruCache的缓存策略

    LruCache是Android 3.1引入的一个内置缓存类,适用于内存缓存。它基于LinkedHashMap实现,这是一种支持按照访问顺序或插入顺序排序的哈希映射结构。 1. LruCache介绍 LruCache使用强引用保存缓存对象,当缓存满时...

    android lrucache

    Android提供了`LruCache`类,用于实现一个高效的内存缓存机制,特别适用于图片或者其他大对象的缓存,从而避免频繁地进行内存分配和回收,提高应用程序的性能。 `LruCache`全称是"Least Recently Used Cache",即...

    LruCache工具类

    在Android开发中,LRUCache工具类是Android SDK提供的一种实现LRU缓存机制的方式,主要应用于内存级别的缓存,比如图片、数据对象等。它通过维护一个最近最少使用的数据结构,当缓存满时,会优先淘汰最久未使用的...

    Android Lrucache加载图片(AsyncTask )

    在Android中,`LruCache`是一个高效的缓存类,它继承自`LinkedHashMap`,并提供了便捷的内存计算和缓存清理功能。通过`LruCache`,开发者可以创建一个对象缓存,将频繁访问的数据存储在内存中,以减少对磁盘或网络的...

    android DiskLruCache需要的几个类

    总的来说,DiskLruCache提供了一种高效、可靠且易于管理的磁盘缓存解决方案,适用于Android应用程序中处理大量数据的场景,如图片、视频或者其他大型文件的缓存。通过理解并正确使用这些类,开发者可以优化应用性能...

    基于LruCache listView缓存图片工具类

    `LruCache`是Android SDK提供的一种内存缓存机制,它可以帮助我们优化应用程序的性能,减少对内存的消耗,提升用户体验。本文将深入探讨如何使用基于`LruCache`的图片加载工具类在ListView中实现图片缓存。 首先,`...

    android中DiskLruCache缓存类

    DiskLruCache是Android系统提供的一个基于硬盘的LRU(Least Recently Used)缓存实现,主要用于存储那些不适合在内存中长期驻留但又需要频繁访问的数据。本文将深入解析DiskLruCache的工作原理、使用方法以及与其他...

    android图片下载LruCache实现三级缓存方式

    首先,LruCache是Android SDK提供的一种基于最近最少使用原则(Least Recently Used)的内存缓存机制。LRU策略的核心思想是:当缓存空间满时,最近最少使用的数据会被优先淘汰。在图片加载场景中,LruCache可以帮助...

    LruCache缓存demo

    在Android系统中,LRUCache是Android SDK提供的一种基于LRU策略的内存缓存工具,主要用于图片、数据库记录等对象的缓存,以提高应用性能。 标题“LruCache缓存demo”指的是一个关于如何使用LRUCache进行缓存操作的...

    PhotosWallDemo 结合LruCache和DiskLruCache

    首先,LruCache(Least Recently Used Cache)是Android SDK中提供的一种基于最近最少使用原则的内存缓存机制。它的核心思想是当缓存空间满时,优先淘汰最近最少使用的数据。LruCache内部通过双向链表和哈希表实现,...

    Android的缓存技术:LruCache和DiskLruCache

    使用LruCache时,开发者需要自定义一个对象类,并实现equals()和hashCode()方法,确保对象能够正确地被缓存和查找。同时,LruCache的大小需要预先设定,通常可以通过设备内存的1/8作为参考值。 以下是如何在Android...

    AndroidStudio利用DiskLruCache和LruCache实现简单的照片墙

    AndroidStudio利用DiskLruCache和LruCache实现简单的照片墙

    详解Android的内存优化--LruCache

    其中,`LruCache` 是 Android SDK 提供的一种基于 LRU(Least Recently Used)算法实现的高效缓存机制。本文将深入探讨 `LruCache` 的概念、工作原理,并通过源码分析来揭示其内部机制。 **LruCache 概念** `...

Global site tag (gtag.js) - Google Analytics