Question

I am trying to create a simple demo for the ImageLoader functionality for the Android Volley Framework. Constructor is the following:

public ImageLoader(RequestQueue queue, ImageCache imageCache)

The problem is with the ImageCache. Its JavaDoc states:

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. What exactly the 'Implementations must not block' in this context means?
  2. 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?
  3. If no such exist - what may be the negative implications of using my existing implementation which is (just the reading from the file):

    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;
    

    }

Was it helpful?

Solution

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

In your case, you cannot do disk I/O.

This is a Level One (L1) cache, meaning it is designed to return in a matter of microseconds, not milliseconds or seconds. That's why they advocate LruCache, which is a memory cache.

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?

An L1 cache should not be a file cache.

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

An L1 cache should not be a file cache.

Volley already has an integrated L2 file cache, named DiskBasedCache, used for caching HTTP responses. You can substitute your own implementation of Cache for DiskBasedCache if you wish, and supply that when you create your RequestQueue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top