采用LRU算法实现的话就是将最老的数据删掉。利用LRU缓存,我们能够提高系统的性能.
 
一,是它本身已经实现了按照访问顺序的存储,也就是说,最近读取的会放在最前面,最不常读取的会放在最后(当然,它也可以实现按照插入顺序存储)。
二,LinkedHashMap本身有一个方法用于判断是否需要移除最不常读取的数,但是,原始方法默认不需要移除(这是,LinkedHashMap相当于一个linkedlist),所以,我们需要override这样一个方法,使得当缓存里存放的数据个数超过规定个数后,就把最不常用的移除掉。
源码分析:
 /**
* Static library version of {@link android.util.LruCache}. Used to write apps
* that run on API levels prior to 12. When running on API level 12 or above,
* this implementation is still used; it does not try to switch to the
* framework's implementation. See the framework SDK documentation for a class
* overview.
*/
public class LruCache<K, V> {
/**缓存 map 集合,要用LinkedHashMap */
private final LinkedHashMap<K, V> map; /**缓存大小 */
private int size;
/**最大缓存大小*/
private int maxSize;
/**put的次数*/
private int putCount;
/**create的次数*/
private int createCount;
/**回收的次数*/
private int evictionCount;
/**命中的次数*/
private int hitCount;
/**丢失的次数*/
private int missCount; /**
*构造方法,maxSize最大缓存大小,初始化LinkedHashMap
*/
public LruCache(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
}
this.maxSize = maxSize;
//将LinkedHashMap的accessOrder设置为true来实现LRU
//false 插入顺序
//true 访问顺序
this.map = new LinkedHashMap<K, V>(0, 0.75f, true);
} /**
* 重新设置最大缓存大小
* @param maxSize 最大缓存大小.
*/
public void resize(int maxSize) {
if (maxSize <= 0) {
throw new IllegalArgumentException("maxSize <= 0");
} synchronized (this) {
this.maxSize = maxSize;
}
trimToSize(maxSize);
} /**
如果缓存中存在或者被创建过,返回该值,如果已经返回了,会被移动到队列头部,如果没有被缓存和创建,会被返回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++;
} /*
*如果丢失了就试图创建一个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
map.put(key, mapValue);
} else {
size += safeSizeOf(key, createdValue);
}
} if (mapValue != null) {
entryRemoved(false, key, createdValue, mapValue);
return mapValue;
} else {
//每次新加入对象都需要调用trimToSize方法看是否需要回收
trimToSize(maxSize);
return createdValue;
}
} /**
*会被移动到队列头部
* @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加上预put对象的大小
size += safeSizeOf(key, value);
previous = map.put(key, value);
if (previous != null) {
//如果之前存在键为key的对象,则size应该减去原来对象的大小
size -= safeSizeOf(key, previous);
}
} if (previous != null) {
entryRemoved(false, key, previous, value);
}
//每次新加入对象都需要调用trimToSize方法看是否需要回收
trimToSize(maxSize);
return previous;
} /**
* 删除最老的条目,直到其余条目的总数达到或低于要求的大小。
*/
public 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!");
}
//如果当前size小于maxSize或者map没有任何对象,则结束循环
if (size <= maxSize || map.isEmpty()) {
break;
}
//移除链表头部的元素,并进入下一次循环
Map.Entry<K, V> toEvict = map.entrySet().iterator().next();
key = toEvict.getKey();
value = toEvict.getValue();
map.remove(key);
size -= safeSizeOf(key, value);
//回收次数+1
evictionCount++;
} entryRemoved(true, key, value, null);
}
} /**
*从内存缓存中根据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.
*
* <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.
* 当某Item丢失时会调用到,返回计算的相应的value或者null
*/
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.
*
* <p>An entry's size must not change while it is in the cache.
*这个方法要特别注意,跟我们实例化LruCache的maxSize要呼应,怎么做到呼应呢,比如maxSize的大小为缓存
*的个数,这里就是return 1就ok,如果是内存的大小,如果5M,这个就不能是个数了,就需要覆盖这个方法,返回每个缓存
*value的size大小,如果是Bitmap,这应该是bitmap.getByteCount();
*/
protected int sizeOf(K key, V value) {
return 1;
} /**
* Clear the cache, calling {@link #entryRemoved} on each removed entry.
* 清理缓存
*/
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.
*返回{@link#get}返回值的次数,该值为
*已存在于缓存中。
*/
public synchronized final int hitCount() {
return hitCount;
} /**
* Returns the number of times {@link #get} returned null or required a new
* value to be created.
*返回创建或者返回null的次数
*/
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.
调用put返回次数
*/
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.
* 返回缓存拷贝,排序规则为最近最多访问
*/
public synchronized final Map<K, V> snapshot() {
return new LinkedHashMap<K, V>(map);
} /**
* 返回maxSize hits misses hitRate值
* LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%
*/
@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);
}
}

