【问题标题】:Non-blocking file cache (BitmapLruCache) implementation?非阻塞文件缓存(BitmapLruCache)实现?
【发布时间】:2013-05-16 17:53:23
【问题描述】:

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

public ImageLoader(RequestQueue queue, ImageCache imageCache)

问题在于ImageCache。它的 JavaDoc 声明:

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

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

    公共字节[] get(字符串文件名) {

    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;
    

    }

【问题讨论】:

    标签: java android android-volley


    【解决方案1】:

    在这种情况下,“实施不得阻塞”究竟是什么意思?

    在你的情况下,你不能做磁盘 I/O。

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

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多