繁体   English   中英

非阻塞文件缓存(BitmapLruCache)实现?

[英]Non-blocking file cache (BitmapLruCache) implementation?

我正在尝试为Android Volley FrameworkImageLoader功能创建一个简单的演示。 构造函数如下:

public ImageLoader(RequestQueue queue, ImageCache imageCache)

问题出在ImageCache 它的JavaDoc声明:

简单缓存适配器接口。 如果提供给ImageLoader,它将在发送给Volley之前用作L1缓存。 实现不得阻止。 建议使用LruCache实现。

  1. 在这种情况下,“实施必须阻止”究竟是什么意思?
  2. 是否有一个非阻塞文件缓存的例子(甚至非android但是“纯”java),我可以用它来教育我自己如何将我现有的文件缓存转换为非阻塞?
  3. 如果不存在 - 使用我现有的实现(仅从文件读取)可能是负面影响:

    public byte [] get(String filename){

     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 { KhandroidLog.e("Cannot find file " + path); } } catch (FileNotFoundException e) { filesCache.remove(filename); KhandroidLog.e("Cannot find file: " + path); } catch (IOException e) { KhandroidLog.e(e.getMessage()); } } return ret; 

    }

在这种情况下,“实施必须阻止”究竟是什么意思?

在您的情况下,您不能执行磁盘I / O.

这是一级(L1)缓存,这意味着它设计为在几微秒内返回,而不是毫秒或秒。 这就是为什么他们提倡LruCache ,它是一个内存缓存。

是否有一个非阻塞文件缓存的例子(甚至非android但是“纯”java),我可以用它来教育我自己如何将我现有的文件缓存转换为非阻塞?

L1缓存不应该是文件缓存。

使用我现有的实现可能是什么负面影响(只是从文件中读取)

L1缓存不应该是文件缓存。

Volley已经有一个集成的L2文件缓存,名为DiskBasedCache ,用于缓存HTTP响应。 如果愿意,您可以替换自己的Cache for DiskBasedCache实现,并在创建RequestQueue

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM