结合使用 LRU 图像缓存和 HTTPResponseCache 进行磁盘和内存缓存
Use LRU Image Caching In Conjuction With HTTPResponseCache for Disk and Memory Caching
最初 objective 是同时使用磁盘和内存缓存。这需要实现 LRU 缓存和 DiskLruCache。但是,由于 HTTPResponse 缓存使用磁盘 space,我选择使用 LRU 缓存并执行 con.setUseCaches(true);
问题是我不太明白先实现什么。
对于LRU和DiskLru缓存,算法是这样的:
即
首先检查图像的内存缓存
如果有图像,return 它并更新缓存
否则检查磁盘缓存
如果磁盘缓存有图像,return 它并更新缓存
否则从互联网下载图像,return它并更新缓存
现在使用以下代码:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private int inSampleSize = 0;
private String imageUrl;
private BaseAdapter adapter;
private ImagesCache cache;
private int desiredWidth, desiredHeight;
private Bitmap image = null;
private ImageView ivImageView;
Context mContext;
public DownloadImageTask(BaseAdapter adapter, int desiredWidth, int desiredHeight) {
this.adapter = adapter;
this.cache = ImagesCache.getInstance();
this.desiredWidth = desiredWidth;
this.desiredHeight = desiredHeight;
}
public DownloadImageTask(Context mContext, ImagesCache cache, ImageView ivImageView, int desireWidth, int desireHeight) {
this.cache = cache;
this.ivImageView = ivImageView;
this.desiredHeight = desireHeight;
this.desiredWidth = desireWidth;
this.mContext = mContext;
}
@Override
protected Bitmap doInBackground(String... params) {
imageUrl = params[0];
return getImage(imageUrl);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result != null) {
cache.addImageToWarehouse(imageUrl, result);
if (ivImageView != null) {
ivImageView.setImageBitmap(result);
}
else {
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
/*
* IMPORTANT:
* This enables your retrieval from the cache when working offline
*/
/***
* Force buffered operations to the filesystem. This ensures that responses
* written to the cache will be available the next time the cache is opened,
* even if this process is killed.
*/
HttpResponseCache cache = HttpResponseCache.getInstalled();
if(cache != null) {
//the number of HTTP requests issued since this cache was created.
Log.e("total num HTTP requests", String.valueOf(cache.getHitCount()));
//the number of those requests that required network use.
Log.e("num req network", String.valueOf(cache.getNetworkCount()));
//the number of those requests whose responses were served by the cache.
Log.e("num use cache", String.valueOf(cache.getHitCount()));
// If cache is present, flush it to the filesystem.
// Will be used when activity starts again.
cache.flush();
/***
* Uninstalls the cache and releases any active resources. Stored contents
* will remain on the filesystem.
*/
//UNCOMMENTING THIS PRODUCES A java.lang.IllegalStateException: cache is closedtry {
// cache.close();
//}
//catch(IOException e){
// e.printStackTrace();
//}
}
}
private Bitmap getImage(String imageUrl) {
if (cache.getImageFromWarehouse(imageUrl) == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = inSampleSize;
//installing the cache at application startup.
try {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache == null) {
File httpCacheDir = new File(mContext.getCacheDir(), "http");
long HTTP_CACHE_SIZE_IN_BYTES = 1024 * 1024 * 1024; // 1GB
HttpResponseCache.install(httpCacheDir, HTTP_CACHE_SIZE_IN_BYTES);
//Log.e("Max DiskLRUCache Size", String.valueOf(DiskLruCache.getMaxSize());
}
}
catch (IOException e) {
e.printStackTrace();
}
try {
int readTimeout = 300000;
int connectTimeout = 300000;
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
InputStream stream = connection.getInputStream();
image = BitmapFactory.decodeStream(stream, null, options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
if (imageWidth > desiredWidth || imageHeight > desiredHeight) {
System.out.println("imageWidth:" + imageWidth + ", imageHeight:" + imageHeight);
inSampleSize = inSampleSize + 2;
getImage(imageUrl);
}
else {
options.inJustDecodeBounds = false;
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.addRequestProperty("Cache-Control", "only-if-cached");
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
stream = connection.getInputStream();
image = BitmapFactory.decodeStream(stream, null, options);
return image;
}
}
catch (Exception e) {
Log.e("getImage", e.toString());
}
}
return image;
}
}
似乎在 doInBackground() 中我将保存到 HttpResponseCache,而在 onPostExecute() 中我将相同的图像保存到 LRUCache。我想做的是:
如果图片没有缓存,保存到HttpResponseCache
如果 HttpResponseCache 已满,则保存到 LRUCache。
如果两者都已满,请使用默认删除机制删除旧的未使用图像。
问题是保存到两个缓存而不是任何一个
编辑重述问题
**
由于两者都缓存相同的图像,两次并将缓存保存到同一文件系统,也许问题应该是使用哪个,因为似乎没有必要同时使用两者...
**
如果有人想要 re-use 上述任何代码,我将只采用 http 响应缓存而不使用 LRU 缓存,因为特别是如果您正在缓存 web 服务响应,例如JSON、xml。为什么?
由于上面的 LRU 缓存实现,我曾丢失 200MB 的设备存储空间。
HTTPResponseCache 的优点:
- 将 HTTP 和 HTTPS 响应缓存到文件系统,以便它们可以
重用,节省时间和带宽
- HttpUrlConnection 做:自动处理缓存机制,
- 借助于加快应用程序的响应时间
HttpResponseCache
- 它从 API 级别 1 开始可用 => 它经受住了时间的考验并且坚固耐用
另一方面:
虽然 LRUCache 相对于 DiskLRUCache 有其优势:
- 您必须实施 class(和其他帮助程序 classes),这意味着如果 android 开发人员的代码发生变化,您将不得不不断下载和编辑您的本地在您的应用程序中断后的版本将被弃用。
- 删除图像后,您可能仍会发现您的磁盘 space 正在使用,因为图像将在您的设备中的某个位置(就像我的情况一样)。
这就是结论...
最初 objective 是同时使用磁盘和内存缓存。这需要实现 LRU 缓存和 DiskLruCache。但是,由于 HTTPResponse 缓存使用磁盘 space,我选择使用 LRU 缓存并执行 con.setUseCaches(true);
问题是我不太明白先实现什么。 对于LRU和DiskLru缓存,算法是这样的:
即
首先检查图像的内存缓存
如果有图像,return 它并更新缓存
否则检查磁盘缓存
如果磁盘缓存有图像,return 它并更新缓存
否则从互联网下载图像,return它并更新缓存
现在使用以下代码:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
private int inSampleSize = 0;
private String imageUrl;
private BaseAdapter adapter;
private ImagesCache cache;
private int desiredWidth, desiredHeight;
private Bitmap image = null;
private ImageView ivImageView;
Context mContext;
public DownloadImageTask(BaseAdapter adapter, int desiredWidth, int desiredHeight) {
this.adapter = adapter;
this.cache = ImagesCache.getInstance();
this.desiredWidth = desiredWidth;
this.desiredHeight = desiredHeight;
}
public DownloadImageTask(Context mContext, ImagesCache cache, ImageView ivImageView, int desireWidth, int desireHeight) {
this.cache = cache;
this.ivImageView = ivImageView;
this.desiredHeight = desireHeight;
this.desiredWidth = desireWidth;
this.mContext = mContext;
}
@Override
protected Bitmap doInBackground(String... params) {
imageUrl = params[0];
return getImage(imageUrl);
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (result != null) {
cache.addImageToWarehouse(imageUrl, result);
if (ivImageView != null) {
ivImageView.setImageBitmap(result);
}
else {
}
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
/*
* IMPORTANT:
* This enables your retrieval from the cache when working offline
*/
/***
* Force buffered operations to the filesystem. This ensures that responses
* written to the cache will be available the next time the cache is opened,
* even if this process is killed.
*/
HttpResponseCache cache = HttpResponseCache.getInstalled();
if(cache != null) {
//the number of HTTP requests issued since this cache was created.
Log.e("total num HTTP requests", String.valueOf(cache.getHitCount()));
//the number of those requests that required network use.
Log.e("num req network", String.valueOf(cache.getNetworkCount()));
//the number of those requests whose responses were served by the cache.
Log.e("num use cache", String.valueOf(cache.getHitCount()));
// If cache is present, flush it to the filesystem.
// Will be used when activity starts again.
cache.flush();
/***
* Uninstalls the cache and releases any active resources. Stored contents
* will remain on the filesystem.
*/
//UNCOMMENTING THIS PRODUCES A java.lang.IllegalStateException: cache is closedtry {
// cache.close();
//}
//catch(IOException e){
// e.printStackTrace();
//}
}
}
private Bitmap getImage(String imageUrl) {
if (cache.getImageFromWarehouse(imageUrl) == null) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = inSampleSize;
//installing the cache at application startup.
try {
HttpResponseCache cache = HttpResponseCache.getInstalled();
if (cache == null) {
File httpCacheDir = new File(mContext.getCacheDir(), "http");
long HTTP_CACHE_SIZE_IN_BYTES = 1024 * 1024 * 1024; // 1GB
HttpResponseCache.install(httpCacheDir, HTTP_CACHE_SIZE_IN_BYTES);
//Log.e("Max DiskLRUCache Size", String.valueOf(DiskLruCache.getMaxSize());
}
}
catch (IOException e) {
e.printStackTrace();
}
try {
int readTimeout = 300000;
int connectTimeout = 300000;
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
InputStream stream = connection.getInputStream();
image = BitmapFactory.decodeStream(stream, null, options);
int imageWidth = options.outWidth;
int imageHeight = options.outHeight;
if (imageWidth > desiredWidth || imageHeight > desiredHeight) {
System.out.println("imageWidth:" + imageWidth + ", imageHeight:" + imageHeight);
inSampleSize = inSampleSize + 2;
getImage(imageUrl);
}
else {
options.inJustDecodeBounds = false;
connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.addRequestProperty("Cache-Control", "only-if-cached");
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
stream = connection.getInputStream();
image = BitmapFactory.decodeStream(stream, null, options);
return image;
}
}
catch (Exception e) {
Log.e("getImage", e.toString());
}
}
return image;
}
}
似乎在 doInBackground() 中我将保存到 HttpResponseCache,而在 onPostExecute() 中我将相同的图像保存到 LRUCache。我想做的是:
如果图片没有缓存,保存到HttpResponseCache 如果 HttpResponseCache 已满,则保存到 LRUCache。 如果两者都已满,请使用默认删除机制删除旧的未使用图像。 问题是保存到两个缓存而不是任何一个
编辑重述问题
**
由于两者都缓存相同的图像,两次并将缓存保存到同一文件系统,也许问题应该是使用哪个,因为似乎没有必要同时使用两者...
**
如果有人想要 re-use 上述任何代码,我将只采用 http 响应缓存而不使用 LRU 缓存,因为特别是如果您正在缓存 web 服务响应,例如JSON、xml。为什么?
由于上面的 LRU 缓存实现,我曾丢失 200MB 的设备存储空间。
HTTPResponseCache 的优点:
- 将 HTTP 和 HTTPS 响应缓存到文件系统,以便它们可以 重用,节省时间和带宽
- HttpUrlConnection 做:自动处理缓存机制,
- 借助于加快应用程序的响应时间 HttpResponseCache
- 它从 API 级别 1 开始可用 => 它经受住了时间的考验并且坚固耐用
另一方面:
虽然 LRUCache 相对于 DiskLRUCache 有其优势:
- 您必须实施 class(和其他帮助程序 classes),这意味着如果 android 开发人员的代码发生变化,您将不得不不断下载和编辑您的本地在您的应用程序中断后的版本将被弃用。
- 删除图像后,您可能仍会发现您的磁盘 space 正在使用,因为图像将在您的设备中的某个位置(就像我的情况一样)。
这就是结论...