java非阻塞文件缓存(BitmapLruCache)实现?
我正在尝试为Android Volley Framework的ImageLoader功能创建一个简单的演示。构造函数如下所示:
public ImageLoader(RequestQueue queue, ImageCache imageCache)
问题在于ImageCache
。其JavaDoc声明:
Simple cache adapter interface. If provided to the ImageLoader, it will be used as an L1 cache before dispatch to Volley. Implementations must not block. Implementation with an LruCache is recommended.
- 在此上下文中,“实现不能阻止”的确切含义是什么李>
- 是否有一个非阻塞文件缓存(即使是非安卓但“纯”java)的例子,我可以用它来教育自己如何将现有的文件缓存转换为非阻塞文件李>
如果不存在这种情况-使用我的现有实现(仅从文件中读取)可能会有什么负面影响:
公共字节[]获取(字符串文件名){
byte[] ret = null; if (filesCache.containsKey(filename)) { FileInfo fi = filesCache.get(filename); BufferedInputStream input; String path = cacheDir + "/" + fi.getStorageFilename(); try { File file = new File(path); if (file.exists()) { input = new BufferedInputStream(new FileInputStream(file)); ret = IOUtils.toByteArray(input); input.close(); } else { Kh安卓Log.e("Cannot find file " + path); } } catch (FileNotFoundException e) { filesCache.remove(filename); Kh安卓Log.e("Cannot find file: " + path); } catch (IOException e) { Kh安卓Log.e(e.getMessage()); } } return ret;
}
# 1 楼答案
在您的情况下,您不能执行磁盘I/O
这是一个一级(L1)缓存,意味着它被设计为在几微秒内返回,而不是几毫秒或几秒钟。这就是为什么他们提倡
LruCache
,这是一种内存缓存一级缓存不应是文件缓存
一级缓存不应是文件缓存
Volley已经有一个集成的二级文件缓存,名为
DiskBasedCache
,用于缓存HTTP响应。如果愿意,可以用自己的Cache
实现替换DiskBasedCache
,并在创建RequestQueue
时提供