有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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.

  1. 在此上下文中,“实现不能阻止”的确切含义是什么
  2. 是否有一个非阻塞文件缓存(即使是非安卓但“纯”java)的例子,我可以用它来教育自己如何将现有的文件缓存转换为非阻塞文件
  3. 如果不存在这种情况-使用我的现有实现(仅从文件中读取)可能会有什么负面影响:

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

    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) 个答案

  1. # 1 楼答案

    What exactly the 'Implementations must not block' in this context means?

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

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

    Is there an example of non-blocking file cache (even non-android but "pure" java) which I can use to educate my self how to convert my existing file cache to be non-blocking?

    一级缓存不应是文件缓存

    what may be the negative implications of using my existing implementation which is (just the reading from the file)

    一级缓存不应是文件缓存

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