Glide: Put image into Memory (and Disc) cache from custom ModelLoader

Created on 25 Aug 2017  路  4Comments  路  Source: bumptech/glide

I'm using Glide 4.0 and want to have the image loaded from the custom ModelLoader in the Memory and Disc caches. There are two cases.

  1. In the first case, I search for the images URLs first in the custom DataFetcher.loadData() and then just use HttpUrlFetcher with GlideUrl to handle the first found URL.
    I return ObjectKey(artist+album as string) as a Key in ModelLoader.buildLoadData().
  1. In the second case, I have byte[] data that I'm getting from the database. This data is usually extracted from the local file but not always.
    So I use ObjectKey(full path to the local file or title of the song as string) as a Key in ModelLoader.buildLoadData().
    And then I pass ByteArrayInputStream(byte[]) to the callback in DataFetcher.loadData().

In both cases, a found image seem to be cached on the Disc but not put into a Memory cache because I'm able to see placeholders before an image is loaded only on items with custom ModelLoaders. And I don't see any placeholders on items that are not using custom ModelLoaders and loading images from general URLs for example, the image is showing immediately.

What should be done to put image into Memory cache?

question

All 4 comments

Your model needs to implements hashCode() / equals().
The key is now only used as a secondary key for discCache.

@Tolriq ok, will implement that. What about memory cache?

Memory cache is now based on your model hashCode/equals so if you do not implement it correctly Glide can't match your requests and can't use the memory cache :)

If 2 models are equals and same hashCode then Glide do the match and reuse it's memory cache.

The docs have been updated with more insisting on those. Maybe it's still not clear enough?

@Tolriq it works after implementing hashCode/equals in a model, thanks!

I was confused about LoadData constructor expecting Key object public LoadData(Key sourceKey, DataFetcher<Data> fetcher) that is not actually used for constructing a key for the cache.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sergeyfitis picture sergeyfitis  路  3Comments

mttmllns picture mttmllns  路  3Comments

ghost picture ghost  路  3Comments

technoir42 picture technoir42  路  3Comments

kenneth2008 picture kenneth2008  路  3Comments