# Clearing a JUCE::ImageCache

**URL:** <https://forum.juce.com/t/clearing-a-juce-imagecache/33533>\
**Category:** General JUCE discussion\
**Created:** [May 22, 2019, 7:13pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533 "2019-05-22T19:13:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![timur](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/timur/32/541_2.png) [@timur](https://forum.juce.com/u/timur)\
**Post date:** [May 22, 2019, 7:13pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533/1 "2019-05-22T19:13:27Z")

</div>

Is there a way to clear a `JUCE::ImageCache`?

I only see `ImageCache::releaseUnusedImages()`, but that does not have the behaviour I want. If I load an image from a path, and then clear the cache, and then load another instance of Image from the same path, I want that second instance to be reloaded from disk (because I cleared the cache in between). Is that possible somehow?

Thanks!

---

<div class="post-metadata">

**Author:** ![timur](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/timur/32/541_2.png) [@timur](https://forum.juce.com/u/timur)\
**Post date:** [May 22, 2019, 7:17pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533/2 "2019-05-22T19:17:35Z")

</div>

Related to that: is it somehow possible to have more than one `JUCE::ImageCache`? Currently, I can only see how to have a global singleton one. But global state is sometimes not good… this precludes using RAII for managing/clearing the image cache.

---

<div class="post-metadata">

**Author:** ![alassandro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/alassandro/32/4297_2.png) [@alassandro](https://forum.juce.com/u/alassandro)\
**Post date:** [May 22, 2019, 7:24pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533/3 "2019-05-22T19:24:14Z")

</div>

> [@timur](#):
>
> Is there a way to clear a `JUCE::ImageCache` ?
> 
> I only see `ImageCache::releaseUnusedImages()` , but that does not have the behaviour I want.

Yes, you can only clear the cache by nullifying any of your active `Images` that came from the cache and then immediately calling `releaseUnusedImages()`

It seems `ImageCache` is intended only to be used as a global singleton cache… would love to see it become non-global so it can be managed in the way you’re describing 🙂

---

<div class="post-metadata">

**Author:** ![timur](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/timur/32/541_2.png) [@timur](https://forum.juce.com/u/timur)\
**Post date:** [May 22, 2019, 7:26pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533/4 "2019-05-22T19:26:49Z")

</div>

Thanks. Oh well, then I need to implement my own ImageCache…

It’s just ~200 lines of code so should be doable.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 22, 2019, 7:30pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533/5 "2019-05-22T19:30:28Z")

</div>

The cache works simply by sharing the images.  
If you pull an image from ImageCache, you can overwrite the pixels and all other instances will follow.  
Does that help?

---

<div class="post-metadata">

**Author:** ![timur](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/timur/32/541_2.png) [@timur](https://forum.juce.com/u/timur)\
**Post date:** [May 22, 2019, 7:35pm UTC](https://forum.juce.com/t/clearing-a-juce-imagecache/33533/6 "2019-05-22T19:35:46Z")

</div>

I don’t think so.

When I request an image, I need to control whether it’s cached or not, independently from whether cached versions of it were retrieved previously and whether or not those are still around somewhere.
