LRU是Least Recently Used 的缩写,即“最近最少使用”,说明LRU缓存算法的淘汰策略是将最近最少使用的数据移除,让出内存给最新读取的数据。
LRU缓存就是通过http请求网络上的图片文件,然后保存在缓存中。显示图片时,先从缓存中取,如果缓存中没有的话就发送请求向服务器取。
看下代码:
布局文件代码:
垂直线性布局中放置了两个按钮和一张图片。
当点击第一个下载显示按钮时,首先进行判断,如果缓存中没有就从网络上获取图片,如果缓存中有的话就优先从缓存中取就OK了;第二个按钮作用是清除缓存。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.shen.actionbar.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="showPic