Non-blocking file cache (BitmapLruCache)?

I am trying to create a simple demo for ImageLoader for the Android Volley Framework . The constructor is as follows:

public ImageLoader(RequestQueue queue, ImageCache imageCache) 

The problem is ImageCache . Its JavaDoc says:

Simple cache adapter interface. If provided by ImageLoader, it will be used as the L1 cache before being sent to Volley. Implementations should not be blocked. It is recommended to use LruCache.

  • What exactly does “Implementation Should Not Block” in this context mean?
  • Is there an example of a non-blocking file cache (even non-android, but "clean" java) that I can use to teach myself how to convert my existing file cache to non-blocking?
  • If this does not exist - what could be the negative consequences of using my existing implementation, which is (just reading from a 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; 

    }

+4
source share
1 answer

What exactly does “Implementations Should Not Block” in this context mean?

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

This is the cache of the first level (L1), that is, it is intended to return within microseconds, not milliseconds or seconds. That's why they protect LruCache , which is the memory cache.

Is there an example of a non-blocking file cache (even non-android, but "clean" java) that I can use to educate myself on how to convert my existing file cache to non-blocking?

L1 cache should not be a file cache.

what could be the negative consequences of using my existing implementation, which is (just reading from a file)

L1 cache should not be a file cache.

Volley already has an integrated L2 cache file called DiskBasedCache , used to cache HTTP responses. You can replace your own Cache implementation with DiskBasedCache if you want, and provide it when creating the RequestQueue .

+5
source

Source: https://habr.com/ru/post/1482041/


All Articles