实际使用:

public class BitmapCacheActivity extends Activity {
private ImageView iv_picture;
private BitmapCache<String, Bitmap> mMemoryCache;
private BitmapCache.BitmapRemovedCallBack<String> mEnteryRemovedCallBack =
new BitmapCache.BitmapRemovedCallBack<String>() {
@Override
public void onBitmapRemoved(String key) {
//处理回收bitmap前,清空相关view的bitmap操作
mMemoryCache.remove(key);
}
}; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bitmapcache);
iv_picture = findViewById(R.id.iv_picture);
// 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。
// BitmapCache通过构造函数传入缓存值,以bit为单位。
int memClass = ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
// 使用单个应用最大可用内存值的1/8作为缓存的大小。
int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new BitmapCache<>(cacheSize, mEnteryRemovedCallBack);
loadBitmap(R.mipmap.ic_launcher_round, iv_picture);
} /**
* bitmap添加到缓存中去
* @param key
* @param bitmap
*/
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
} /**
* 从缓存中获取bitmap
* @param key
* @return
*/
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
} /**
* 加载bitmap
* @param resId
* @param imageView
*/
public void loadBitmap(int resId, ImageView imageView) {
final String imageKey = String.valueOf(resId);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
//从缓存里面获取,没有设置默认的,然后在进行一步缓存操作
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageResource(R.drawable.person_image_empty);
BitmapLoadingTask task = new BitmapLoadingTask(imageView);
task.execute(resId);
}
} class BitmapLoadingTask extends AsyncTask<Integer, Void, Bitmap> {
private ImageView imageView; public BitmapLoadingTask(ImageView imageView) {
this.imageView = imageView;
} // 在后台加载图片。
@Override
protected Bitmap doInBackground(Integer... params) {
final Bitmap bitmap = decodeSampledBitmapFromResource(
getResources(), params[0], 100, 100);
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
} @Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//显示在bitmap上
imageView.setImageBitmap(bitmap);
} public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
} public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
} return inSampleSize;
}
}
}

