Ability to clear tagged items in cache
- Dominant language
- Java
- Stars
- 35k
- Forks
- 6.2k
- Avg merge
- 4d 1h
- Merged PRs (30d)
- 8
Description
**Glide Version**: 3.7.1
**Integration libraries**: None
**Device/Android Version**: All/5.0.X+
**Issue details / Repro steps / Use case background**:
I know that it's been stated many times that Glide is not a file manager. I'm just curious about how feasible it would be to add this feature.
I'm using Glide in my application to load images from local storage as well as the network.
It is used for local storage only when displaying all the images a user has on the device in a `RecyclerView`.
The issue is that when I load the image from local storage, it gets resized to fit into a grid three columns wide. I'd like to not have to resize every time the view gets recycled, but if I use `DiskCacheStrategy.RESULT` the app will now have a ton of items in the cache that are only good for this one view, i.e. I only want to cache these resized images while the user is scrolling in the grid. I can't delete them from the cache after the user leaves without deleting the entire cache, and there are other items in there that I actually want to cache. So I use `DiskCacheStrategy.NONE` and `skipMemoryCache(true)`. But that means that every time a user scrolls away from an image and then scrolls back to it, it has to be resized again.
What I'm requesting is a feature that will allow you to tag each request with a `String` so you can later say `clearCache(tag)` which will remove all entries tagged with `tag` from the cache. I know that `Key`s are hashed before putting them in the cache, so the tag could be prepended to the hashed `Key` and then the cache can be iterated, removing keys that start with `tag`.
**Glide load line (in RecyclerView.Adapter#onBindViewHolder)**:
```java
Glide.with(fragment)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.centerCrop()
.crossFade()
.placeholder(ColorDrawable(myColor))
.into(RippleImageViewTarget(imageView, color) // RippleImageViewTarget defined below and imageView is from the RecyclerView.ViewHolder
public class RippleImageViewTarget extends ImageViewTarget {
private final int color;
public RippleImageViewTarget(ImageView imageView, @ColorInt int color) {
super(imageView);
this.color = color;
}
@Override
public void setDrawable(Drawable drawable) {
view.setImageDrawable(new RippleDrawable(color, drawable, null));
}
@Override
public void setResource(GlideDrawable resource) {
setDrawable(resource)
}
}
```
Contributor guide
Research direction
Start from the Glide load line in RecyclerView.Adapter#onBindViewHolder and trace how DiskCacheStrategy, skipMemoryCache(true), request keys, and the disk cache are implemented. Define and test tagged requests so clearCache(tag) removes only matching resized entries while preserving unrelated cache entries; the issue names no source files or tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- mobile, performance
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100