LruCache的使用及原理的更多相关文章

  1. android基础---->LruCache的使用及原理

    在你应用程序的UI界面加载一张图片是一件很简单的事情,但是当你需要在界面上加载一大堆图片的时候,情况就变得复杂起来.Android为我们提供了LruCache,今天我们就来学习这个缓存的知识以及原理. ...

  2. android基础---->DiskLruCache的使用及原理

    DiskLruCache是谷歌推荐的用来实现硬盘缓存的类,今天我们开始对于DiskLruCache的学习.DiskLruCache的测试代码:DiskLruCache的测试代码下载.关于FidkLru ...

  3. Android常用库源码解析

    图片加载框架比较 共同优点 都对多级缓存.线程池.缓存算法做了处理 自适应程度高,根据系统性能初始化缓存配置.系统信息变更后动态调整策略.比如根据 CPU 核数确定最大并发数,根据可用内存确定内存缓存 ...

  4. LruCache算法原理及实现

    LruCache算法原理及实现 LruCache算法原理 LRU为Least Recently Used的缩写,意思也就是近期最少使用算法.LruCache将LinkedHashMap的顺序设置为LR ...

  5. 内存缓存LruCache实现原理

    自己项目中一直都是用的开源的xUtils框架,包括BitmapUtils.DbUtils.ViewUtils和HttpUtils四大模块,这四大模块都是项目中比较常用的.最近决定研究一下xUtils的 ...

  6. LruCache原理解析

    LruCache是一个泛型类,它内部采用LinkedHashMap,并以强引用的方式存储外界的缓存对象,提供get和put方法来完成缓存的获取和添加操作.当缓存满时,LruCache会移除较早的缓存对 ...

  7. LRUCache原理分析

    一.注释 LRUCache的原理,基本都在注释里面描述清楚了. /** * A cache that holds strong references to a limited number of va ...

  8. Android LruCache技术原理

    概述 记得在很早之前,我有写过一篇文章Android高效加载大图.多图解决方案,有效避免程序OOM,这篇文章是翻译自Android Doc的,其中防止多图OOM的核心解决思路就是使用LruCache技 ...

  9. Android面试收集录10 LruCache原理解析

    一.Android中的缓存策略 一般来说,缓存策略主要包含缓存的添加.获取和删除这三类操作.如何添加和获取缓存这个比较好理解,那么为什么还要删除缓存呢?这是因为不管是内存缓存还是硬盘缓存,它们的缓存大 ...

随机推荐

  1. Android LK Bootlaoder启动概览

    LK - Little kernel 1. 起源地: bootable\bootloader\lk\arch\arm (1)rule.mk $(BUILDDIR)/trustzone-test-sys ...

  2. 什么是IOC

    什么是IOC         IOC:控制反转,控制权的转移,应用程序本身不负责对象的创建和维护,而是由外部容器负责创建和维护.        DI(依赖注入)是其一种实现方式        目的:创 ...

  3. 多线程爬虫Miner

    多线程爬虫Miner 需要配置项:1.URL包含关键字.2.存储方式:DB-数据库存储;FILE-文件存储.3.爬取页面最大深度.4.下载页面线程数.5.分析页面线程数.6.存储线程数. ------ ...

  4. (NO.00003)iOS游戏简单的机器人投射游戏成形记(十)

    打开Arm.h,在其接口中添加一个新方法: -(void)armShoot; 接下来在Arm.m中实现该方法: -(void)armShoot{ CGPoint startPoint = [self ...

  5. Shell中read的常用方式

    read命令的语法: read -p "Prompt" variable1 variable2 variableN -p "Prompt": 显示提示信息(和用 ...

  6. android binder机制详解

    摘要 Binder是android中一个很重要且很复杂的概念,它在系统的整体运作中发挥着极其重要的作用,不过本文并不打算从深层次分析Binder机制,有两点原因:1是目前网上已经有2篇很好的文章了,2 ...

  7. 【Unity技巧】统一管理回调函数——观察者模式

    这次的内容有点类似设计模式里的观察者模式.但是和常规意义上的观察者模式也不是完全一致,所以各位就不要咬文嚼字啦!咦?设计模式?!不懂!没关系,说不定你以前就用过. 开场白 我们来想象一个场景.在加载一 ...

  8. OC语言(一)

    一.概述 1.基本上所有关键词@开头 2.字符串以@开头,如@"Hello" 3.基本数据类型 char int float double BOOL(YES\NO) 4.空为nil ...

  9. CCRenderBuffer初始化中的render state参数

    绘制纹理三角形的渲染状态(render state)已经被CCSprite基类设置过了,所以你可以简单的将self.renderState传递过去就可以了. 渲染状态是混合模式(blend mode) ...

  10. ARM linux常用汇编语法

    汇编语言每行的语法:     lable: instruction  ; comment 段操作: .section           格式: .section 段名 [标志]     [标志]可以 ...